Rmagic resizing images with transparant background

Hey,

I use Rmagic for resizing images, and i want a transparant background,
now I use a white background, but that is to beautiful on an gradiant
background :s:s

I have this code:

if !File.file?(file)
width = pic.columns
height = pic.rows
if (width < desired_width && height < desired_height)
#no scale needed
else
if (width > height)
pic.scale!((desired_width.to_f/width.to_f))
else
pic.scale!((desired_height.to_f/height.to_f))
end
end
back = Magick::Image.new(desired_width.to_i,desired_height.to_i) {
self.background_color = ‘white’ #need to be transparant
self.format = ‘JPG’ #perhaps gif or png then
}
if (width < desired_width && height < desired_height)
#no scale needed, center in white space
x = (desired_width - width) / 2
y = (desired_height - height) / 2
back.composite!(pic, Magick::CenterGravity, x, y,
Magick::InCompositeOp)
else
back.composite!(pic, Magick::CenterGravity, Magick::InCompositeOp)
end
File.open(file, “wb”) do |f|
f.write(back.to_blob)
end
end

Nick B. wrote:

Hey,

I use Rmagic for resizing images, and i want a transparant background,
now I use a white background, but that is to beautiful on an gradiant
background :s:s

I have this code:

if !File.file?(file)
width = pic.columns
height = pic.rows
if (width < desired_width && height < desired_height)
#no scale needed
else
if (width > height)
pic.scale!((desired_width.to_f/width.to_f))
else
pic.scale!((desired_height.to_f/height.to_f))
end
end
back = Magick::Image.new(desired_width.to_i,desired_height.to_i) {
self.background_color = ‘white’ #need to be transparant
self.format = ‘JPG’ #perhaps gif or png then
}
if (width < desired_width && height < desired_height)
#no scale needed, center in white space
x = (desired_width - width) / 2
y = (desired_height - height) / 2
back.composite!(pic, Magick::CenterGravity, x, y,
Magick::InCompositeOp)
else
back.composite!(pic, Magick::CenterGravity, Magick::InCompositeOp)
end
File.open(file, “wb”) do |f|
f.write(back.to_blob)
end
end

Hmmm…well, the JPEG format doesn’t support transparency. You’ll have
to use GIF or PNG.

Have you tried specifying a background color of “none” or “transparent”?
(RMagick 1.15.0: ImageMagick/GraphicsMagick Conventions)

Also, consider using Magick::Image#write
(RMagick 1.15.0: class Image (instance methods, part 3)) instead of

File.open(file, “wb”) do |f|
f.write(back.to_blob)
end

Hey,

now I have this code, but all my images are black and 1 kb big.


file = dir + fsimage.id.to_s + “" + desired_width.to_i.to_s + "” +
desired_height.to_i.to_s + “.png”

back = Magick::Image.new(desired_width.to_i,desired_height.to_i) {
#self.background_color = ‘white’
#self.format = ‘JPG’
self.background_color = ‘transparent’
self.format = ‘PNG’
}

#File.open(file, “wb”) do |f|

f.write(back.to_blob)

#end
back.write(file)

Can u help me?
Thx
N.

Nick B. wrote:

Hey,

now I have this code, but all my images are black and 1 kb big.


file = dir + fsimage.id.to_s + “" + desired_width.to_i.to_s + "” +
desired_height.to_i.to_s + “.png”

back = Magick::Image.new(desired_width.to_i,desired_height.to_i) {
#self.background_color = ‘white’
#self.format = ‘JPG’
self.background_color = ‘transparent’
self.format = ‘PNG’
}

#File.open(file, “wb”) do |f|

f.write(back.to_blob)

#end
back.write(file)

Can u help me?
Thx
N.

The following script composites a 240x320 JPG onto a 400x400 transparent
background and writes the resulting image in PNG format. I’m using
RMagick 1.14.1 and ImageMagick 6.2.9.

If you have any other questions about RMagick please email me at rmagick
AT rubyforge DOT org, or post in the RMagick help forum on RubyForge
(http://rubyforge.org/forum/forum.php?forum_id=33). I don’t read this
list very often but I get a copy of every posting to the forum. Good
luck!

#!/usr/bin/env ruby
require ‘RMagick’
img = Magick::Image.read(“face.jpg”).first
bg = Magick::Image.new(400, 400) {self.background_color = ‘none’}
result = bg.composite(img, Magick::CenterGravity,
Magick::OverCompositeOp)
result.write(“tb.png”)