Page 1 of 1
How to get a transparent background for my logo added to user uploaded images using Minimagick composite?
Posted: 2016-01-07T06:15:12-07:00
by shaomai888
Rails Code:
Code: Select all
process :watermark
def watermark
second_image = MiniMagick::Image.open("https://s3.amazonaws.com/....logo.png")
manipulate! do |img|
result = img.composite(second_image) do |c|
c.compose "Over" # OverCompositeOp
c.gravity "Southeast"
end
result
end
end
Users upload images. I want those images to have my logo placed on the bottom corner. The code above results in the logo being placed on the photos surrounded by a square white background.
I have been attempting to try to code white/#FFFFFF to be converted to transparent but have not been able to find the correct code.
Re: How to get a transparent background for my logo added to user uploaded images using Minimagick composite?
Posted: 2016-01-07T22:53:19-07:00
by fmw42
I do not know Rails or MiniMagick. But perhaps you could provide your input images and I can atleast provide the equivalent Imagemagick command line code. You can then try converting it to your API.
In order to make your logo white become transparent, try something like the equivalent of
Code: Select all
convert logo.png -fuzz XX% -transparent white new_logo.png
where XX is some value from 0 to 100 and controls how close one needs to be to white to change it to transparent. If you have pure white, the XX=0; otherwise, the more non-white (gray) you have in the logo background, the larger XX needs to be. But not so large as to convert part of the "non-white" colors to transparent.
Do that in your code, before doing the composite.
Re: How to get a transparent background for my logo added to user uploaded images using Minimagick composite?
Posted: 2016-01-10T20:44:18-07:00
by shaomai888
Well, right now I am just in practice mode because I do not yet have a logo created for the website. (I was just using a coke bottle downloaded off the web).
I see now that ImageMagick and minimagick are separate. So would turning the white background to transparent be easier with Imagemagick? If that is the case I will just switch to use the ImageMagick gem instead of minimagick. I was under the false impression they were related.
Re: How to get a transparent background for my logo added to user uploaded images using Minimagick composite?
Posted: 2016-01-10T22:11:56-07:00
by fmw42
According to
https://github.com/minimagick/minimagick, minimagick is a Ruby wrapper around Imagemagick. So it is an Imagemagick variant. There is also RMagic which is a Ruby API for Imagemagick. See the link below to Program Interfaces.
In Imagemagick command line mode, you could do something like
Code: Select all
convert image -fuzz XX% -transparent white result.png
where XX controls how close to white colors you want to make transparent. XX=0 is only exactly equal to white and larger values will convert colors further from white to transparent. You must save the image in some format such as png to preserve the transparency. JPEG does not support transparency. See
http://www.imagemagick.org/script/comma ... ransparent and
http://www.imagemagick.org/script/comma ... s.php#fuzz
There are other ImageMagick APIs that have code more similar to your minimagick. See
http://www.imagemagick.org/script/sitem ... interfaces