#374 Image Manipulation
Aug 16, 2012 | 11 minutes |
Tools
Learn how to do extensive image manipulation with the ImageMagick commands. Also learn how RMagick can be used in combination with CarrierWave to process uploaded images.
- Download:
- source codeProject Files in Zip (54.2 KB)
- mp4Full Size H.264 Video (30.4 MB)
- m4vSmaller H.264 Video (14.5 MB)
- webmFull Size VP8 Video (15.3 MB)
- ogvFull Size Theora Video (30.3 MB)
Awesome stuff, Ryan, as usual. Btw, does anyone know of an easy way to manipulate pdfs, specifically to create a jpg of a pdf? I tried with minimagick with no success.
have you tried prawn?
I will definitely look into it. I seem to remember prawn was more of a pdf generator than converter, but I'll take a second look. :)
if you want to get down into the nitty gritty and really focus on the formatting - perhaps take a look at Latex for rails, it was exactly what I was looking for.
Try this from the command line
convert -size 300x300 doc.pdf doc.jpg
Change the size and file names to suit.
Credit StackOverflow
Hmm. This worked, but the image came out with severe decrease in quality. But thanks very much for sending me in the right direction.
I used the method below to generate page previews of uploaded pdf files.
system "convert -verbose -density 150 #{file_path}[#{from_page}-#{to_page}] -quality 100 #{output_file_path}.png"
HIH
I'm probably too late to help, but for anyone else wondering... ghostscript is your friend here. Quality can certainly be an issue like you mention in a later post. So what I do is create an intermediary image rasterized at 288 dpi (with -r 288) - I like to use png for this. Then convert that to a thumbnail or whatever in any format with imagemagick. I like to use png because if you say have something with white letters you can then use imagemagick to convert the transparency to black or something so the white letters will be visible - I've encountered this with some eps images.
Imagemagick delegates rasterizing to ghostscript, but I find it's better to just cut out the middle man and create a high dpi image that can be more easily manipulated with imagemagick directly. Postscript is a pain, but if you have to use it then ghostscript is the right tool for the job - at least the first part of it.
Obviously I share Ryan's love/hate relationship with imagemagick.
P.S. magick bindings suck - I always use a wrapper or system calls.
Riding the wave of the subject, could talk, in a forthcoming episode, about progress bar upload. How about?
I'm using Mini Magick, partly because of reported problems with Rmagick and because Rmagick didn't play nicely on my set-up.
great episode Ryan! It's good to see the powerful features of imagemagick used in a real world situation as opposed to just being listed.
This is great, Ryan! Your stamp project looks really neat! Is the code available? If so, can you link to the show notes?
There's a link to the show notes to the left of the 'Comments' button.
Thanks -- I am aware of the episode's show notes. I was referring to the code that Ryan submitted to Rails Rumble. That's not included in the show notes!
It would be interesting to see other options covered in the future.
Maybe:
https://github.com/jcupitt/ruby-vips
https://github.com/eltiare/carrierwave-vips
can finally help to replace ImageMagick…
Thanks Roland - they look like good options!
Thanks for these links !
I've experienced big memory leaks with RMagick...
Pushing off processing to a background task using DelayedJob seems like a worthy experience!
A good alternative to RMagick if your needs are just resizing: https://github.com/seattlerb/image_science
Ryan, this is not my thumbnail from github, in fact I don't know who this is... could my thumbnail be refreshed?
Thanks, really nice gem
Great screencast. I really like ImageMagick, here are some great examples of some stuff you can do if you're new to the library: http://imagemagick.org/Usage/
There is a slight mistake in the screencast. It should be
xc:red
instead ofcx:red
whencanvas:red
is not working.Does anyone know how to grab just the first frame of an animated gif and save it as a png? I'm having trouble finding this on stackoverflow and blogs.
The goal is to allow the user to upload gifs, but to only use the first frame so that there's not motion all over the page.
Hey Ryan, an important heads-up!
You actually need to do
require "RMagick"
and not
require "rmagick"
This works on Mac OS because of the commonly case-insensitive filesystem, but once you deploy on a linux server, the all small-caps won't function correctly. Took me a while to figure out this discrepancy.
Possible that this is due to my out of date (but unupdateable) rubygems!
There's also Image Sorcery, which allows you to directly use the ImageMagick command line tools.
Nice tutorial.
Running into an issue trying to get this to work. Running Ubuntu 12.1 and can't seem to get rmagick to install due to some dependencies including one I have been searching for a solution on
libmysqlclient16
Any suggestions appreciated.
Thanks
Stuart