Skip to content

Instantly share code, notes, and snippets.

@tsega
Last active February 11, 2016 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsega/c0a338c357bcfc94d09c to your computer and use it in GitHub Desktop.
Save tsega/c0a338c357bcfc94d09c to your computer and use it in GitHub Desktop.
A ruby command line script to generate jpg's from the first pages of pdf files.
#!/usr/bin/env ruby
# This require that you have ImageMagick and Ghostscript installed and available on your PATH
# It also assumes you have you pdf files in `pdfs` folder
# Note: Since it generates jpg's it does not handle transparency very well
# modify the .jpg to .png to generate png files.
Dir['pdfs/*.pdf'].each do |pdf_file|
jpg_file = pdf_file.chomp(File.extname(pdf_file)) + ".jpg"
system("convert -density 144 #{pdf_file}[0] -resize 200x300 #{jpg_file} ")
end
@alanorth
Copy link

I prefer GraphicsMagick, the ImageMagick fork with better code quality, more sane command line options, and a less annoying developer. :)

$ gm convert -quality 85 -thumbnail x600 -flatten 64661.pdf\[0\] cover.jpg

The results are very similar obviously! One tip, I prefer the "x600" notation, which gives 600px height at whatever width is appropriate for the aspect ratio—hey, these thumbnails will be around for decades, on god-only-knows-what-resolution devices right?

edit: Also, there are diminishing returns in JPEG quality after around 85 or so, so I always use 82–85.

@tsega
Copy link
Author

tsega commented Feb 11, 2016

These thumbnails will be around for decades, on god-only-knows-what-resolution devices right?

Usually, keeping a high-resolution copy that you could manipulate on the fly is a good idea. Progress in the Responsive Images community is promising in getting rid of the whole device-screen nightmare.

@alanorth
Copy link

We could always re-process them, you're right. For now, they need to be high-quality enough to be used wherever our partners might use them, for example:

https://ccafs.cgiar.org/publications/how-countries-plan-address-agricultural-adaptation-and-mitigation

The size of these is more or less ok, but the quality is horrible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment