Page 1 of 1

How to combine density/resize converting pdf->jpg?

Posted: 2011-04-15T05:01:44-07:00
by Nairi
Hi!

Due to the fact that I slowly but surely go crazy trying out what I have to set in which way I decided to simply ask, even if it will be a really easy solution.

First of all my environment:
ImageMagick: 6.6.9-4 2011-04-01 Q16
GhostScript: 9.02
OS: Windows 7 Pro x64

What I want to do:
I have some pdf-files that I should convert to jpg-images, but two things I have to keep in mind converting them:

1. they have to have 300dpi
2. they have a maximum size of w: 990px an h: 1400px.

As far as I have seen the pdfs have transparent background, but I don't even know if that has to do with my problem. If I convert it with

Code: Select all

convert -density 300 test.pdf -quality 85 testpic.jpg
all is good, except the imagesize. If I change it to:

Code: Select all

convert -density 300 test.pdf -resize 990x1400 -quality 85 testpic.jpg
I have the right dpis, the right size BUT the formerly transparent background went black. So... any ideas what I can do? And yes, if I scale down the images from my first convert-call, the image is OK. But somehow it has to go in one statement, right?

Re: How to combine density/resize converting pdf->jpg?

Posted: 2011-04-15T07:36:18-07:00
by anthony
It went black because JPEG images do not save transparency!!!

It did not appear before because typically -- transparency has color -- In this case white from the PDF conversion! Resizing the image removed that color, setting it to black, because mathematically the color of a transparent area does not matter!

See... JPEG transparency - NOT
http://www.imagemagick.org/Usage/formats/#jpg_trans

For solutions see Removing Transparency
http://www.imagemagick.org/Usage/masking/#remove

You can remove the transparency either before or after the resize, that does not matter.

Re: How to combine density/resize converting pdf->jpg?

Posted: 2011-05-12T02:31:57-07:00
by Nairi
First of all... sorry for the late reply, but I was on holiday and had no time to check back here.

Thanks a lot for the answer but I want also to clearify something... I know that jpeg don't use transparency, that means that I simply wonder why ImageMagick renders it once with a white background and a different time with black background, thats all. I know that you stated why, but if you don't know what happens in the background (during the conversion) its simply not understandable :wink:

I will add my solution as soon as I have it. *goes over testing*

Edit:
And here we go:

Code: Select all

convert -density 300 test.pdf -background white -alpha off -resize 990x1400 -quality 85 test.jpg

Re: How to combine density/resize converting pdf->jpg?

Posted: 2011-05-12T18:22:45-07:00
by anthony
The -background white does nothing in the above. -alpha off will just turn off transparency making the existing (white) color valid.

If you want to force transparency to white, replace -alpha off with -flatten (for a single image)
OR use -bordercolor white -border 0

See Removing Transparency
http://www.imagemagick.org/Usage/masking/#remove

Re: How to combine density/resize converting pdf->jpg?

Posted: 2011-05-13T00:42:09-07:00
by Nairi
OK... if I understood you and the link right, this should work correctly:

Code: Select all

convert -density 300 test.pdf -bordercolor white -border 0 -resize 990x1400 -quality 85 testpic.jpg
And what flatten concerns... I tried it out just before posting in this forum and had some funny results, because I had a multipage pdf file to convert and I think you can imagine what had happend to that file :wink:

Thanks a lot again for the help.

Re: How to combine density/resize converting pdf->jpg?

Posted: 2011-05-15T05:34:36-07:00
by anthony
Glad to see it worked out. We are glad to help any way we can.