# Rmagic resizing images with transparant background

**URL:** <https://www.ruby-forum.com/t/rmagic-resizing-images-with-transparant-background/75432>\
**Category:** Rails\
**Created:** [November 9, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/rmagic-resizing-images-with-transparant-background/75432 "2006-11-09T21:12:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [November 9, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/rmagic-resizing-images-with-transparant-background/75432/1 "2006-11-09T21:12:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [November 10, 2006, 1:02am UTC](https://www.ruby-forum.com/t/rmagic-resizing-images-with-transparant-background/75432/2 "2006-11-10T01:02:17Z")

</div>

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](http://www.simplesystems.org/RMagick/doc/imusage.html#color_names))

Also, consider using Magick::Image#write  
([RMagick 1.15.0: class Image (instance methods, part 3)](http://www.simplesystems.org/RMagick/doc/image3.html#write)) instead of

> File.open(file, “wb”) do |f|  
> f.write(back.to\_blob)  
> end

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [November 10, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/rmagic-resizing-images-with-transparant-background/75432/3 "2006-11-10T19:11:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [November 10, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/rmagic-resizing-images-with-transparant-background/75432/4 "2006-11-10T22:38:15Z")

</div>

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](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”)
