Trying to duplicate gimp grayscale conversion and FX repeat on pdf

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
troyd1
Posts: 73
Joined: 2007-07-29T17:13:19-07:00

Trying to duplicate gimp grayscale conversion and FX repeat on pdf

Post by troyd1 »

I have multi-page color pdfs that I am converting to grayscale. I tried a basic "convert -colorspace grayscale a.pdf b.png" and the results were less than great. I then tried converting it with gimp. Very sharp and clear. After a bunch of research it said that gimp uses the photoshop 0.299 R’ + 0.587 G’ + 0.114 B. I also noticed convert was downsizing the image some. The gimp conversion changed it to 8 bit sRGB, so I came up with this: "convert -density 100 -depth 8 -set colorspace RGB -fx '(
0.299r+0.587g+0.114b)' a.pdf q.png"

The density 100 made it the same size as the gimp conversion. This is not as good as the gimp image, but pretty good.

If I change -set colorspace RGB to -set colorspace sRGB it is worse even though the gimp image says it is sRGB.

2 questions.

Can someone provide a convert statement to duplicate what the gimp image > mode > grayscale does from RGB?

Second, the fx only creates one image and I need one for each page in the pdf like the -colorspace grayscale does. How do i code the convert to apply to each image created from each page?

Thanks in advance for any suggestions!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to duplicate gimp grayscale conversion and FX repeat on pdf

Post by fmw42 »

What is your IM version and platform? Please always provide that information. Also it would help if you provided and example input PDF and your GIMP output PNG.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at viewtopic.php?f=1&t=9620

For novices, see

viewtopic.php?f=1&t=9620
http://http://www.imagemagick.org/scrip ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/

I do not think we know exactly what GIMP is doing. Someone would have to read their code to see what is going on.

Try this in place of -fx (windows syntax) with proper IM syntax

Code: Select all

convert -density 100 a.pdf -set colorspace RGB ^
-channel r -evaluate multiply 0.299 +channel ^
-channel g -evaluate multiply 0.587 +channel ^
-channel b -evaluate multiply 0.114 +channel ^
-depth 8 q.png
or

Code: Select all

convert -density 100 a.pdf -set colorspace RGB ^
-color-matrix "
0.299 0 0
0 0.587 0
0 0 0.114"
-depth 8 q.png
You should then get multiple q-x.png image where x=0,1,2 ... N for each of your N pages, since PNG does not support layers/pages. If you want one image with pages in raster format, use TIFF.

Try with and without -set colorspace RGB. With -set colorspace RGB in a current version of IM, you are creating a linear result with the weights you chose. This is like luminance whereas without -set colorspace RGB you get a non-linear luma result, but with different weights from the usual luma weighting.
troyd1
Posts: 73
Joined: 2007-07-29T17:13:19-07:00

Re: Trying to duplicate gimp grayscale conversion and FX repeat on pdf

Post by troyd1 »

Sorry long day, version information: IM Version: ImageMagick 6.5.4-7 2012-05-07 Q16 OpenMP http://www.imagemagick.org
Copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC

Linux version: Linux mail-relay.xyzzy.com 2.6.32-71.el6.i686 #1 SMP Fri Nov 12 04:17:17 GMT 2010 i686 i686 i386 GNU/Linux

This is a production machine and I can likely not upgrade IM.

Let me know if you need any more info and thanks for the fx info.

Also the gimp version I am using is 2.8.22 if anyone wants to tackle seeing what it is doing. It is a little above my ability to do that.
Last edited by troyd1 on 2017-10-03T04:51:29-07:00, edited 1 time in total.
troyd1
Posts: 73
Joined: 2007-07-29T17:13:19-07:00

Re: Trying to duplicate gimp grayscale conversion and FX repeat on pdf

Post by troyd1 »

I tried with and without the -set colorspace RGB and it is the same.

As a note, I am looking for a filter that will make text that is scanned the sharpest and most readable if anyone has a suggestion on what to try for that. Going to 8 bit helped a lot and I don't want to go any higher on density because of file size.
troyd1
Posts: 73
Joined: 2007-07-29T17:13:19-07:00

Re: Trying to duplicate gimp grayscale conversion and FX repeat on pdf

Post by troyd1 »

Also, I got the photoshop idea from this post: https://www.imagemagick.org/discourse-s ... hp?t=10984

"In Adobe Photoshop's imaging operations, luminosity is the term used incorrectly to refer to the luma component of a color image signal; that is, a weighted sum of the nonlinear red, green, and blue signals. It seems to be calculated with the Rec. 601 luma co-efficients (Rec. 601: Luma (Y’) = 0.299 R’ + 0.587 G’ + 0.114 B’)."

When I tried the code provided above with the channels, it gave me a very green page and not grayscale. I must not be understanding the meaning of that or converting it to practice correctly. Any suggestions there?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to duplicate gimp grayscale conversion and FX repeat on pdf

Post by fmw42 »

In such an old version of IM, RGB and sRGB were swapped. So you are actually working in non-linear sRGB even though you request it with RGB.

Also note that -set colorspace RGB does not change the pixel data. It simply assigns the data that colorspace. To actually change the pixel data you need to use -colorspace rather than -set colorspace. But in your case you are already in RGB colorspace (which is actually non-linear)

Those Luma values are the same as -colorspace gray in your version of Imagemagick. In your old version of IM, -color-matrix was called -recolor. See http://www.imagemagick.org/script/comma ... colorspace. So this should do the same as what you were doing.

Code: Select all

convert -density 100 image.pdf -colorspace gray -depth 8 grayimage.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to duplicate gimp grayscale conversion and FX repeat on pdf

Post by fmw42 »

Can you post your PDF image so we can test with it?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to duplicate gimp grayscale conversion and FX repeat on pdf

Post by fmw42 »

Using IM 6.9.9.15 Q16 Mac OSX, I can convert the IM internal color logo: image to png. Then make it grayscale in GIMP and in Imagemagick as follows and get virtually identical results.

Code: Select all

convert logo: logo.png
convert logo.png -grayscale rec601luma logo_im_gray.png
compare -metric rmse logo_im_gray.png logo_gimp_gray.png null:
665.193 (0.0101502)
So this is about 1% different and is not visually different.

In IM 6.5.x.x -colorspace gray is identical to the current -grayscale rec601luma

So if you are not getting good results, perhaps it is because your PDF file is a vector shell containing a raster image which could be a lossy JPG or have some other colorspace such as CMYK. Perhaps you should remove the raster image from the PDF and process it. You can do that with pdfimages tool.
troyd1
Posts: 73
Joined: 2007-07-29T17:13:19-07:00

Re: Trying to duplicate gimp grayscale conversion and FX repeat on pdf

Post by troyd1 »

http://drive.google.com/drive/folders/0 ... 2Nmek9sNGM

Above is a link to my test files.

The test.pdf file is the original.

The test-gimp.png is test.pdf converted to png using export as with default options.

The test-gimp-gray.png is test.pdf converted to png after selecting image > mode > grayscale using export as with default options.

The test-im.png is test.pdf convert to png using the im command "convert -density 100 test.pdf test-im.png".

The test-im-gray.png is test.pdf convert to png using the im command " convert -density 100 -depth 8 -set colorspace RGB -color
space gray test.pdf test-im.png".

You can see how much clearer the text is when you zoom in on the gimp conversions vs the im conversions.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to duplicate gimp grayscale conversion and FX repeat on pdf

Post by fmw42 »

Try supersampling as follows:

Code: Select all

convert -density 400 test.pdf -resize 25% -colorspace gray -depth 8 grayimage.png
or

Code: Select all

convert -density 400 test.pdf -resize 25% -colorspace gray -colorspace sRGB -depth 8 grayimage3.png
Note I use -colorspace sRGB to get linear gray, because in your old version (6.5.4-7) of IM sRGB and RGB were swapped.

Also be sure your Ghostscript is current, since Imagemagick uses GS to process PDF images.
troyd1
Posts: 73
Joined: 2007-07-29T17:13:19-07:00

Re: Trying to duplicate gimp grayscale conversion and FX repeat on pdf

Post by troyd1 »

That really worked well. Thanks for the advice!

I tried colorspace RGB, sRGB and not adding the parameter and they looked identical.
Post Reply