Remove Gradient Background

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
wmichaelv
Posts: 12
Joined: 2014-02-15T15:54:19-07:00
Authentication code: 6789

Remove Gradient Background

Post by wmichaelv »

I browsed a bit on this forum about gradient coloring. There's a topic talking about pixels, there's a topic talking about color range. They have similar problem as me, but it's not exactly the same, so I don't really know what to type on the command prompt.

This is my problem:

I would like to change the following image:

Image

(So I can get the 'bullet' and then color it)

Into (this is a very very rough display done via photoshop since I'm not sure how to take out the gradient background on photoshop either) :

Image

Then color it (again, done via photoshop):

Image

I tried

Code: Select all

convert bullet.png -fuzz 10% -fill none -draw "matte 0,0 replace" asd.png
But the result is very rough (you can see the pixel corners)
Image

The reason why I choose imagemagick rather than photoshop is also because I'm going to do this on multiple images, so that I could save 15-20 hours of editting.

Regards.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove Gradient Background

Post by fmw42 »

try this on your second image

Code: Select all

convert result1_zps9b323129.png -alpha off -colorspace gray +level-colors "black,green1" -alpha on show:
adjust the green1 color as needed. You can use rgb or hex colors as well as names. see

http://www.imagemagick.org/script/color.php
http://www.imagemagick.org/Usage/color_ ... vel-colors
http://www.imagemagick.org/script/comma ... vel-colors
wmichaelv
Posts: 12
Joined: 2014-02-15T15:54:19-07:00
Authentication code: 6789

Re: Remove Gradient Background

Post by wmichaelv »

Thanks!!

The result looks good.

Image

But I'm wondering if there's a way to remove the gradient black background.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove Gradient Background

Post by fmw42 »

try this on your original image

Code: Select all

convert bulletAa000_zps37fdf1ff.png -alpha off -colorspace gray +level-colors "black,green1" -fuzz 20% -transparent black result.png
adjust the fuzz value to suit.
wmichaelv
Posts: 12
Joined: 2014-02-15T15:54:19-07:00
Authentication code: 6789

Re: Remove Gradient Background

Post by wmichaelv »

I don't think it's about fuzzing.

It's like you have this picture.

Image

And these backgrounds


Image

Black

Image

Green

And then you combine them, so you would get:

Image

Black Background

Image

Green Background

And so I'm wondering how to remove that gradient background. Thanks! : )
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove Gradient Background

Post by fmw42 »

I am now very confused about what you want removed? Please clarify further.

try this

Code: Select all

convert bulletAa000_zps37fdf1ff.png -alpha off -colorspace gray -fuzz 80% -transparent black tmp1.png
or this

Code: Select all

convert bulletAa000_zps37fdf1ff.png -alpha off -colorspace gray -fuzz 80% -fill black -opaque black tmp2.png:
or this

Code: Select all

convert bulletAa000_zps37fdf1ff.png -alpha off -colorspace gray +level-colors "black,green1" \
\( -clone 0 -fill white -colorize 100 -background black -vignette 0x0+50%+50% -blur 0x1 \) \
-alpha off -compose over -compose copy_opacity -composite tmp3.png
adjust the blur 0xX to feather the round outline. The vignette is used to create a circle without knowing the image size and using a percent of the dimensions for the size of the circle. You may need to use a smaller size than 50%, since I believe that -vignette had a bug fix after the version I am using here, today.
wmichaelv
Posts: 12
Joined: 2014-02-15T15:54:19-07:00
Authentication code: 6789

Re: Remove Gradient Background

Post by wmichaelv »

Ah, sorry for the confusion.
What I want to do is to change this picture:

Image


Into:


Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove Gradient Background

Post by fmw42 »

try this

Code: Select all

convert asdBlackResult_zpsff8b4756.png  \
\( +clone -fill white +opaque black \) \
-alpha off -compose copy_opacity -composite \
-brightness-contrast 50x20 tmp1.png
or

Code: Select all

convert  asdBlackResult_zpsff8b4756.png  \
\( -clone 0 -fill black -colorize 100 \) \
\( -clone 0 -fill white +opaque black \) \
\( -clone 0 -clone 1 +swap -compose subtract -composite  \) \
-delete 0,1 -alpha off +swap  -compose copy_opacity -composite  \
-brightness-contrast 50x20 tmp2.png
or perhaps add some -modulate

Code: Select all

convert asdBlackResult_zpsff8b4756.png  \
\( +clone -fill white +opaque black \) \
-alpha off -compose copy_opacity -composite \
-brightness-contrast 50x20 -modulate 120,90,100 tmp3.png

Background removal can be hard. See http://www.imagemagick.org/Usage/masking/#bg_remove
wmichaelv
Posts: 12
Joined: 2014-02-15T15:54:19-07:00
Authentication code: 6789

Re: Remove Gradient Background

Post by wmichaelv »

Thanks for the replies and assistances. I really appreciate them.

I'm having a bit of difficulty at converting the script into windows cmd. Haha

Edit:

I check that link you gave me, it's very helpful. It seems I'm required to provide a second background.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove Gradient Background

Post by fmw42 »

No second background is needed. The main things are: 1) remove \ from parenthesis, 2) use ^ rather than \ for escapes and new line symbol and 3) double % to %%

In windows syntax:

Code: Select all

convert asdBlackResult_zpsff8b4756.png  ^
( +clone -fill white +opaque black ) ^
-alpha off -compose copy_opacity -composite ^
-brightness-contrast 50x20 -modulate 120,90,100 tmp3.png
wmichaelv
Posts: 12
Joined: 2014-02-15T15:54:19-07:00
Authentication code: 6789

Re: Remove Gradient Background

Post by wmichaelv »

Thanks for the help, I have mastered the art of conversion now. I really do appreciate, and start to feel bad about myself for continuing this free service. I would love to buy you a lunch if we lived close by. Haha.

A problem though, while the result is good, it doesn't store the opacity level of each circles.

Image

I've read the tutorial that you gave me, and it has a good part with the opacity, and how to get a real good result. Thanks again for the good read.
Background Removal using Two Backgrounds
The major problem with the previous techniques is that you really do not have enough information to completely recover all the information about the foreground object.
You really need to recover two pieces of information, how transparent each pixel in the foreground object is, and what is its original color. And you can not perfectly recover both pieces of information from just one image.
Even when you know exactly what the background image looks like, you can not just subtract it from the foreground object, unless the two are very different and known colors.
The problem is you simply can not be sure if the color that is visible is really the color given (opaque), or it is some blending of some other color and the background (semi-transparent). You can not separate the origina color from the alpha value needed, unless you have a source of some extra information.
The one situation in which you can completely recover all the details of a foreground object, is when you have two images containing two very different but completely known background colors. In that situation you do have enough information to recover both the color and its transparency of the foreground object, for a perfect background removal.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove Gradient Background

Post by fmw42 »

try this to preserve your transparency channel

Code: Select all

convert asdBlackResult_zpsff8b4756.png ^
( -clone 0 -alpha off -brightness-contrast 50x20 -modulate 120,90,100 ) ^
( -clone 0 -alpha on -channel a -separate +channel ) ^
-delete 0 -alpha off -compose copy_opacity -composite tmp1.png
or

Code: Select all

convert asdBlackResult_zpsff8b4756.png -brightness-contrast 50x20 -modulate 120,90,100 tmp2.png
wmichaelv
Posts: 12
Joined: 2014-02-15T15:54:19-07:00
Authentication code: 6789

Re: Remove Gradient Background

Post by wmichaelv »

I think I understand all the commands that I need. I just have to experiment and set the numbers properly now. Thanks for helping this hopeless kid. : )
Post Reply