Programatically resizing photos

I find that Italy is a target-rich environment for taking pictures and
I'm taking lots. I need to resize the pics (make them smaller, like 320X240)
so I can put them on the web. Doing this by hand with iPhoto is a bit of
a pain. Can anyone suggest a way of doing this using a Ruby script?

Thanks.

Phil

Phil Tomson wrote:

I find that Italy is a target-rich environment for taking pictures and I'm taking lots. I need to resize the pics (make them smaller, like 320X240) so I can put them on the web. Doing this by hand with iPhoto is a bit of a pain. Can anyone suggest a way of doing this using a Ruby script?

Thanks.

Phil

As you're using a Macintosh you could look at sips, which is installed by default - just type sips --help, which allows all sorts of image manipulation such as --resampleHeightWidth pixelsH pixelsW.

ptkwt@aracnet.com (Phil Tomson) wrote in
news:cq642408rh@enews3.newsguy.com:

Can anyone suggest a way of doing this using a Ruby script?

If you don't insist on doing it with a Ruby script, I guess something like
this using ImageMagick should do the trick:

      convert -resize 320x200 image.jpg image-out.jpg

And since this is available from Ruby using Tim Hunters RMagick, you could
also (off the top of my head):
      
      require 'rmagick'
      
      img = Magick::Image.read( 'image.jpg' ).first
      img.change_geometry!( '320x200' ) do |cols, rows, img|
            img.resize!( cols, rows )
      end

      img.write( 'img-out.jpg' )

happy hacking !
kaspar

hand manufactured code - www.tua.ch/ruby

You mention iPhoto, so I'll take that as a sign that you are a Mac OS X
user. But in case ruby-gtk2 and RMagick can be loaded on such a system
or in case others may be interested...

I have been working on a little application I call WebGal--a gtk2-based
GUI tool for creating web galleries. WebGal has functions for adding and
removing images from a gallery, rotating source image files, titling,
dating, captioning, and making notes on images. Right now it's still
getting its legs, but it does the basic job just fine (standard caveats
related to development software apply).

Using extremely simple templates and CSS, WebGal will create all the
necessary files so that you have a working HTML web gallery, complete
with a thumbnail index page and easy navigate from one image to the next
within the gallery.

Settings are available to indicate the size of thumbnails and final
display images.

If you're interested it's at http://www.andsoforth.com/geek/WebGal.html

-Michael C. Libby, www.andsoforth.com

···

On Mon, 2004-12-20 at 18:22 +0900, Phil Tomson wrote:

I find that Italy is a target-rich environment for taking pictures and
I'm taking lots. I need to resize the pics (make them smaller, like 320X240)
so I can put them on the web. Doing this by hand with iPhoto is a bit of
a pain. Can anyone suggest a way of doing this using a Ruby script?

Phil,

I'm working on some RMagick-based web gallery code right now, as I know
several people (myself included) who want to set up 'photo-a-day'
blogs. Unfortunately, with all the holiday craziness, I haven't had
time to do much more than tinker with it, but the basic code to do
resizing is pretty straightforward.

Here's a snippet from the work-in-progress:

···

---
require 'RMagick'

THUMB_MAX_X = 120
THUMB_MAX_Y = 90

images = Dir['images/*']

images.each do |imgfile|
image = Magick::Image.read(imgfile).first
xscale = THUMB_MAX_X.to_f / image.columns
yscale = THUMB_MAX_Y.to_f / image.rows
factor = (xscale < yscale) ? xscale : yscale
thumb = image.scale(factor)
outfile = 'thumbs/'+File.basename(imgfile)
thumb.write(outfile)
end

---

There's really not much to it aside from that, aside from uploads
(Net::SFTP), templating (ERb), and metadata (EXIF+YAML+???).

Lennon

I found the later version of iPhoto to be better for this, but the
options were not necessarily where expected.

Unfortunately, I'm away from my Mac for the holidays, so I can't double check.

Nick

···

On Mon, 20 Dec 2004 18:22:07 +0900, Phil Tomson <ptkwt@aracnet.com> wrote:

I find that Italy is a target-rich environment for taking pictures and
I'm taking lots. I need to resize the pics (make them smaller, like 320X240)
so I can put them on the web. Doing this by hand with iPhoto is a bit of
a pain. Can anyone suggest a way of doing this using a Ruby script?

Thanks.

Phil

--
Nicholas Van Weerdenburg

Hey, Phil,

I use RMagick and have been very happy with it. Here's a snippet from the program that generates our family picture website. The "640" code just scales the picture to make sure it fits inside a 640x640 box (so one dimension is == 640 and the other is <= 640), and the "thumb" code makes sure the new image is 160x120, filling with white any area not covered by a photo of a different aspect ratio. (Since there are many thumbnails on a page, I wanted them to be all the same size.) You're welcome to check out our site (http://pine.fm), and tell me if you want any more of the code.

···

-----------

def makeImage (fileName, size)
   if !FileTest.exist?('pictures/'+fileName)
     # TODO: Error report?
     return 'Cannot find picture "'+fileName+'".'
   end

   altText = if PICTURES[fileName]
     PICTURES[fileName][:desc]
   else
     'This picture needs info.'
   end

   case size

   when 640
     #
     # Make 640x640 image
     #
     smallFileName = 'pictures_640/640__' + fileName

     if !FileTest.exist?(smallFileName)
       image = Magick::ImageList.new('pictures/'+fileName)
       image = image.change_geometry('640x640') do |cols, rows, img|
         img.resize!(cols, rows)
       end
       image.write(smallFileName) { self.quality = 85 }
     end

     #img(:src=>smallFileName)
     '<img src="'+smallFileName+'" />'

   when :thumb
     #
     # Make thumbnail image
     #
     smallFileName = 'pictures_thumbs/thumb__' + fileName

     if !FileTest.exist?(smallFileName)
       image = Magick::ImageList.new('pictures/'+fileName)
       thumb = nil
       image.change_geometry('160x120') do |cols, rows, img|
         thumb = img.resize(cols, rows)
       end
       white_bg = Magick::Image.new(160, 120)
       image = white_bg.composite(thumb, Magick::CenterGravity,
                                         Magick::OverCompositeOp)
       image.write(smallFileName) { self.quality = 85 }
     end

     #img(:src=>smallFileName)
     '<img width="160" height="120" src="'+smallFileName+'" />'

   else
     raise 'In "makeImage()"... Bad Size: '+size.to_s
   end
end

I use RMagick for my patch to instiki that resizes photos for display
as thumbnails. Its API is really spiffy, easy to use.

···

On Tue, 21 Dec 2004 03:59:09 +0900, Michael C. Libby <mcl-ruby-talk@andsoforth.com> wrote:

On Mon, 2004-12-20 at 18:22 +0900, Phil Tomson wrote:
> I find that Italy is a target-rich environment for taking pictures and
> I'm taking lots. I need to resize the pics (make them smaller, like 320X240)
> so I can put them on the web. Doing this by hand with iPhoto is a bit of
> a pain. Can anyone suggest a way of doing this using a Ruby script?

"rcoder" <rcoder@gmail.com> wrote in news:1103575653.957990.69480
@c13g2000cwb.googlegroups.com:

xscale = THUMB_MAX_X.to_f / image.columns
yscale = THUMB_MAX_Y.to_f / image.rows
factor = (xscale < yscale) ? xscale : yscale
thumb = image.scale(factor)

AFAIK, the change_geometry method does all of this and then some... May I
suggest using that method instead ?

kaspar

hand manufactured code - www.tua.ch/ruby