Problem with +transparent and +opaque

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
pcarver
Posts: 2
Joined: 2013-04-02T21:06:50-07:00
Authentication code: 6789

Problem with +transparent and +opaque

Post by pcarver »

I'm sure I've missed something obvious, but I've reread what I believe to be the relevant sections of the documentation several times and can't see what I'm missing.

I want to remove all colors except one from an image. I'm not looking to make use of or retain any geometrical information, I simply want all pixels that are the designated color to stay that color and all other pixels to be go away (ideally become transparent, but I'd settle for setting them all to white or black.)

The end game here is that I want create a sequence of frames that will become a movie of creating the original image one color at a time. The movie will start out with a black screen and pixels of the original image will appear one color per frame. Each frame of the movie will add all the pixels of a single color to the image of the previous frame.

The problem I'm running into is that neither +transparent nor +opaque seem to be working as described in the instructions. I'm running Version: ImageMagick 6.5.4-7 2012-05-07 Q16 OpenMP http://www.imagemagick.org
on CentOS 6.4 64 bit.

Code: Select all

$ convert input.jpg -define histogram:unique-colors=true -format %c histogram:info:- |grep "#1A1414"
        22: ( 26, 20, 20) #1A1414 rgb(26,20,20)
$ convert input.jpg -fill none +opaque "#1A1414" output.jpg
$ convert output.jpg -define histogram:unique-colors=true -format %c histogram:info:-
    704072: (  0,  0,  0) #000000 black
       224: (  1,  1,  1) #010101 rgb(1,1,1)
        85: (  2,  2,  2) #020202 rgb(2,2,2)
        45: (  3,  3,  3) #030303 grey1
        24: (  4,  4,  4) #040404 rgb(4,4,4)
        17: (  5,  5,  5) #050505 grey2
         7: (  6,  6,  6) #060606 rgb(6,6,6)
         9: (  7,  7,  7) #070707 rgb(7,7,7)
         4: (  8,  8,  8) #080808 grey3
         3: (  9,  9,  9) #090909 rgb(9,9,9)
         1: ( 11, 11, 11) #0B0B0B rgb(11,11,11)
         1: ( 12, 12, 12) #0C0C0C rgb(12,12,12)
         1: ( 13, 13, 13) #0D0D0D grey5
         3: ( 14, 14, 14) #0E0E0E rgb(14,14,14)
         2: ( 15, 15, 15) #0F0F0F grey6
         1: ( 16, 16, 16) #101010 rgb(16,16,16)
         4: ( 17, 17, 17) #111111 rgb(17,17,17)
         3: ( 18, 18, 18) #121212 grey7
         3: ( 20, 20, 20) #141414 grey8
         1: ( 22, 22, 22) #161616 rgb(22,22,22)
         1: ( 25, 25, 25) #191919 rgb(25,25,25)
         1: ( 26, 26, 26) #1A1A1A grey10
$ rm output.jpg
$ convert input.jpg -fill white +opaque "#1A1414" output.jpg
$ convert output.jpg -define histogram:unique-colors=true -format %c histogram:info:-
         2: ( 16, 16, 16) #101010 rgb(16,16,16)
         1: ( 18, 18, 18) #121212 grey7
         4: ( 19, 19, 19) #131313 rgb(19,19,19)
         6: ( 20, 20, 20) #141414 grey8
         2: ( 21, 21, 21) #151515 rgb(21,21,21)
         5: ( 22, 22, 22) #161616 rgb(22,22,22)
         1: ( 23, 23, 23) #171717 grey9
         1: ( 26, 26, 26) #1A1A1A grey10
         3: (247,247,247) #F7F7F7 grey97
         5: (248,248,248) #F8F8F8 rgb(248,248,248)
         8: (249,249,249) #F9F9F9 rgb(249,249,249)
        24: (250,250,250) #FAFAFA grey98
        57: (251,251,251) #FBFBFB rgb(251,251,251)
        93: (252,252,252) #FCFCFC grey99
       167: (253,253,253) #FDFDFD rgb(253,253,253)
       194: (254,254,254) #FEFEFE rgb(254,254,254)
    703939: (255,255,255) #FFFFFF white

As you can see I'm ending up with an image with a bunch of colors in it. What I'm expecting in the histogram after using +opaque is an image with exactly two colors, 1) the color I specified with +opaque and 2) the fill color. Why am I getting a bunch of other colors and not getting my designated color?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Problem with +transparent and +opaque

Post by snibgo »

If you want to replace all colours apart from a specified one with transparency, use "+transparent". See http://www.imagemagick.org/script/comma ... ransparent

Code: Select all

convert input.jpg +transparent "#1A1414" output.jpg
Your IM version is also very old; I don't know (and can't test) if "+transparent" works in your version.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem with +transparent and +opaque

Post by fmw42 »

with +opaque you may need to enable the alpha channel

try

convert input.jpg -channel rgba -fill none +opaque "#1A1414" output.png

Note that jpg does not support transparency, so I have changed the output to png.

This works fine for me on IM 6.8.4.6 Q16 Mac OSX Snow Leopard

convert logo: -channel rgba -fill none +opaque white show:


Your version of IM is very old ( 400 versions old). Perhaps you should consider upgrading.
pcarver
Posts: 2
Joined: 2013-04-02T21:06:50-07:00
Authentication code: 6789

Re: Problem with +transparent and +opaque

Post by pcarver »

Changing the filetype to png did the trick, even without the "-channel rgba" part. The exact same command with an output.jpg ends up with a histogram with lots of colors while output.png ends up with a histogram of exactly two colors (#000000 and #1A1414) as I wanted. Thanks.

This is actually CentOS on a VM that I created this week and installed Image Magick simply by running "yum install ImageMagick" so I'm not sure why it's very old. I'll need to dig into why I wouldn't have gotten the newest version when I did a fresh install.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Problem with +transparent and +opaque

Post by anthony »

Because CentOS and Redhat are Enterprise level. That means they only upgrade for critical security patches, so as to provide a 'fixed' or unchanging system from version to version, and thus provide 'stability' for enterprise applications.

In other words these systems are always 'old' with regards to software that continually gets new features, such as ImageMagick.

that is not to say you can not download and compile a newer ImageMagick version for these systems, but eventually the old libraries make it harder and harder to satisfy the dependencies needed.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply