Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > RMagick question

Reply
Thread Tools

RMagick question

 
 
Joe Van Dyk
Guest
Posts: n/a
 
      04-07-2005
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


 
Reply With Quote
 
 
 
 
Timothy Hunter
Guest
Posts: n/a
 
      04-07-2005
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.
 
Reply With Quote
 
 
 
 
Timothy Hunter
Guest
Posts: n/a
 
      04-07-2005
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

 
Reply With Quote
 
Stephen Birch
Guest
Posts: n/a
 
      04-08-2005
Timothy Hunter((E-Mail Removed))@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


 
Reply With Quote
 
Timothy Hunter
Guest
Posts: n/a
 
      04-08-2005
Stephen Birch wrote:
> Timothy Hunter((E-Mail Removed))@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:raw, 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.
 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      04-08-2005
Stephen Birch <(E-Mail Removed)> wrote:
> Timothy Hunter((E-Mail Removed))@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:I)
=> 1.22464679914735e-16
irb(main):003:0> include Math
=> Object
irb(main):004:0> sin(PI)
=> 1.22464679914735e-16

martin
 
Reply With Quote
 
Glenn Parker
Guest
Posts: n/a
 
      04-08-2005
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/>


 
Reply With Quote
 
Stephen Birch
Guest
Posts: n/a
 
      04-08-2005
Timothy Hunter((E-Mail Removed))@2005-04-08 21:34:
> The include statement isn't necessary, it's just that I'm a lazy typer.


Got it ... thanks

Steve


 
Reply With Quote
 
Joe Van Dyk
Guest
Posts: n/a
 
      04-11-2005
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 <(E-Mail Removed)> 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
>



 
Reply With Quote
 
Timothy Hunter
Guest
Posts: n/a
 
      04-11-2005
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...asks.html#info for an
example.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between gem install rmagick and port rb-rmagick Pito Salas Ruby 1 04-24-2009 09:29 PM
RMagick error: " no such file to load -- RMagick" jeljer te Wies Ruby 2 03-23-2009 12:23 AM
another RMagick and rotation question shawn bright Ruby 4 02-14-2008 02:26 AM
RMagick Thumbnailing Question Raymond Brigleb Ruby 2 09-08-2005 06:01 PM
rmagick question Joe Van Dyk Ruby 16 09-01-2005 10:30 PM



Advertisments