Grayscale matters

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
visionhappens
Posts: 3
Joined: 2013-02-02T19:04:12-07:00
Authentication code: 6789

Grayscale matters

Post by visionhappens »

Hi all

I have a variety of scanned PDFs that are black and white like the following


Many thanks from a massive newbie
Last edited by visionhappens on 2013-02-02T19:48:53-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colorizing scanned grayscale PDF

Post by fmw42 »

You do not say what resulting image format you want? If you process to pdf, IM will rasterize and then convert that to PDF.

because you want to keep the antialiasing of the text, you should try

convert test.pdf +level-colors red,white result.png (or gif or jpg)


see
http://www.imagemagick.org/Usage/color_ ... vel-colors
http://www.imagemagick.org/Usage/formats/#vector
visionhappens
Posts: 3
Joined: 2013-02-02T19:04:12-07:00
Authentication code: 6789

Re: Colorizing scanned grayscale PDF

Post by visionhappens »

fmw42 wrote:You do not say what resulting image format you want? If you process to pdf, IM will rasterize and then convert that to PDF.

because you want to keep the antialiasing of the text, you should try

convert test.pdf +level-colors red,white result.png (or gif or jpg)


see
http://www.imagemagick.org/Usage/color_ ... vel-colors
http://www.imagemagick.org/Usage/formats/#vector
Thanks very much sir.
Indeed ideally output would be pdf again. Just tried your suggestion and the resulting document is awfully small so text can't be read. Any suggestions how to recoup a clear readable document very welcome.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Colorizing scanned grayscale PDF

Post by snibgo »

For example, green text on a white background:

Code: Select all

convert -density 150 test.pdf lecture2.png

convert lecture2.png -alpha Copy -fuzz 100% -fill White -opaque Gray -background Green -alpha Remove lecture3.png

convert lecture3.png lecture3.pdf
I've shown it as three stages, but you could combine them into one.
snibgo's IM pages: im.snibgo.com
visionhappens
Posts: 3
Joined: 2013-02-02T19:04:12-07:00
Authentication code: 6789

Re: Colorizing scanned grayscale PDF

Post by visionhappens »

snibgo wrote:For example, green text on a white background:

Code: Select all

convert -density 150 test.pdf lecture2.png

convert lecture2.png -alpha Copy -fuzz 100% -fill White -opaque Gray -background Green -alpha Remove lecture3.png

convert lecture3.png lecture3.pdf
I've shown it as three stages, but you could combine them into one.
This is splendid thank you so much!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Colorizing scanned grayscale PDF

Post by snibgo »

I cross-posted with fmw42. His "+level colors" is far more elegant, and probably quicker, than my version that messes around with alpha. My "-density 150" is what you need for readable text.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colorizing scanned grayscale PDF

Post by fmw42 »

Since you are starting with PDF which is vector, it has no size. You have to specify the density you want in dpi. You probably need to use supersampling to increase the quality and then resize to the same input size (assuming it is 72 dpi nominally). What I usually do is

convert -density 288 test.pdf +level-colors red,white -resize 25% result

where 72*4=288 and 1/4=25%

But note that your output will be a raster image imbedded in a vector wrapper.

If you want a larger output size change the -resize argument to a larger value or just remove the -resize and adjust the -density to a value that gives you the size output in pixels that you want.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Grayscale matters

Post by snibgo »

For PDFs that are a single raster image in a vector wrapper, as this one is, I open it in Adobe Reader and control-C the image, which copies it to the clipboard. Then ...

Code: Select all

convert clipboard: info:
... tells me how many pixels it has.

Then I try a simple convert (at 72 ppi)...

Code: Select all

convert test.pdf info:
... and arithmetic tells me the most obvious density to use (usually rounded to the nearest multiple of 150).

(This is in Windows7.)
snibgo's IM pages: im.snibgo.com
Post Reply