Image conversion


(Useko Netsumi) #1

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


(Thomas Adam) #2

— Useko Netsumi usenets_remote_this@earthlink.net wrote:

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

As far as I know, Ruby has bindings to imagemagick, but I would personally
use the shell:

man convert

(from imagemagick)

– Thomas Adam

···

=====
“The Linux Weekend Mechanic” – http://linuxgazette.net
"TAG Editor" – http://linuxgazette.net

" We’ll just save up your sins, Thomas, and punish
you for all of them at once when you get better. The
experience will probably kill you. :)"

– Benjamin A. Okopnik (Linux Gazette Technical Editor)


BT Yahoo! Broadband - Free modem offer, sign up online today and save £80 http://btyahoo.yahoo.co.uk


(Carlos) #3

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

The documentation of RMagick has a lot of examples.

http://rmagick.rubyforge.org/


(Tim Hunter) #4

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

···

On Tue, 3 Feb 2004 08:47:16 -0500, “Useko Netsumi” usenets_remote_this@earthlink.net wrote:

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


(Osuka Adartse) #5

Useko Netsumi wrote:

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)
   @ratio=@srcimg.width.to_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)
   @ratio=@srcimg.width.to_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)
   @ratio=@srcimg.width.to_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”)


(matthew c. mead) #6

I would start with this:
http://rmagick.rubyforge.org/

Having used JMagick to build an HtmlGallery generation app, I may
rewrite in ruby to build a gtk gui into it.

Shameless plug:

http://www.goof.com/~mmead/HtmlGallery/

-matt

···

On Mon, Feb 09, 2004 at 06:28:23AM +0900, Useko Netsumi wrote:

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


matthew c. mead

http://www.goof.com/~mmead/


#7

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

···

“Useko Netsumi” usenets_remote_this@earthlink.net wrote:

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


(paul vudmaska) #8

THAT, is just the coolest! Done thumbs with php…but
i had no idea you could with ruby! Thanks for the link!

···

— Carlos angus@quovadis.com.ar wrote:

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

The documentation of RMagick has a lot of examples.

http://rmagick.rubyforge.org/


Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!


(Ara.T.Howard) #9

if you have image magick installed you can also simply

mogrify -size 50x50 image.jpg

do

man mogrify

-a

···

On Tue, 3 Feb 2004, Tim Hunter wrote:

Date: Tue, 03 Feb 2004 12:30:39 -0500
From: Tim Hunter Tim.Hunter@sas.com
Newsgroups: comp.lang.ruby
Subject: Re: Image conversion …

On Tue, 3 Feb 2004 08:47:16 -0500, “Useko Netsumi” > usenets_remote_this@earthlink.net wrote:

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

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

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’
===============================================================================


(Ruby Tuesday) #10

Hi Osuka, I will try what you suggested.

Thank you very much for your help.

“Osuka Adartse” rocioestradacastaneda@prodigy.net.mx wrote in message
news:40201B0B.9090905@prodigy.net.mx

Useko Netsumi wrote:

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)
   @ratio=@srcimg.width.to_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)
   @ratio=@srcimg.width.to_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)
   @ratio=@srcimg.width.to_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”)


(Useko Netsumi) #11

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

“matthew c. mead” mmead@goof.com wrote in message
news:20040208220720.GA98015@goof.com

I would start with this:
http://rmagick.rubyforge.org/

Having used JMagick to build an HtmlGallery generation app, I may
rewrite in ruby to build a gtk gui into it.

Shameless plug:

http://www.goof.com/~mmead/HtmlGallery/

-matt

Hi, is there any Ruby code snippets I can use to transform my photo to
lower

···

On Mon, Feb 09, 2004 at 06:28:23AM +0900, Useko Netsumi wrote:

resolution, uniform size but maintaining aspect ration, etc …?

Thanks


matthew c. mead

http://www.goof.com/~mmead/


(Useko Netsumi) #12

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

“Ara.T.Howard” ahoward@fattire.ngdc.noaa.gov wrote in message
news:Pine.LNX.4.44.0402031119350.29981-100000@fattire.ngdc.noaa.gov

Date: Tue, 03 Feb 2004 12:30:39 -0500
From: Tim Hunter Tim.Hunter@sas.com
Newsgroups: comp.lang.ruby
Subject: Re: Image conversion …

Hi, is there any Ruby code snippets I can use to transform my photo to
lower

···

On Tue, 3 Feb 2004, Tim Hunter wrote:

On Tue, 3 Feb 2004 08:47:16 -0500, “Useko Netsumi” > > usenets_remote_this@earthlink.net wrote:

resolution, uniform size but maintaining aspect ration, etc …?

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’

============================================================================


(Sam Roberts) #13

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

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

http://www.simplesystems.org/RMagick/doc/comtasks.html#thumb

img = Image.new "bigimage.gif"
thumb = img.scale(125, 125)
thumb.write “thumb.gif”

I am truly impressed, I had no idea it was this easy!

Sam


(Tim Hunter) #14

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”)

···

On Tue, 03 Feb 2004 18:57:21 -0500, Useko Netsumi wrote:

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

“Ara.T.Howard” ahoward@fattire.ngdc.noaa.gov wrote in message
news:Pine.LNX.4.44.0402031119350.29981-100000@fattire.ngdc.noaa.gov

On Tue, 3 Feb 2004, Tim Hunter wrote:

if you have image magick installed you can also simply

mogrify -size 50x50 image.jpg