Image conversion ...

Discussion in 'Ruby' started by Useko Netsumi, Feb 3, 2004.

  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
     
    Useko Netsumi, Feb 3, 2004
    #1
    1. Advertising

  2. Useko Netsumi

    Tim Hunter Guest

    On Tue, 3 Feb 2004 08:47:16 -0500, "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 ...?


    RMagick (http://raa.ruby-lang.org/list.rhtml?name=rmagick) has this
    functionality, but it's way more than a code snippet.
     
    Tim Hunter, Feb 3, 2004
    #2
    1. Advertising

  3. Useko Netsumi

    Carlos Guest

    > 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/
     
    Carlos, Feb 3, 2004
    #3
  4. Useko Netsumi

    Ara.T.Howard Guest

    On Tue, 3 Feb 2004, Tim Hunter wrote:

    > Date: Tue, 03 Feb 2004 12:30:39 -0500
    > From: Tim Hunter <>
    > Newsgroups: comp.lang.ruby
    > Subject: Re: Image conversion ...
    >
    > On Tue, 3 Feb 2004 08:47:16 -0500, "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 ...?

    >
    > 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'
    ===============================================================================
     
    Ara.T.Howard, Feb 3, 2004
    #4
  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")
     
    Osuka Adartse, Feb 3, 2004
    #5
  6. Useko Netsumi

    Ruby Tuesday Guest

    Hi Osuka, I will try what you suggested.

    Thank you very much for your help.

    "Osuka Adartse" <> wrote in message
    news:...
    > 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")
    >
    >
    >
    >
     
    Ruby Tuesday, Feb 3, 2004
    #6
  7. Thanks Ara, I'll get Rmagick and try them out.

    "Ara.T.Howard" <> wrote in message
    news:p...
    > On Tue, 3 Feb 2004, Tim Hunter wrote:
    >
    > > Date: Tue, 03 Feb 2004 12:30:39 -0500
    > > From: Tim Hunter <>
    > > Newsgroups: comp.lang.ruby
    > > Subject: Re: Image conversion ...
    > >
    > > On Tue, 3 Feb 2004 08:47:16 -0500, "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 ...?

    > >
    > > 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'
    >

    ============================================================================
    ===
    >
     
    Useko Netsumi, Feb 3, 2004
    #7
  8. Useko Netsumi

    Tim Hunter Guest

    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" <> wrote in message
    > news:p...
    >> On Tue, 3 Feb 2004, Tim Hunter wrote:
    >>
    >> if you have image magick installed you can also simply
    >>
    >> mogrify -size 50x50 image.jpg
    >>


    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")
     
    Tim Hunter, Feb 4, 2004
    #8
  9. 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/
     
    matthew c. mead, Feb 9, 2004
    #9
  10. 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" <> wrote in message
    news:...
    > 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/
    >
    >
     
    Useko Netsumi, Feb 10, 2004
    #10
  11. "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 ...?


    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
     
    Sascha D?rdelmann, Feb 10, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGVhc3Vu?=

    Conversion issue: Image>Byte array>Image

    =?Utf-8?B?RGVhc3Vu?=, Jul 14, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    923
    =?Utf-8?B?RGVhc3Vu?=
    Jul 14, 2005
  2. surendra
    Replies:
    1
    Views:
    8,504
    dipakPardesi
    Jun 21, 2008
  3. Roland
    Replies:
    0
    Views:
    1,095
    Roland
    May 12, 2004
  4. Laszlo Zsolt Nagy
    Replies:
    1
    Views:
    1,431
    Kartic
    Jan 26, 2005
  5. , India
    Replies:
    2
    Views:
    506
    Fraser Ross
    Sep 15, 2009
Loading...

Share This Page