How to get a transparent background for my logo added to user uploaded images using Minimagick composite?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
shaomai888
Posts: 2
Joined: 2016-01-07T05:38:18-07:00
Authentication code: 1151

How to get a transparent background for my logo added to user uploaded images using Minimagick composite?

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get a transparent background for my logo added to user uploaded images using Minimagick composite?

Post 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.
shaomai888
Posts: 2
Joined: 2016-01-07T05:38:18-07:00
Authentication code: 1151

Re: How to get a transparent background for my logo added to user uploaded images using Minimagick composite?

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get a transparent background for my logo added to user uploaded images using Minimagick composite?

Post 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
Post Reply