I want to generate thumbnails of PDFs at a fixed width (548px wide, with the height determined by the PDF page aspect ratio). With ImageMagick, I can do this with the command:
$ convert -density 300 -resize 548x input.pdf thumbnail.png
This works for arbitrary sized PDFs, however, the larger the PDF the longer it takes. From the documentation I understand that this is because ImageMagick first has to read in the PDF and rasterize it (at 300 DPI), before scaling it down. This obviously takes longer and more memory the larger the PDF is.
My question is: Can we tell ImageMagick to rasterize the PDF to a set resolution when reading it in (something like 2 or 3 times larger than the thumbnail needs to be) before scaling it down and outputting the PDF? This should allow our conversion to run in roughly the same amount of time regardless of the PDF size.
We can do this ourselves manually by reading the PDF resolution first and then calculating the appropriate DPI to generate a rasterized PDF of the right size, but this seems like a bit of a hack and I would expect something like this built into ImageMagick.
This is not possible with the current version of ImageMagick (6.8.8-7). We found your post here and added the following feature that will be available in ImageMagick (6.8.8-8):
You can scale a PDF to a width of 548 pixels with the following command:
$ convert -define psd:fit-page=548x input.pdf thumbnail.png
If you create an image with another DPI than the default (72 DPI) you will have to resize it afterwards:
$ convert -density 300 -define psd:fit-page=548x input.pdf -resize 548x thumbnail.png
Related
I am trying to convert a LaTeX-generated PDF file to a PNG file with anti-aliasing and a transparent background (white text on a black background). After having read the answer to this post and one of the comments to the answer, I compared the convert function of ImageMagick against pdftoppm. So far, the highest quality anti-aliased images that I can generate are using pdftoppm (for a given DPI resolution). I use the following command:
pdftoppm -png -r 2000 text.pdf > text.png
The equivalent command (or so I think) using ImageMagick was:
convert +antialias -interpolate Nearest -density 2000 text.pdf -quality 90 -colorspace RGB text.png
However, I did not get as good-quality anti-aliasing using ImageMagick as I did with pdftoppm. In fact there hardly seems to be any anti-aliasing in the ImageMagick-generated image. See the close-ups below:
pdftoppm image:
ImageMagick image:
So where this leaves me is that I am satisfied with the anti-aliasing that pdftoppm provides. However, ImageMagick seems to have more functionality in now converting the anti-aliased image such that the black background is transparent. I have applied the approaches detailed in this post using ImageMagick, but they compromise the quality of the anti-aliasing that was previously satisfactory.
Can anyone advise me on how to solve the issue of obtaining a transparent background (which will always be black in color) while not affecting the anti-aliasing quality? Additionally, if the ImageMagick command that I used above was sub-optimal for generating a high-quality anti-aliased image, is there a way that I can achieve both anti-aliasing as well as background transparency by using ImageMagick alone? Any form of advice/tips would be much appreciated!
P.s. Since this question is partially LaTeX-related (I use LuaLaTeX to compile the PDF), I have posted a related question here regarding whether there is a much more straightforward way of directly generating the PDF file with a transparent background.
EDIT:
I've managed to fix the issue of transparency based on some comments on the question I posted on the TeX stack exchange. Now it's just about how I can improve the quality of anti-aliasing. Is there a way that I can achieve the same quality anti-aliasing that I get from pdftoppm?
The pdf file that I am converting can be found on this Dropbox link. Note that the font colour is white, and the background shows as white too (in my pdf viewer anyway), but is transparent. This is the converted PNG file.
You should use -density to increase the anti-aliasing of your PDF to PNG conversion. I note that your image is opaque white and the text is simply in the alpha channel.
convert -density 600 text.pdf -alpha extract x.png
If on Imagemagick 7, change convert to magick.
If you want to keep the transparency and keep your text white, then
convert -density 600 text.pdf y.png
The image is above, but will look completely white and blend with the white background. So you will need to download it.
If you want black text on transparency, then
convert -density 600 text.pdf -alpha extract -alpha copy -channel rgb -negate +channel z.png
I have software allowing in input any image with valid vertical A4 pages.
My problem is that the PDF we are receiving can possibly have a random page turned into horizontal (landscape) mode.
I don't want to turn the content, only the page to make it feet in the software, like basically a 90 rotate left on all pages that are in landscape orientation.
This is the actual command I use: convert -scene 1 -density 200 .\TEST2.pdf page-%d.jpg
What I have:
What I would wish:
-auto-orient does not solve my problem.
Command:
convert -auto-orient -scene 1 -density 200 .\multiorientconvert.pdf page-%d.jpg
Result:
Thank you.
I'm trying to output a PDF file from a JPG file using Ghostscript. The following command works fine:
gs -sDEVICE=pdfwrite -sPAPERSIZE=a4 -o /pdf_from_image.pdf /path/to/viewjpeg.ps -c \(/source_image.jpg\) viewJPEG
Based on existing threads and Ghostscript documentation I'm using -sPAPERSIZE=a4 to generate the output in A4 format. The PDF generates fine, but the PROBLEM is when the image dimensions don't match that of A4, GS puts the image at the bottom of the page with best "width" fit. I think it actually tries to put it in the lower left bottom. To add to it, at times the image is auto rotated.
My question is:
1) Is there any option to put the image on top left corner of the page.
2) Stop GS auto rotating the image.
Any help to put me in the right direction would be greatly appreciated. Thanks.
PDF and PostScript use a coordinate system with the origin (0,0) in the lower left corner, so Ghostscript is actually doing the 'correct' thing: putting the image at the origin. To place the image at the top, you'd have to subtract the image height from the page height and translate the image upwards by that amount.
As for why some images are being rotated, I can't say for sure. Some JPGs contain metadata that indicates the intended orientation of the image--however, not all software gets the value right. I don't know if Ghostscript respects that metadata, but you could check if your 'bad' images have the correct orientation tag (you can use Exif or similar to inspect them).
I would like to know if there is a way to resize a picture while keeping it's pixelisation
convert ./TEST.png -resize 723x523 -compose Copy -gravity center -extent 723x523 -quality 92 ./TEST_big.png
Illustration from 20x20 to 100x100 here: image
`
Thanks
No one has answered this so I will put what I've done.
I've actually made a Python script who, for each pixel is making a square of n-pixels (10 for example). I'm making the output larger than the picture size I actually want(because I'm only doing squares) with GDAL and then resize it with imageMagick.
I'm having troubles with converting PDF to PNG in ImageMagick. I've used different variations, including using Ghostscript and piping the output to ImageMagic (as given here - which doesn't seem to work for me, so I have to save to a temp file instead of using a pipe). I've found this is working best for me:
convert -background transparent -density 150x150 Test.pdf Test-IMSoloOut.png
Here's a screenshot of the original PDF in my viewer (Preview, on OS X):
And when I convert it, I get this:
The problem is it's dropping the margins. I'm not clear whether it's making them transparent or what, but the formatting of the image looks sloppy without the margins.
I was given this to try:
convert -density 150x150 Test.pdf -gravity center -background transparent -extent 612x792 Test-Extras.png
And that just gives me one section of the page:
I tried adapting that and changed the 612x792 to 1275x1650, which would fit 150DPI multiplied by the size of the page (8.5" x 11"). When I did that, I got a large image, but, again, the margins were gone, like in the 2nd image above.
These images will be displayed in a Java program that will be displaying a number of pages, so the margins could vary (in other words, I can't just wrap a set border around the output, as seen in the 2nd image, since the border size will change in some images).
I've seen a number of hits on removing margins for PDF to PNG conversion, but they don't seem to provide anything to help me.
My guess is that, in the conversion process, the margins are converted to something transparent, so I tried adding "-transparent white" to the command line for convert, but it didn't help.
What can I do to either get the margins back or, if they're there and not visible, how can I get them to display? Basically I want the PNG file (image #2) to look like the PDF file with margins included (image #1).
I suspect I'm missing something obvious - I hope it's that easy.
How can I make sure the space in the margins show up?
I never got an exact answer, but one of the ImageMagick people was quite helpful with this. Basically, I needed to use the option "-extent" to specify how big the image was. Since I was using a different density than the default, I had to take that into account, too. The command line that worked was:
convert -density 150x150 InputFile.pdf -background white -units PixelsPerInch -density 150 -extent 1275x1650 OutputFile.png
The 1275x1650 comes from multiplying 150 (for the DPI) by 8.5" for the width and by 11 for the height.