RMagick question

J

Joe Van Dyk

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
 
T

Timothy Hunter

Joe said:
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.
 
T

Timothy Hunter

Joe said:
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
 
T

Timothy Hunter

Stephen said:
Timothy Hunter([email protected])@2005-04-08 08:44:



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

Martin DeMello

Stephen Birch said:
Timothy Hunter([email protected])@2005-04-08 08:44:

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
 
G

Glenn Parker

Stephen said:
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.
 
J

Joe Van Dyk

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
 
T

Timothy Hunter

Joe said:
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.
 
J

Joe Van Dyk

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?
 
E

Eric Hodel

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

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 - (e-mail address removed) - 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--
 
T

Timothy Hunter

Joe said:
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
 
J

Joe Van Dyk

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

Timothy Hunter

Joe said:
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.
 
C

Chris Pine

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
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top