White background lost when upgraded imagemagick

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?".
artofsoundmusic
Posts: 10
Joined: 2013-01-26T09:43:10-07:00
Authentication code: 6789

White background lost when upgraded imagemagick

Post by artofsoundmusic »

I'm converting PDFs to PNGs using ImageMagick and Ghostscript.

For the attached link, the image on the left was created used ImageMagick 6.7.5 and Ghostscript 9.05.
The command was
$cmd = '/usr/bin/convert upload/foo.pdf -density 1200x1200 -quality 100 -resize '.$width.'x'.$height.' rename/foo.png ';

see http://imgur.com/TFXZ6nu


My host changed the software on server which now has ImageMagick 6.8.0-1 and Ghostscript 9.06. I ran my script again and page was now transparent instead of white. With a black webpage behind it, the music notes could not be seen.

I changed the command to try to force the background to be white, by adding alpha off as follows
$cmd = '/usr/bin/convert upload/foo.pdf -density 1200x1200 -quality 100 -alpha off -resize '.$width.'x'.$height.' rename/foo.png ';

this made the background white as desired, but the image quality is now degraded (image on the right).

What is the best way to have a white background AND same music quality as before?

Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: White background lost when upgraded imagemagick

Post by fmw42 »

Neither images shows black obscurations. The one on the right has a little darker background behind the notes. That may be due to the fact that IM now considers grayscale images as linear rather than non-linear (different gamma). Can you post your original PDF?

Also see
http://www.imagemagick.org/script/forma ... colorspace

Also in general, when dealing with vector images such as pdf for input, one sets the density before reading the pdf.

There is also the possibility that your delegates.xml file needs to be edited to change the SDEVICE to pngalpha or pnmraw depending upon whether you want one page pdf with transparency or mult-page pdf without transparency.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: White background lost when upgraded imagemagick

Post by snibgo »

The transparency probably provided anti-aliasing, which "smooths" outlines, so simply turning off the alpha has probably removed this. Instead, you might (for example) composite the image against a white background.

Can you provide the actual PDF file?
snibgo's IM pages: im.snibgo.com
artofsoundmusic
Posts: 10
Joined: 2013-01-26T09:43:10-07:00
Authentication code: 6789

Re: White background lost when upgraded imagemagick

Post by artofsoundmusic »

I was thinking about compositing and will do this if there isn't a slight easier fix.

The pdf can be gotten here

http://www.atlasbrass.com/scratch.pdf

Let me know what you think.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: White background lost when upgraded imagemagick

Post by snibgo »

My installations out-of-the-box (v 6.7.9, 6.8.0-9 and 6.8.1-10) give a white backgound. Your service provider may have changed this default behaviour.

So I can't test this, but I suspect that "-background White -flatten" will do what you want.

Code: Select all

convert -density 1200 scratch.pdf -background White -flatten scratch.png
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: White background lost when upgraded imagemagick

Post by fmw42 »

This command works fine for me with no black background in IM 6.8.1.10 Q16 Mac OSX Snow Leopard


convert -density 600x600 scratch.pdf -density 72 -units pixelspercentimeter scratch.png


Note that -quality 100 may not be appropriate for png. See http://www.imagemagick.org/script/comma ... hp#quality

The only problem that I am having is that Mac Preview and Safari will not display the resulting PNG. I was able to view it GraphicConverter, but only after telling it what density to use. So this is very strange. Using libpng 1.5.13
artofsoundmusic
Posts: 10
Joined: 2013-01-26T09:43:10-07:00
Authentication code: 6789

Re: White background lost when upgraded imagemagick

Post by artofsoundmusic »

Thanks for the the responses.

I tried the flatten suggestion, .....

$cmd = '/usr/bin/convert upload/foo.pdf -density 1200 -background White -flatten -resize '.$width.'x'.$height.' rename/foo.png ';

this made the background white, but since the actual PDF is multipage (my sample was just 1) each page was superimposed upon the other, which isn't good. That said, the quality seemed a little better, but not as good as what I had on the old server.

-background White without -flatten doesn't work at all.

At this point, I would be indebted if you could assist me to create the correct composite command. I think it is best to "draw" a totally white rectangle which would be ($width.'x'.$height.) and place the music image over it. It should be a lengthening of my 1 line command such that it will repeat for each page.

I would like to start with my old command

$cmd = '/usr/bin/convert upload/foo.pdf -density 1200x1200 -quality 100 -resize '.$width.'x'.$height.' rename/foo.png ';


If that doesn't work, I'll go back to my hosting company to see if something they did is causing the transparency. What Imagemagick settings would I ask them about?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: White background lost when upgraded imagemagick

Post by snibgo »

We've had a few threads recently about multi-page transparent tiffs, and adjustments made to an XML file to suit. These don't cause transparency, but retain it. You want to remove it. Yes, "-flatten" will flatten all the pages into one image. Try "-background White -alpha Remove".
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: White background lost when upgraded imagemagick

Post by fmw42 »

Ghostscript cannot handle both multi-page pdfs and transparency, as I understand it. IM relies upon Ghostscript. You will likely need to choose one or the other by setting the sDevice in the delegates.xml file to either pngalpha for transparency or pnmraw for multi-page pdfs.

see

<delegate decode="ps:alpha" stealth="True" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pnmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>

If you disable transparency before reading the pdf, you may be able to get away with reading the multipage file by using pnmraw. I have not tested that.

try

convert -background white -alpha background -alpha off -density XX image.pdf ..... image.png

see
http://www.imagemagick.org/Usage/maskin ... background
artofsoundmusic
Posts: 10
Joined: 2013-01-26T09:43:10-07:00
Authentication code: 6789

Re: White background lost when upgraded imagemagick

Post by artofsoundmusic »

I had the hosting company change delegates.xml and this caused the png creation to fail altogether.

I then asked them set im and gs to the prior versions I had before their server crashed. The pngs generate again, but still the part o fthe page that is supposed to be white is transparent.

I need to try some more things...just not sure what.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: White background lost when upgraded imagemagick

Post by snibgo »

Have you tried my "-background White -alpha Remove"?

Or Fred's "-background white -alpha background -alpha off"?
snibgo's IM pages: im.snibgo.com
artofsoundmusic
Posts: 10
Joined: 2013-01-26T09:43:10-07:00
Authentication code: 6789

Re: White background lost when upgraded imagemagick

Post by artofsoundmusic »

Yes I have. No luck.

I think I am going to install a local im/gs copy on my windows PC to see what happens. I think there is a problem with their (Linux) server.

My other choice is to try to bypass im and work directly with gs to create the pngs.
artofsoundmusic
Posts: 10
Joined: 2013-01-26T09:43:10-07:00
Authentication code: 6789

Re: White background lost when upgraded imagemagick

Post by artofsoundmusic »

There may be another way to solve this. I already do a composite step later on, dissolving in a watermark

$cmd = '/usr/bin/composite -dissolve '.$watermark_darkness.' '. $wmtype .' scaled_watermark.jpg ' . $file. ' '. $file;

Is there a way I can put a white background behind all of this?

I tried playing around with "texture xc:white" but it mostly made the pages all black.
artofsoundmusic
Posts: 10
Joined: 2013-01-26T09:43:10-07:00
Authentication code: 6789

Re: White background lost when upgraded imagemagick

Post by artofsoundmusic »

I was able to get in a white background during the composite step. Quality seems to be improved but not as good as the left side of the imgur link. I have the hosting company trying to tackle the original problem I posted, we'll see where that goes.
artofsoundmusic
Posts: 10
Joined: 2013-01-26T09:43:10-07:00
Authentication code: 6789

Re: White background lost when upgraded imagemagick

Post by artofsoundmusic »

snibgo,

can you post your imagemagick program settings? I think they can be obtained similar to the following

$ cd ImageMagick-6.8.2-0
$ ./configure
Post Reply