RMagick question

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

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

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 <joevandyk@gmail.com> 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

Timothy Hunter(cyclists@nc.rr.com)@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

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?

···

On Apr 11, 2005 2:58 PM, Joe Van Dyk <joevandyk@gmail.com> 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

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

#rows and #columns

PGP.sig (194 Bytes)

···

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?

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

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 RMagick 1.15.0: Common Tasks for an example.

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

···

Stephen Birch <sgbirch@imsmail.org> wrote:

Timothy Hunter(cyclists@nc.rr.com)@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?

Stephen Birch wrote:

Timothy Hunter(cyclists@nc.rr.com)@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.

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/&gt;

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

Thanks!

···

On Apr 11, 2005 3:24 PM, wannes <wannes@oeaniz.be> 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

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(cyclists@nc.rr.com)@2005-04-08 21:34:

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

Got it ... thanks

Steve

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

···

On Apr 11, 2005 4:04 PM, Timothy Hunter <cyclists@nc.rr.com> 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:
RMagick 1.15.0: class Image (instance methods, part 3)

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

RMagick 1.15.0: Common Tasks
RMagick 1.15.0: class Image (class and instance methods, part 1)

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.

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