Image conversion ...

U

Useko Netsumi

Hi, is there any Ruby code snippets I can use to transform my photo to lower
resolution, uniform size but maintaining aspect ration, etc ...?

Thanks
 
A

Ara.T.Howard

Date: Tue, 03 Feb 2004 12:30:39 -0500
From: Tim Hunter <[email protected]>
Newsgroups: comp.lang.ruby
Subject: Re: Image conversion ...



RMagick (http://raa.ruby-lang.org/list.rhtml?name=rmagick) has this
functionality, but it's way more than a code snippet.

if you have image magick installed you can also simply

mogrify -size 50x50 image.jpg

do

man mogrify

-a

--

ATTN: please update your address books with address below!

===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP :: http://www.ngdc.noaa.gov/stp/
| NGDC :: http://www.ngdc.noaa.gov/
| NESDIS :: http://www.nesdis.noaa.gov/
| NOAA :: http://www.noaa.gov/
| US DOC :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.
| Art is everything else.
| -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
===============================================================================
 
O

Osuka Adartse

Useko said:
Hi, is there any Ruby code snippets I can use to transform my photo to lower
resolution, uniform size but maintaining aspect ration, etc ...?

Thanks
I use GD and this lil' code, it keeps aspect ratio and ONLY works on jpg
and png, since you say you use php, this could look familiar, the code
could be *better* but does what I want use it or take it as reference,
usage of copyResampled would give better output but haven't been able to
use it.

*warning nuby code ahead*

require "GD"
class Thumber
#imgpath=Full path to src image i.e.
"/home/osuka/pics/good_looking_me.jpg"
#thumb_dir=dir path ro dest directory i.e. "/home/osuka/backups/"
*notice* trailing /
#newx=new image width i.e. 320
#newy=new image height i.e. 240
#prefix=prefix to add to given image i.e. "thumb_"
#type=image type of uotput thumb i.e. "jpg"|png|gif <-depends
on GD image write support this particular code
#uses only jpg and png, haven't need anything else, or in gif case
can modified it to use an external app like Magick.
#or gif2png
def initialize(imgpath,thumb_dir,newx,newy,prefix,type)
@prefix=prefix
@imgpath=imgpath
@thumb_dir=thumb_dir
@type=type

if(@imgpath.downcase=~/[a-zA-Z0-9]+.(jpg||jpeg)+/)
#comment out if noisy
puts @srctype="jpeg"
@srcimg=GD::Image.new_from_jpeg(imgpath)
@[email protected]_f/@srcimg.height
elsif(@imgpath.downcase=~/[a-zA-Z0-9]+.(png)+/)
#comment out if noisy
puts @srctype="png"
@srcimg=GD::Image.new_from_png(imgpath)
@[email protected]_f/@srcimg.height
elsif(@imgpath.downcase=~/[a-zA-Z0-9]+.(gif)+/)
#comment out if noisy
puts @srctype="gif"
@srcimg=GD::Image.new_from_gif(imgpath)
@[email protected]_f/@srcimg.height
else
#comment out if noisy
puts "other"
end
@newx=newx
@newy=newy
#comment out if noisy
puts @ratio
if(@newy>@newx)
@newy=(@newx/@ratio).round
else
puts @newx=(@newy*@ratio).round
end

if(File.exists?(@thumb_dir+@prefix+File.basename(@imgpath)))
#comment out if noisy
puts "file exist"
else
#comment out if noisy
puts "file doesn't exist"
@dstimg=GD::Image.new(@newx,@newy)

@srcimg.copyResized(@dstimg,0,0,0,0,@newx,@newy,@srcimg.width,@srcimg.height)
@thumbf=File.new(@thumb_dir+@prefix+File.basename(@imgpath),"wb")
if(@type=="jpg")
@dstimg.jpeg(@thumbf,85)
elsif(@type=="png")
@dstimg.png(@thumbf)
else(@type=="gif")
#@dstimg.gif(thumbf,85)
end
end

end
end

#example usage:
#img1="e:/adartse/DNA.JPG"
#mypic=Thumber.new(img1,"e:/temp/",80,100,"","jpg")
 
R

Ruby Tuesday

Hi Osuka, I will try what you suggested.

Thank you very much for your help.

Osuka Adartse said:
Useko said:
Hi, is there any Ruby code snippets I can use to transform my photo to lower
resolution, uniform size but maintaining aspect ration, etc ...?

Thanks
I use GD and this lil' code, it keeps aspect ratio and ONLY works on jpg
and png, since you say you use php, this could look familiar, the code
could be *better* but does what I want use it or take it as reference,
usage of copyResampled would give better output but haven't been able to
use it.

*warning nuby code ahead*

require "GD"
class Thumber
#imgpath=Full path to src image i.e.
"/home/osuka/pics/good_looking_me.jpg"
#thumb_dir=dir path ro dest directory i.e. "/home/osuka/backups/"
*notice* trailing /
#newx=new image width i.e. 320
#newy=new image height i.e. 240
#prefix=prefix to add to given image i.e. "thumb_"
#type=image type of uotput thumb i.e. "jpg"|png|gif <-depends
on GD image write support this particular code
#uses only jpg and png, haven't need anything else, or in gif case
can modified it to use an external app like Magick.
#or gif2png
def initialize(imgpath,thumb_dir,newx,newy,prefix,type)
@prefix=prefix
@imgpath=imgpath
@thumb_dir=thumb_dir
@type=type

if(@imgpath.downcase=~/[a-zA-Z0-9]+.(jpg||jpeg)+/)
#comment out if noisy
puts @srctype="jpeg"
@srcimg=GD::Image.new_from_jpeg(imgpath)
@[email protected]_f/@srcimg.height
elsif(@imgpath.downcase=~/[a-zA-Z0-9]+.(png)+/)
#comment out if noisy
puts @srctype="png"
@srcimg=GD::Image.new_from_png(imgpath)
@[email protected]_f/@srcimg.height
elsif(@imgpath.downcase=~/[a-zA-Z0-9]+.(gif)+/)
#comment out if noisy
puts @srctype="gif"
@srcimg=GD::Image.new_from_gif(imgpath)
@[email protected]_f/@srcimg.height
else
#comment out if noisy
puts "other"
end
@newx=newx
@newy=newy
#comment out if noisy
puts @ratio
if(@newy>@newx)
@newy=(@newx/@ratio).round
else
puts @newx=(@newy*@ratio).round
end

if(File.exists?(@thumb_dir+@prefix+File.basename(@imgpath)))
#comment out if noisy
puts "file exist"
else
#comment out if noisy
puts "file doesn't exist"
@dstimg=GD::Image.new(@newx,@newy)

@srcimg.copyResized(@dstimg,0,0,0,0,@newx,@newy,@srcimg.width,@srcimg.height
)
@thumbf=File.new(@thumb_dir+@prefix+File.basename(@imgpath),"wb")
if(@type=="jpg")
@dstimg.jpeg(@thumbf,85)
elsif(@type=="png")
@dstimg.png(@thumbf)
else(@type=="gif")
#@dstimg.gif(thumbf,85)
end
end

end
end

#example usage:
#img1="e:/adartse/DNA.JPG"
#mypic=Thumber.new(img1,"e:/temp/",80,100,"","jpg")
 
U

Useko Netsumi

Thanks Ara, I'll get Rmagick and try them out.

Ara.T.Howard said:
Date: Tue, 03 Feb 2004 12:30:39 -0500
From: Tim Hunter <[email protected]>
Newsgroups: comp.lang.ruby
Subject: Re: Image conversion ...



RMagick (http://raa.ruby-lang.org/list.rhtml?name=rmagick) has this
functionality, but it's way more than a code snippet.

if you have image magick installed you can also simply

mogrify -size 50x50 image.jpg

do

man mogrify

-a

--

ATTN: please update your address books with address below!

============================================================================
===
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP :: http://www.ngdc.noaa.gov/stp/
| NGDC :: http://www.ngdc.noaa.gov/
| NESDIS :: http://www.nesdis.noaa.gov/
| NOAA :: http://www.noaa.gov/
| US DOC :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.
| Art is everything else.
| -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'============================================================================
===
 
T

Tim Hunter

Thanks Ara, I'll get Rmagick and try them out.

Using RMagick, the Ruby equivalent to this command is:

require 'RMagick'
include Magick

img = Image.read("image.jpg").first
img.change_geometry("50x50") { |cols, rows|
img.resize!(cols, rows)
}
img.write("image.jpg")
 
U

Useko Netsumi

Do they have the mswin32 version of RMagick? They mention to have the cygwin
version but how do I mix and match between mswin32 ruby and cygwin RMagick?

I do have a development Linux server in the same machine with my Windows
Development. I have to use the windows for my day jobs and use it
exclusively. Perhaps if there are the mswin32 version of all Ruby related
apps would be convenience for beginer such as myself.

Thanks
 
S

Sascha D?rdelmann

Useko Netsumi said:
Hi, is there any Ruby code snippets I can use to transform my photo to lower
resolution, uniform size but maintaining aspect ration, etc ...?

Besides other solutions you could use the command line tool or the
library behind xnview
http://perso.wanadoo.fr/pierre.g/xnview/enhome.html. It's easy to
install and easy to use via Ruby scripts.

Simple things can be done by fox but I wouldn't recommend it for batch
image processing.

Cheers
Sascha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top