Removing All Shades of Grey from an Image
-
- Posts: 8
- Joined: 2019-05-17T08:38:25-07:00
- Authentication code: 1152
Removing All Shades of Grey from an Image
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.
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Removing All Shades of Grey from an Image
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Removing All Shades of Grey from an Image
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:
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
snibgo's IM pages: im.snibgo.com
-
- Posts: 8
- Joined: 2019-05-17T08:38:25-07:00
- Authentication code: 1152
Re: Removing All Shades of Grey from an Image
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.
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Removing All Shades of Grey from an Image
My suggestion works fairly well on your image; it makes all pure gray pixels transparent. Change to "-threshold 10%" if you want.
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Removing All Shades of Grey from an Image
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:
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
-
- Posts: 8
- Joined: 2019-05-17T08:38:25-07:00
- Authentication code: 1152
Re: Removing All Shades of Grey from an Image
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.
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Removing All Shades of Grey from an Image
Good stuff. It will work better if your input image hasn't been through JPEG compression.
snibgo's IM pages: im.snibgo.com
-
- Posts: 8
- Joined: 2019-05-17T08:38:25-07:00
- Authentication code: 1152
Re: Removing All Shades of Grey from an Image
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.
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Removing All Shades of Grey from an Image
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...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")
Code: Select all
convert input.jpg \
\( +clone -colorspace CMYK -channel CMY -separate -evaluate-sequence add \) \
-colorspace sRGB -alpha off -compose copyopacity -composite result.png
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Removing All Shades of Grey from an Image
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:
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 ) ...
snibgo's IM pages: im.snibgo.com
-
- Posts: 8
- Joined: 2019-05-17T08:38:25-07:00
- Authentication code: 1152
Re: Removing All Shades of Grey from an Image
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
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
Last edited by OmahaWxMan on 2019-05-23T10:55:04-07:00, edited 1 time in total.
-
- Posts: 8
- Joined: 2019-05-17T08:38:25-07:00
- Authentication code: 1152
Re: Removing All Shades of Grey from an Image
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.
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
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
Any further pointers would be greatly appreciated.
Thanks again.
Wx