Combine transparent, fill, opaque into single command

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
User avatar
Tosyk
Posts: 25
Joined: 2012-09-11T18:55:12-07:00
Authentication code: 67789
Location: Armenia

Combine transparent, fill, opaque into single command

Post by Tosyk »

I'm trying to turn magenta color of an image simultaneously into transparent and black. the following code doesn't work together — it only work if I split it into two different operations. so here it is:

Code: Select all

C:\Program Files\ImageMagick\convert.exe" 2.gif -fuzz 100% -alpha on -transparent magenta -colorspace sRGB -fill black -opaque magenta -gamma 1.05,1.05,1.05 -format png32 2.png
image attached. anyone able to help me?

https://drive.google.com/open?id=1DlOpt ... I-9KcJCr3D
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combine transparent, fill, opaque into single command

Post by fmw42 »

I do not understand how you can turn magenta into both transparent and black at the same time. Please clarify and show your desired output and the two separate commands that produce it.

If you just want to turn the magenta to transparent-black, which is rgba(0,0,0,0), then you can do

Code: Select all

convert 2.gif -fuzz 10% -transparent magenta -background black -alpha background PNG32:2.png

or if you want to remove the transparency and have the result be black where it was magenta, then do

Code: Select all

convert 2.gif -fuzz 10% -transparent magenta -background black -alpha background -alpha off 2.png

I do not recommend using -fuzz 100% or it will catch all colors and make your image totally transparent.

Add your -level as desired.

_______________________________

Please, always provide your IM version and platform when asking questions, since syntax may differ.

Also provide your exact command line and your images, if possible.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620

If using Imagemagick 7, then see http://imagemagick.org/script/porting.php#cli


For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown
https://imagemagick.org/script/porting.php#cli
User avatar
Tosyk
Posts: 25
Joined: 2012-09-11T18:55:12-07:00
Authentication code: 67789
Location: Armenia

Re: Combine transparent, fill, opaque into single command

Post by Tosyk »

here's image I want to get as a result:
https://drive.google.com/open?id=1gpDQY ... kSdRX2gFOe

I want to change magenta to black on image and add alpha channel to the same position where magenta was

I'm using 7.0.8-10 version
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combine transparent, fill, opaque into single command

Post by fmw42 »

In Imagemagick 7, use magick not convert. So this does what you want.

Code: Select all

magick 2.gif -fuzz 10% -transparent magenta -background black -alpha background PNG32:2.png
In windows it would be magick.exe. In a bat file, % needs to be doubled to %%.

See https://imagemagick.org/Usage/masking/#alpha for the use of -alpha background
User avatar
Tosyk
Posts: 25
Joined: 2012-09-11T18:55:12-07:00
Authentication code: 67789
Location: Armenia

Re: Combine transparent, fill, opaque into single command

Post by Tosyk »

OMG! thank you! exactly what I want.
I use convert.exe for long time and never realised that magic.exe can do more tricks :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combine transparent, fill, opaque into single command

Post by fmw42 »

See https://imagemagick.org/script/porting.php#cli

For other tools such as identify, mogrify, montage, etc, you do need to preface them with magick, such as magick identify. But not for convert, it is just replaced by magick.


An alternate approach is

Code: Select all

magick 2.gif -fuzz 10% -fill none -opaque magenta PNG32:2.png
See https://imagemagick.org/Usage/color_basics/#replace

None is equivalent to transparent and also reba(0,0,0,0) and #00000000 or #000

See https://imagemagick.org/script/color-management.php
User avatar
Tosyk
Posts: 25
Joined: 2012-09-11T18:55:12-07:00
Authentication code: 67789
Location: Armenia

Re: Combine transparent, fill, opaque into single command

Post by Tosyk »

thanks for the tip! however I can't understand this sequence — the one with converter.exe is clearer for me. here's how I see this peace of code:

Code: Select all

magick 2.gif -fuzz 10% -fill none -opaque magenta PNG32:2.png
  1. magick - use magick.exe
  2. 2.gif - set input file
  3. fuzz 10% - set this parameter to use further in code
  4. fill none - fill with no color, so basically I understand this command as "do nothing"
  5. opaque magenta - (use fuzz 10% here) set magenta as none transparent color? why? I want it to be transparent instead
  6. PNG32: - set png format to be used with alpha channel
  7. 2.png - set output file
this is why I'd never came up with this approach myself.

btw, I use this cmd code to convert files by drag and drop on it:

Code: Select all

@echo off
if [%1]==[] goto :eof
:loop

"C:\Program Files\ImageMagick\magick.exe" ^
	%1 ^
	-fuzz 10% ^
	-transparent magenta ^
	-background black ^
	-alpha background ^
	-gamma 1.05,1.05,1.05 ^
	-format png32 ^
	"%~dpn1.png"
	
shift
if not [%1]==[] goto loop
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combine transparent, fill, opaque into single command

Post by fmw42 »

fill none - fill with no color, so basically I understand this command as "do nothing"
-fill none means fill with transparent black, i.e, rgba(0,0,0,0). It is the same as -fill transparent.

-opaque magenta tell IM which color to replace with the -fill color.

So here -fill none -opaque magenta, means to fill magenta (and colors within the fuzz value) all to transparent.

The fuzz values tells IM how close to pure magenta you want to include in the replacement to transparent.
User avatar
Tosyk
Posts: 25
Joined: 2012-09-11T18:55:12-07:00
Authentication code: 67789
Location: Armenia

Re: Combine transparent, fill, opaque into single command

Post by Tosyk »

thanks for the clarification!
Post Reply