Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rmagick - image with transparent background from text

Tags:

ruby

rmagick

I am trying to create an image file from text using Rmagick in ruby. Additionally I need it's background transparent. My code:

canvas = Image.new(400, 60)

watermark_text = Draw.new
watermark_text.annotate(canvas, 0,0,0,0, text) do
  self.gravity = WestGravity
  self.pointsize = 50
  self.font = "whatever.ttf"
  self.fill = 'black'
  self.stroke = "none"
end

canvas.write(@path)

It works, but the background of the image is white and I need it to be transparent. Any ideas? File is saved as png.

like image 590
Salet Avatar asked Jul 11 '13 13:07

Salet


1 Answers

When you create an image, the default background is white. You can tell rmagick you want a different background:

canvas = Image.new(400, 60) do |c|
  c.background_color= "Transparent"
end
like image 142
Neil Slater Avatar answered Sep 28 '22 12:09

Neil Slater