Threshold red color on an all red transparent PNG

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
graftonclipper
Posts: 1
Joined: 2017-09-14T14:59:09-07:00
Authentication code: 1151

Threshold red color on an all red transparent PNG

Post by graftonclipper »

Hi, I have transparent PNGs that look like this

Image

And I'm wondering how I can threshold the red so that it only shows the darkest reds, like in the image below.

Image

Ideally, the output would be have the white as a transparent png background.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Threshold red color on an all red transparent PNG

Post by fmw42 »

Please post your transparent PNG input and desired output. You have posted JPGs or you hosting provide has changed them.

Also please always provide your IM version and platform, since syntax may differ.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Threshold red color on an all red transparent PNG

Post by GeeMack »

graftonclipper wrote: 2017-09-14T15:16:27-07:00Hi, I have transparent PNGs that look like this [...] And I'm wondering how I can threshold the red so that it only shows the darkest reds, like in the image below. [...] Ideally, the output would be have the white as a transparent png background.
You can get a result much like your example output image if you start by creating a sort of threshold transparency mask from a clone of your input image. Then you can composite that mask onto the input image to leave only the darkest red showing on a transparent background. I've tried this with IM 6.9.8 in Windows and IM 6.7.7 from bash. In Windows CMD syntax...

Code: Select all

convert input.jpg -alpha set ^
   ( -clone 0 -colorspace gray -brightness-contrast 25,100 -negate -alpha copy ) ^
   -compose copyopacity -composite output.png
The threshold can be adjusted by changing N in the "-brightness-contrast N,100" setting. To nearly match your output example I have to set it between 25 and 50 depending on which version of IM I'm using.

To run a command like this from a *nix shell or script you'd have to change the continued line carets "^" to backslashes "\" and escape the parentheses with backslashes "\( ... \)".

If you're using IM 7 you'll use the command "magick ..." instead of "convert ...".
Post Reply