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

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
Nairi
Posts: 3
Joined: 2011-04-15T03:32:08-07:00
Authentication code: 8675308

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

Post 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?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Nairi
Posts: 3
Joined: 2011-04-15T03:32:08-07:00
Authentication code: 8675308

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

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Nairi
Posts: 3
Joined: 2011-04-15T03:32:08-07:00
Authentication code: 8675308

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

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post by anthony »

Glad to see it worked out. We are glad to help any way we can.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply