Colorspaces and porting from IM 6.6.x

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
coloring
Posts: 82
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Colorspaces and porting from IM 6.6.x

Post by coloring »

I have an old script that is used to do clean up some images before OCR. The script has grown organically but it is "well oiled" and it has done its job perfectly in the past years. Now we moved to a new Ubuntu version that ships version 6.7.7-10 and the script is producing horrible results. We traced it down to a change in the way default colorspaces are handled, as suggested by http://www.imagemagick.org/script/color-management.php and viewtopic.php?t=20501.

I tried sprinkling some -set colorspace RGB -colorspace RGB around but it did not work.

The core of my script is

Code: Select all

cat "$workdir/src.png" |
	# remove shades of gray1
	convert png:fd:0 -fuzz 10% -fill black -opaque 'rgb(159,153,153)' png:fd:1 |
	tee /tmp/step1.png |

	# remove shades of gray2
	convert png:fd:0 -fuzz 10% -fill black -opaque 'rgb(109,97,109)' png:fd:1 |
	tee /tmp/step2.png |

	# remove shades of gray3
	convert png:fd:0 -fuzz 10% -fill black -opaque 'rgb(178,185,193)' png:fd:1 |
	tee /tmp/step3.png |

	# convert to grayscale with an emphasis on reds
	convert png:fd:0 \
		\( -clone 0 -channel GR -separate +channel -evaluate-sequence mean \) \
		\( -clone 0 -channel RB -separate +channel -evaluate-sequence mean \) \
		-delete 0 -evaluate-sequence mean \
		png:fd:1 |
	tee /tmp/step4.png |

	# black text on white background
	convert png:fd:0 -threshold 50% -negate png:fd:1 |
	cat > "$workdir/bw.png"
This script works perfectly with IM 6.6.9 but produces completely wrong results with IM 6.7.7-10.

How can I change this script to work in the same way it both version or, at least, only in version 6.7.7 but in the correct way?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Colorspaces and porting from IM 6.6.x

Post by snibgo »

My advice: don't "upgrade" to 6.7.7. That's like "upgrading" an old but reliable car to another old and different (and very weird) car.

6.7.7 and similar version were confused about RGB/sRGB colourspaces, and especially when the image was grayscale. Upgrade to the current version.
snibgo's IM pages: im.snibgo.com
coloring
Posts: 82
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Colorspaces and porting from IM 6.6.x

Post by coloring »

For what is worth I'd stay with 6.6.9, but we would like to use what comes with the current Ubuntu LTS, and that is 6.7.7. :(
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colorspaces and porting from IM 6.6.x

Post by fmw42 »

Unfortunately, 6.7.7 was during a major change in colorspace and may not be fully correct. See viewtopic.php?f=4&t=21269. So Ubuntu picked a very unstable release.
coloring
Posts: 82
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Colorspaces and porting from IM 6.6.x

Post by coloring »

Just as a workaround, isn't there a way out of RGB/sRGB? The intermediate files do not need to be in PNG format so anything will do.

WRT Ubuntu, the LTS will be stuck on 6.7.7 until 16.04 LTS: https://bugs.launchpad.net/ubuntu/+sour ... ug/1179054 (15.04 is not an LTS).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colorspaces and porting from IM 6.6.x

Post by fmw42 »

try adding either

-set colorspace RGB
or
-set colorspace sRGB

before -channel in your commands. I am not sure which is needed, because the releases at that time were undergoing a swap of RGB and sRGB and grayscale (separate channels) were considered non-linear (sRGB) at one time, then linear (RGB) at another time, then back to non-linear again.
coloring
Posts: 82
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Colorspaces and porting from IM 6.6.x

Post by coloring »

Using -set colorspace RGB also before -channel worked. I had already put that command everywhere, but not inside the expression.

Thank you very much fmw42!
Last edited by coloring on 2015-08-27T12:19:15-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colorspaces and porting from IM 6.6.x

Post by fmw42 »

Glad to hear that was all that was needed.
Post Reply