I have a pdf
and I'd like to convert it to a series of images, numbered 0001.jpg - XXXX.jpg ?
I found this so far
sips -s format png your_pdf_file.pdf --out your_png_file.png
but it seems to only to the first page of the pdf, and the quality of the outputted png seems not so good. Any suggestions?
24 Answers
Alternatively you can use ghostscript which is on every mac preinstalled (and what is what imagemagic uses under the hood)
gs -dSAFER -dQUIET -dNOPLATFONTS -dNOPAUSE -dBATCH \ # When converting multiple-page PDFs you should add "%d" to the filename-string # which will be replaced with the sequence-number of the file -sOutputFile="outputFileName" \ # Create a PNG-File with alpha-transparency -sDEVICE=pngalpha # resolution in DPI -r72 \ # Use high grade text antialiasing. Can be 0, 1, 2 or 4 -dTextAlphaBits=4 \ # Use high grade graphics anti-aliasing. Can be 0, 1, 2 or 4 -dGraphicsAlphaBits=4 \ # If you are converting a CMYK-PFD to RGB-color you should use CIE-Color -dUseCIEColor \ # use the PDFs Trim-Box to define the final image -dUseTrimBox \ # start converting on the first side of the PDF -dFirstPage=1 \ # convert only until the first page of the PDF -dLastPage=1 \ # Path to the file you want to convert InputFile.pdfFor more information on how to use the CLI-Parameters, have a look at
4You can use convert ( ImageMagick ) to parse PDF files.
PNG output
Convert test.pdf to numbered PNG images:
convert -density 150 -trim test.pdf page%d.png
JPEG output
Convert test.pdf to numered JPEG images:
convert -density 150 -trim test.pdf -quality 100 -flatten -sharpen 0x1.0 page%d.jpg
As per @JBWhitmore's explanation from his great answer):
convert \ -verbose \ -density 150 \ -trim \ <your-PDF-file>.pdf \ -quality 100 \ -flatten \ -sharpen 0x1.0 \ page%d.{png,pdf}It results in the left image. Compare this to the result of my original command (the image on the right):
4(To really see and appreciate the differences between the two, right-click on each and select "Open Image in New Tab...".)
Also keep the following facts in mind:
- The worse, blurry image on the right has a file size of 1.941.702 Bytes (1.85 MByte). Its resolution is 3060x3960 pixels, using 16-bit RGB color space.
- The better, sharp image on the left has a file size of 337.879 Bytes (330 kByte). Its resolution is 758x996 pixels, using 8-bit Gray color space.
So, no need to resize; add the
-densityflag. The density value 150 is weird -- trying a range of values results in a worse looking image in both directions!
ImageMagick
The easiest way is to use ImageMagick. Install ImageMagick if you haven't already
brew install imagemagick --with-fontconfig --with-ghostscript --with-openjpeg --with-webp Convert the PDF document into a series of images
convert -quality 100 -density 200 -colorspace sRGB "The_Artificial_Intelligence_Crush_2018.pdf" -flatten output-%02d.jpgMore details on producing sharp, high-quality image using ImageMagick.
Ghostscript
ImageMagick uses Ghostscript in the background to perform the PDF conversion. So why not use Ghostscript directly?
gs -dNOPAUSE -sDEVICE=jpeg -r200 -dJPEGQ=100 -sOutputFile=document-%02d.jpg "The_Artificial_Intelligence_Crush_2018.pdf" -dBATCHReference
Convert PDF to image with high resolutionHow to convert multi-page PDF to images in command lineHow to convert a PDF document into Powerpoint slides
just rename sample.pdf to sample.zip and then unzip sample.zip - it will create a folder with the images