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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Nick Brutyn 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 > endHmmm...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"? (http://www.simplesystems.org/RMagick/doc/imusage.html#color_names) Also, consider using Magick::Image#write (http://www.simplesystems.org/RMagick/doc/image3.html#write) instead of> File.open(file, "wb") do |f| > f.write(back.to_blob) > end-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Nick Brutyn 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") -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---