Transparent Image Using Rmagick

R

Radu Ciocan

Hello,

I use rmagick to create a new image.
How can I make this image with 50% transparency?

Thanks
 
R

Radu Ciocan

I found here an example:
http://blog.corunet.com/english/the-definitive-heatmap
it works, but it use external ImageMagick convert program

@opacity = "0.50"
@path = './'
transparency = "convert "<<@path<<'image.png -channel A -fx
"A*'<<@opacity<<'" '<<@path<<'image_transparent.png'
system(transparency)

How can I transform this in ruby rmagick code?
 
T

Tim Hunter

Radu said:
Hello,

I use rmagick to create a new image.
How can I make this image with 50% transparency?

Thanks
If you just want a solid-color image that's 50% transparent, specify the
color and transparency when you create the image:

img = Magick::Image.new(columns. rows) {self.background_color =
Magick::pixel.new(rr, gg, bb, Magick::MaxRGB/2)}

where columns and rows specify the size of the image, and rr, gg, bb
specify the red, green, and blue channels.

If you have an image already and you want to change the opacity, use the
#opacity attribute:

img.opacity = Magick::MaxRGB / 2

http://www.simplesystems.org/RMagick/doc/imageattrs.html#opacity
http://www.simplesystems.org/RMagick/doc/struct.html#Pixel
http://www.simplesystems.org/RMagick/doc/image1.html#new
 
R

Radu Ciocan

Thanks Tim for your reply.

My image is not with one solid color.
I have already an image to change the opacity.

Unfortunately, I tried
img.opacity = Magick::MaxRGB / 2
but it doesn't working

I tried also with background_color = 'none'.

Any clues?

Thanks
 
R

rmagick

Thanks Tim for your reply.

My image is not with one solid color.
I have already an image to change the opacity.

Unfortunately, I tried
img.opacity = Magick::MaxRGB / 2
but it doesn't working

I tried also with background_color = 'none'.

What makes you think it doesn't work? How are you viewing the image?
What kind of image is it? A GIF? A JPEG?
 
R

Radu Ciocan

Tim said:
What makes you think it doesn't work? How are you viewing the image?
What kind of image is it? A GIF? A JPEG?

It's a png image.
The image generated with convert has opacity 50% (i viewed with acdsee)
The image generated with rmagick doesn't have opacity.
 
R

rmagick

It's a png image.
The image generated with convert has opacity 50% (i viewed with acdsee)
The image generated with rmagick doesn't have opacity.

Send the image and a script that reproduces the problem to rmagick AT
rubyforge DOT org and I'll take a look at it.
 
R

Radu Ciocan

Tim said:
Send the image and a script that reproduces the problem to rmagick AT
rubyforge DOT org and I'll take a look at it.

Here is an example of script:

require 'RMagick'
image = Magick::Image.read('public/images/rails.png').first
image.opacity = Magick::MaxRGB / 2
image.write("public/images/rails_with_transparency.png")

(rails.png is the default rails logo from a new rails app)
theoretically, this should be working, but is not working,
the new image rails_with_transparency.png is the same as rails.png
 
T

Tim Hunter

Radu said:
Here is an example of script:

require 'RMagick'
image = Magick::Image.read('public/images/rails.png').first
image.opacity = Magick::MaxRGB / 2
image.write("public/images/rails_with_transparency.png")

(rails.png is the default rails logo from a new rails app)
theoretically, this should be working, but is not working,
the new image rails_with_transparency.png is the same as rails.png
I didn't get any mail from you. Without the image I can't help you.
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top