RMagick question

Discussion in 'Ruby' started by Joe Van Dyk, Apr 7, 2005.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    Hi,

    I'd like to generate a thumbnail of an image. The thumbnail should
    have curved corners (corners colored a specified color). Any ideas on
    what functions I could use?

    Thanks,
    Joe
    Joe Van Dyk, Apr 7, 2005
    #1
    1. Advertising

  2. Joe Van Dyk wrote:
    > Hi,
    >
    > I'd like to generate a thumbnail of an image. The thumbnail should
    > have curved corners (corners colored a specified color). Any ideas on
    > what functions I could use?
    >
    > Thanks,
    > Joe
    >
    >

    You might try this approach. Start by making a thumbnail from your image
    with #resize or #thumbnail. Use Image.new to construct a second image
    the same size as the thumbnail. Specify the color you want as the
    background color. Use #roundrectangle in the Draw class to draw a
    rounded rectangle on the image. Specify the stroke and fill colors as
    "none" or "transparent". Use #composite with the Magick::OverCompositeOp
    argument to composite this image over the thumbnail.

    Give me a shout if you want and I'll see if I can put together an
    example that does this.

    If you want the corners to be transparent instead of some specific
    color, check out the vignette example in the examples/ subdirectory or
    the "Add transparency with a mask" example in the RMagick portfolio at
    http://rmagick.rubyforge.org.
    Timothy Hunter, Apr 7, 2005
    #2
    1. Advertising

  3. Joe Van Dyk wrote:
    > Hi,
    >
    > I'd like to generate a thumbnail of an image. The thumbnail should
    > have curved corners (corners colored a specified color). Any ideas on
    > what functions I could use?
    >
    > Thanks,
    > Joe
    >
    >

    Here's a script that adds transparent round corners.

    require 'RMagick'
    include Magick

    hat = Image.read('Flower_Hat.jpg').first
    hat.resize!(0.5)

    mask = Image.new(hat.columns, hat.rows) {self.background_color = 'black'}
    gc = Draw.new
    gc.stroke('white').fill('white')
    gc.roundrectangle(0, 0, hat.columns-1, hat.rows-1, 20, 20)
    gc.draw(mask)

    mask.matte = false
    hat.matte = true

    thumb = hat.composite(mask, CenterGravity, CopyOpacityCompositeOp)
    thumb.display
    Timothy Hunter, Apr 8, 2005
    #3
  4. Timothy Hunter()@2005-04-08 08:44:
    > require 'RMagick'
    > include Magick


    Newbie Q ... why does RMagick need a require *and* an include
    statement, I don't that in other packages?

    Steve
    Stephen Birch, Apr 8, 2005
    #4
  5. Stephen Birch wrote:
    > Timothy Hunter()@2005-04-08 08:44:
    >
    >>require 'RMagick'
    >>include Magick

    >
    >
    > Newbie Q ... why does RMagick need a require *and* an include
    > statement, I don't that in other packages?
    >
    > Steve
    >
    >

    The include statement isn't necessary, it's just that I'm a lazy typer.

    All the RMagick classes - Image, Draw, etc. - are enclosed in the Magick
    module. Without the include statement, you would refer to them as
    Magick::Image, Magick::Draw, etc. The include statement mixes the Magick
    module into Object, which adds the Magick constants to Object.
    Thereafter you can refer to these classes without the Magick prefix.

    Including Magick in Object in the general case is a bad idea because it
    could cause namespace conflicts, but for the purposes of an example it's
    okay and saves a few keystrokes.
    Timothy Hunter, Apr 8, 2005
    #5
  6. Stephen Birch <> wrote:
    > Timothy Hunter()@2005-04-08 08:44:
    > > require 'RMagick'
    > > include Magick

    >
    > Newbie Q ... why does RMagick need a require *and* an include
    > statement, I don't that in other packages?


    This mixes in the Magick module, so that you don't need to preface its
    module methods with Magick::

    Compare the following:

    $ irb
    irb(main):001:0> sin(PI)
    NameError: uninitialized constant PI
    from (irb):1
    irb(main):002:0> Math::sin(Math::pI)
    => 1.22464679914735e-16
    irb(main):003:0> include Math
    => Object
    irb(main):004:0> sin(PI)
    => 1.22464679914735e-16

    martin
    Martin DeMello, Apr 8, 2005
    #6
  7. Joe Van Dyk

    Glenn Parker Guest

    Stephen Birch wrote:
    >
    > Newbie Q ... why does RMagick need a require *and* an include
    > statement, I don't that in other packages?


    Check out the recommended usage for the Benchmark module.

    --
    Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoil.com/>
    Glenn Parker, Apr 8, 2005
    #7
  8. Timothy Hunter()@2005-04-08 21:34:
    > The include statement isn't necessary, it's just that I'm a lazy typer.


    Got it ... thanks

    Steve
    Stephen Birch, Apr 8, 2005
    #8
  9. Joe Van Dyk

    Joe Van Dyk Guest

    Thanks for all the responses (check out www.jerrymahan.com for the result).

    Another RMagick question:

    I'm trying to get the width and height (in pixels) of an image. I've
    searched the docs and the closest I can find to what I want is the
    geometry string. But the string is something like
    "<width>34</width><height>88</height>" or something weird like that.
    Isn't there an easier way to get the width/height of an image?

    Thanks,
    Joe

    On Apr 6, 2005 6:06 PM, Joe Van Dyk <> wrote:
    > Hi,
    >
    > I'd like to generate a thumbnail of an image. The thumbnail should
    > have curved corners (corners colored a specified color). Any ideas on
    > what functions I could use?
    >
    > Thanks,
    > Joe
    >
    Joe Van Dyk, Apr 11, 2005
    #9
  10. Joe Van Dyk wrote:
    > Thanks for all the responses (check out www.jerrymahan.com for the result).
    >
    > Another RMagick question:
    >
    > I'm trying to get the width and height (in pixels) of an image. I've
    > searched the docs and the closest I can find to what I want is the
    > geometry string. But the string is something like
    > "<width>34</width><height>88</height>" or something weird like that.
    > Isn't there an easier way to get the width/height of an image?


    Sure. Use the #columns and #rows attributes of the image.

    See http://www.simplesystems.org/RMagick/doc/comtasks.html#info for an
    example.
    Timothy Hunter, Apr 11, 2005
    #10
  11. Joe Van Dyk

    Joe Van Dyk Guest

    On Apr 11, 2005 2:58 PM, Joe Van Dyk <> wrote:
    > Thanks for all the responses (check out www.jerrymahan.com for the result).
    >
    > Another RMagick question:
    >
    > I'm trying to get the width and height (in pixels) of an image. I've
    > searched the docs and the closest I can find to what I want is the
    > geometry string. But the string is something like
    > "<width>34</width><height>88</height>" or something weird like that.
    > Isn't there an easier way to get the width/height of an image?
    >
    > Thanks,
    > Joe
    >


    Aha, I think I'd use rows() and columns() for that.

    Yet another question!

    I have images that have a bunch of different aspect ratios. When I
    generate a thumbnail of those different images, I want all the
    thumbnails to have the same image ratio. Currently, I can only have
    the thumbnails have the same width OR the same height, and not both at
    the same time.

    Any ideas as for the algorithm to do this? Or is there some function
    inside RMagick that does something similar to this?
    Joe Van Dyk, Apr 11, 2005
    #11
  12. Joe Van Dyk

    Joe Van Dyk Guest

    On Apr 11, 2005 3:24 PM, wannes <> wrote:
    > Joe Van Dyk wrote:
    > > Another RMagick question:
    > > I'm trying to get the width and height (in pixels) of an image. I've
    > > searched the docs and the closest I can find to what I want is the
    > > geometry string. But the string is something like
    > > "<width>34</width><height>88</height>" or something weird like that.
    > > Isn't there an easier way to get the width/height of an image?

    >
    > http://studio.imagemagick.org/RMagick/doc/imageattrs.html#rows
    > http://studio.imagemagick.org/RMagick/doc/imageattrs.html#columns
    >
    > didn't test it, but seems logical.
    >
    > grtz,
    > wannes


    Yeah, I finally found it. I was searching for "width" and "height"
    and I didn't think to check "rows" or "columns". :(

    Thanks!
    Joe Van Dyk, Apr 11, 2005
    #12
  13. Joe Van Dyk

    Eric Hodel Guest

    --Apple-Mail-17--340049892
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain; charset=US-ASCII; format=flowed

    On 11 Apr 2005, at 14:58, Joe Van Dyk wrote:

    > Thanks for all the responses (check out www.jerrymahan.com for the
    > result).
    >
    > Another RMagick question:
    >
    > I'm trying to get the width and height (in pixels) of an image. I've
    > searched the docs and the closest I can find to what I want is the
    > geometry string. But the string is something like
    > "<width>34</width><height>88</height>" or something weird like that.
    > Isn't there an easier way to get the width/height of an image?


    #rows and #columns

    --
    Eric Hodel - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

    --Apple-Mail-17--340049892
    content-type: application/pgp-signature; x-mac-type=70674453;
    name=PGP.sig
    content-description: This is a digitally signed message part
    content-disposition: inline; filename=PGP.sig
    content-transfer-encoding: 7bit

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (Darwin)

    iEYEARECAAYFAkJa+oIACgkQMypVHHlsnwSA0QCeK/6NZclCtEGaEH3u3GiJxBu8
    PagAoNgfPCTZoOyu/wtyPCPuhtF20NU4
    =UGhe
    -----END PGP SIGNATURE-----

    --Apple-Mail-17--340049892--
    Eric Hodel, Apr 11, 2005
    #13
  14. Joe Van Dyk wrote:
    > I have images that have a bunch of different aspect ratios. When I
    > generate a thumbnail of those different images, I want all the
    > thumbnails to have the same image ratio. Currently, I can only have
    > the thumbnails have the same width OR the same height, and not both at
    > the same time.
    >
    > Any ideas as for the algorithm to do this? Or is there some function
    > inside RMagick that does something similar to this?
    >

    Do you mean you want to resize an image to a specific size without
    retaining its current aspect ratio? Just specify the desired new width
    and height when you call #resize:
    http://www.simplesystems.org/RMagick/doc/image3.html#resize


    In general you can use the #change_geometry method to compute a new
    image geometry. Here's a couple of links:

    http://www.simplesystems.org/RMagick/doc/comtasks.html#resizing
    http://www.simplesystems.org/RMagick/doc/image1.html#change_geometry
    Timothy Hunter, Apr 12, 2005
    #14
  15. Joe Van Dyk

    Joe Van Dyk Guest

    On Apr 11, 2005 4:04 PM, Timothy Hunter <> wrote:
    > Joe Van Dyk wrote:
    > > I have images that have a bunch of different aspect ratios. When I
    > > generate a thumbnail of those different images, I want all the
    > > thumbnails to have the same image ratio. Currently, I can only have
    > > the thumbnails have the same width OR the same height, and not both at
    > > the same time.
    > >
    > > Any ideas as for the algorithm to do this? Or is there some function
    > > inside RMagick that does something similar to this?
    > >

    > Do you mean you want to resize an image to a specific size without
    > retaining its current aspect ratio? Just specify the desired new width
    > and height when you call #resize:
    > http://www.simplesystems.org/RMagick/doc/image3.html#resize
    >
    > In general you can use the #change_geometry method to compute a new
    > image geometry. Here's a couple of links:
    >
    > http://www.simplesystems.org/RMagick/doc/comtasks.html#resizing
    > http://www.simplesystems.org/RMagick/doc/image1.html#change_geometry


    No, I want to resize an image to a specific size and maintain its
    current aspect ratio. This will probably involve cropping, I'd guess.

    Here's my first attempt (psuedo untested code)

    thumbnail_width = 100.0
    thumbnail_height = 75.0
    thumbnail_aspect_ratio = thumbnail_width / thumbnail_height

    image = RMagick::Image.new( whatever in here )
    image_aspect_ratio = image.rows.to_f / image.cols.to_f

    if thumbnail_aspect_ratio > image_aspect_ratio
    # Image is too wide, geometry string should restrict height
    geometry_string = "x#{thumbnail_height}"
    else
    # Image is either just right or too tall, geometry string should
    # restrict width
    geometry_string = "#{thumbnail_width}"
    end

    thumbnail_image = image.change_geometry(geometry_string) { ... }

    # Crop image to the thumbnail's width and height
    thumbnail_image.crop(RMagick::CENTER_GRAVITY,
    thumbnail_width, thumbnail_height)



    That should work, right? (don't have access to RMagick here yet..)
    Joe Van Dyk, Apr 12, 2005
    #15
  16. Joe Van Dyk wrote:
    > No, I want to resize an image to a specific size and maintain its
    > current aspect ratio. This will probably involve cropping, I'd guess.


    Yes, in that case cropping is called for.
    Timothy Hunter, Apr 12, 2005
    #16
  17. Joe Van Dyk

    Chris Pine Guest

    What we do is shrink it down to fit in the thumbnail size, and fill in
    the rest with white. No cropping, but you get a lot of white
    sometimes:

    if deep || !FileTest.exist?(smallFileName) ||
    File.mtime(filename) >= File.mtime(smallFileName)
    image = Magick::ImageList.new(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)

    if isMovie filename
    playArrow = Magick::Draw.new
    playArrow.stroke('black').stroke_width(2).fill('lime')
    playArrow.polygon(7,7, 27,17, 7,27)
    playArrow.draw image
    end

    image.write(smallFileName) { self.quality = 85 }
    end

    Chris
    Chris Pine, Apr 12, 2005
    #17
    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. Tim Hunter

    [ANN] RMagick moves to RubyForge

    Tim Hunter, Jul 27, 2003, in forum: Ruby
    Replies:
    0
    Views:
    100
    Tim Hunter
    Jul 27, 2003
  2. Joe Van Dyk

    rmagick question

    Joe Van Dyk, Aug 26, 2005, in forum: Ruby
    Replies:
    16
    Views:
    255
    Timothy Hunter
    Sep 1, 2005
  3. Raymond Brigleb

    RMagick Thumbnailing Question

    Raymond Brigleb, Sep 8, 2005, in forum: Ruby
    Replies:
    2
    Views:
    119
  4. shawn bright

    another RMagick and rotation question

    shawn bright, Feb 13, 2008, in forum: Ruby
    Replies:
    4
    Views:
    131
    shawn bright
    Feb 14, 2008
  5. Pito Salas
    Replies:
    1
    Views:
    137
    Tim Hunter
    Apr 24, 2009
Loading...

Share This Page