Page 1 of 2
Removing All Shades of Grey from an Image
Posted: 2019-05-17T09:05:59-07:00
by OmahaWxMan
Hi All,
I've been hitting the google books and am trying to figure out how to remove all shades of grey from an image. (ie preserve all "colors")
I wrote a bash script that works, but is slow and doesn't scale, when each image is exponentially larger. (250 -> 500 -> 1000 -> 2000 -> 4000 -> 8000 pixels)
file=13-1000x1000.jpg
temp=output.png
convert $file -fuzz 0% -fill transparent -opaque 'rgb(255,255,255)' $temp
for rgb in `seq 0 1 254`; do
convert $temp -fuzz 0% -fill transparent -opaque "rgb($rgb,$rgb,$rgb)" $temp
done
convert $temp -transparent black $temp
Is there a way with ImageMagick to accomplish this more efficiently?
Thanks
Wx.
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-17T09:15:52-07:00
by fmw42
Please always provide your IM version and platform when asking questions. Also provide an example input and output image, so we can see what is going on and suggest solutions. Post images to some free hosting service and put the URLs here
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-17T09:24:55-07:00
by snibgo
Reading and writing miff is much faster than png, so that would help.
Your solution won't work for 16-bit images unless you have a longer loop.
A loop-less method is to find where chroma is zero, and make those pixels transparent:
Code: Select all
magick in.png ( +clone -colorspace HCL -channel G -separate +channel -threshold 0 ) -alpha off -compose CopyOpacity -composite out.png
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-17T09:33:15-07:00
by OmahaWxMan
Sure, sorry, I should have read the forum posting guide first.
CentOS 7,
Version: ImageMagick 6.7.8-9 2019-02-01 Q16
http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP
Before:
After:
Thanks again,
Wx.
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-17T09:50:32-07:00
by snibgo
My suggestion works fairly well on your image; it makes all pure gray pixels transparent. Change to "-threshold 10%" if you want.
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-17T09:50:37-07:00
by fmw42
I would modify snibgo's command slightly since you have a JPG which will show JPG compression artifacts. So I increase the threshold to 15%.
Unix syntax for IM 6:
Code: Select all
convert in.jpg \( +clone -colorspace HCL -channel G -separate +channel -threshold 15% \) -alpha off -compose CopyOpacity -composite out.png
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-17T11:19:09-07:00
by OmahaWxMan
All,
Thank you so much. I've incorporated the one-liner into another script and the results are wonderful.
I'll share a low-resolution image here. The high resolution is 10000x6000, but this should suffice.
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-17T11:31:03-07:00
by fmw42
Nice!
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-17T11:31:51-07:00
by snibgo
Good stuff. It will work better if your input image hasn't been through JPEG compression.
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-17T11:45:34-07:00
by OmahaWxMan
Yes, GIFs are too big though, I'm exporting the JPG from the source software @ 100% quality, which ends up being around 25MB for that image. I think it'll be "good enough for government." My aim is a pretty picture that's useful but not science quality.
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-17T16:01:45-07:00
by GeeMack
OmahaWxMan wrote: ↑2019-05-17T09:05:59-07:00I've been hitting the google books and am trying to figure out how to remove all shades of grey from an image. (ie preserve all "colors")
There are several ways to approach this, and some very good solutions have already been provided. I worked from a slightly different direction. This uses a mask made from adding the not-black channels in the CMYK colorspace...
Code: Select all
convert input.jpg \
\( +clone -colorspace CMYK -channel CMY -separate -evaluate-sequence add \) \
-colorspace sRGB -alpha off -compose copyopacity -composite result.png
It seems to leave a little less speckling around the colored zones than some of the results of the previously offered solutions.
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-17T16:31:43-07:00
by snibgo
Interesting method, GeeMack. It creates opacity less than 100% where the sum of C+M+Y is less than 100%. This may be reasonable, or could be thresholded to make the opacity either 0 or 100%.
We can make it slightly faster (and use less memory) by separating just the CMY channels, rather then separating them all then deleting the K channel:
Code: Select all
... ( +clone -colorspace CMYK -channel 0,1,2 -separate +channel -evaluate-sequence add ) ...
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-17T16:59:18-07:00
by GeeMack
snibgo wrote: ↑2019-05-17T16:31:43-07:00We can make it slightly faster (and use less memory) by separating just the CMY channels, rather then separating them all then deleting the K channel:
Thanks. I noticed that and was editing probably as you were replying.
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-21T06:26:21-07:00
by OmahaWxMan
Thanks again for the additional examples. I've tested all three and the results from the time command, when applied to the overall script that creates the imagery, add little overhead. Here's one more example from last night; if you happen to live between west Texas and Missouri I hope you are OK.
This is 5000x3000 and about 7MB in size, originally. I don't know what compression Google used, but the link is down to 1.4MB.
Thanks,
Omaha
Re: Removing All Shades of Grey from an Image
Posted: 2019-05-22T10:51:32-07:00
by OmahaWxMan
Hi Everyone,
Last question for this thread from me, hopefully. After more testing I realized that I can't entirely drop the black channel from the CYMK separation. If you've ever looked at the color bars that the National Weather Service uses on their radar after the magenta/pink band comes white for the most powerful (coldest) sections of a storm.
Example:
https://lh3.googleusercontent.com/07_WJ ... -4=s500-no
Dropping out the K-channel will cause the center of the storm to become transparent with the above convert commands
e.g.
Code: Select all
convert $1 \
\( +clone -colorspace CMYK -channel CMY -separate -evaluate-sequence add \) \
-colorspace sRGB -alpha off -compose copyopacity -composite \
overlay1.png
To my novice eyes, adding a second +clone line and fuzzing the K-channel to a few percentage points of #000000 would work, but I'm not sure how to pull that off and then combine both into the intended results.
Any further pointers would be greatly appreciated.
Thanks again.
Wx