Page 2 of 4

Re: Converting anything to RGB correctly

Posted: 2010-06-22T03:54:12-07:00
by Albert25
In case it's useful to others coming across this thread, here is a script with plenty of comments, which should be easy to adapt to various needs:

Code: Select all

#!/bin/bash

## convert (essentially from AdobeRGB or CMYK) to sRGB jpg
## and optionally resize


# paths to profile files
srgb=/docs/photos/icc/sRGB.icm
cmyk=/docs/photos/icc/USWebCoatedSWOP.icc

# defaults
outdir=.
debug=

# read options
while getopts "dvho:s:a:" opt
do
  case $opt in
    "d|v" ) debug=1;; # verbose is same as debug for now
    "s"   ) size=$OPTARG;;
    "o"   ) outdir=$OPTARG;;
    "a"   ) add_options=$OPTARG;;
    "h"   ) cat <<END_HELP;
Usage: $0 [options] FILE [FILE...]
Options:
    -h                   Print this help
    -d                   Print debugging info while processing
    -v                   Be verbose (same as -d for now)
    -s nnn[xhhh]    Specify new width in pixels or new size as widthxheight (ie -w 640x480)
    -a "im options" Additional options for IM
    -o dir              Output directory
END_HELP
            exit 0;;
  esac
done

shift $(($OPTIND - 1))

if [ -n "$size" ]; then
	resize="-resize $size"
else
	resize=
fi

shopt -s nullglob
for f in "$@"
do
	# skip if not jpeg or tiff
	#[[ "$f" =~ '\.(jpe?g|tiff?)$' ]] || continue

	dir=$(dirname "$f")
	name=$(basename "$f")
	base=${name%.*}

	profile="/tmp/$base.icc"

	out="$outdir/$base-x$size.jpg"

	# extract possible color profile
	[ -n "$debug" ] && echo "    extracting profile $f"
	convert "$f" "$profile" 2>/dev/null

	[ -n "$debug" ] && echo "    resize = $resize"

	if [ -s "$profile" ]; then
		# we have an embedded profile, so ImageMagick will use that anyway
		[ -n "$debug" ] && echo "    convert with embedded profile $f"
		convert "$f" -profile "$srgb" +profile '*' $resize $add_options "$out" \
		&& echo -e "\tOK $out"
	else
		# no embedded profile in source
		if identify -format "%r" "$f" | grep CMYK >/dev/null ; then
			# CMYK file without embedded profile
			[ -n "$debug" ] && echo "    convert cmyk without embedded profile $f"
			convert "$f" -profile "$cmyk" -profile "$srgb" +profile '*' $resize $add_options "$out" \
			&& echo -e "\tOK $out"
			# alternative would be
			#convert "$f" -colorspace RGB -resize $size "$out"
		else
			# No profile and not CMYK. Probably already in RGB. Just resize
			[ -n "$debug" ] && echo "    resizing only $f"
			convert "$f" $resize $add_options "$out" \
			&& echo -e "\tOK $out"
		fi
	fi
	[ -f "$profile" ] && rm "$profile"
done
I use it in Linux, but I guess it should also work on Mac.

Re: Converting anything to RGB correctly

Posted: 2010-06-26T21:17:53-07:00
by Drarakel
One more suggestion... (I'm also in the process of updating my scripts.)
if [ -s "$profile" ]; then
# we have an embedded profile, so ImageMagick will use that anyway
[ -n "$debug" ] && echo " convert with embedded profile $f"
convert "$f" -profile "$srgb" +profile '*' $resize $add_options "$out" \
&& echo -e "\tOK $out"
In the "with embedded profile" case, you could check if the profile is already sRGB. A sRGB->sRGB conversion doesn't change the pixels - but the JPG recompression would be unnecessary then (unless you always include another operations like resize).
Personally, I do such a check with exiftool now - one can get the color profile name relatively easy there. (The most common sRGB profile name is "sRGB IEC61966-2.1", I believe.)

Re: Converting anything to RGB correctly

Posted: 2010-06-27T20:41:09-07:00
by anthony
I have placed the script in the IM Examples script area
http://www.imagemagick.org/Usage/script ... t_any2srgb

However you may like to have a look at IM example hints on better script writing
Hints for Better ImageMagick Shell/PHP Scripts
http://www.imagemagick.org/Usage/api/#scripts

Re: Converting anything to RGB correctly

Posted: 2010-07-04T01:29:21-07:00
by Albert25
Can you give me a hint on what specific aspect you feel needs "better script writing"? If something specific can make the script more portable, or if comments are unclear for you, or there is something else you think needs to be improved, I would be happy to use your advice.

If it is to add more options than just resize, or take more input formats, these are things I would rather leave "as an exercise for the the reader" :-) ...

Re: Converting anything to RGB correctly

Posted: 2010-07-04T17:08:29-07:00
by anthony
Albert25 wrote:Can you give me a hint on what specific aspect you feel needs "better script writing"? If something specific can make the script more portable, or if comments are unclear for you, or there is something else you think needs to be improved, I would be happy to use your advice.
It is more about having enough comments and documentation that someone who comes across the script will know what it does without needing to decode or run it. I add some extra comments in the version I have put online at convert_any2srgb. They are just suggestions that should be kept in mind when writing any type imagemagick script, to make things easier.
If it is to add more options than just resize, or take more input formats, these are things I would rather leave "as an exercise for the the reader" :-) ...
No problem. Basically don't restrict the user unless you have security concerns such as for web use.

Re: Converting anything to RGB correctly

Posted: 2013-07-15T17:41:11-07:00
by briandenks
Albert, Fred, Drarakel, snibgo ... its a very old board post but something exactly what im trying to establish in another post ..
Could you guys help me out with calling the script to achieve this ? Can it be called through IM ?
Im running linux server 3.2.0-48 server

Re: Converting anything to RGB correctly

Posted: 2013-07-15T19:02:06-07:00
by fmw42
What script? http://www.imagemagick.org/Usage/script ... t_any2srgb

If that is the script you want, you can call it from a unix terminal or from PHP using the exec() command.

Please give us a little more information about what exactly you are trying to do.

Re: Converting anything to RGB correctly

Posted: 2013-07-15T19:13:47-07:00
by briandenks
fmw42, thanks ! yes sorry i kept the detail low initially to re-instate the thread without trampling on it, apologies.
Im sorry i am cross posting here from <link>viewtopic.php?f=1&t=23608&p=100758#p100758<link> due to this being what we wish to achieve,
a simple solution to determining if any profile is present and to deal with the convert / resize / resample.
Our IM code is inbedded in coldfusion web app and i was looking for a way to call the script without having to use coldfusion, hence my question if IM could call a .sh ...

Bill

Re: Converting anything to RGB correctly

Posted: 2013-07-15T19:21:33-07:00
by fmw42
briandenks wrote:fmw42, thanks ! yes sorry i kept the detail low initially to re-instate the thread without trampling on it, apologies.
Im sorry i am cross posting here from <link>viewtopic.php?f=1&t=23608&p=100758#p100758<link> due to this being what we wish to achieve,
a simple solution to determining if any profile is present and to deal with the convert / resize / resample.
Our IM code is inbedded in coldfusion web app and i was looking for a way to call the script without having to use coldfusion, hence my question if IM could call a .sh ...

Bill
In general it is best to start a new thread in situations like this. If necessary you can link back to the original thread.

But since we got started, then lets just continue.

Again I am confused about what you want. IM does not access scripts itself. You can write scripts to contain IM commands, but it is the OS or the API that calls the script. For example on a Linux/Mac OS, you can open a terminal window and issue

bash scriptname

That will launch the script

in PHP you would use something like

exec("bash path2IMconvert/scriptname")

IM can look for a profile by using shell commands such as sed or grep from the verbose information

profile=`identify -verbose image | sed ....`

The exact command can be worked out if you need that.

Re: Converting anything to RGB correctly

Posted: 2013-07-15T19:31:50-07:00
by briandenks
Thanks Fred,

I would love to be able to feed an CMYK ( no profile) PSD or RGB PSD to IM and resize/resample thumbnail to png jpg gif tif bmap that doesnt look like its too bright.
currently we push a cmyk (untagged or in IM speak +profile) to IM and output to png jpg gif tif bmap using US webswop , the tif is EXACT the same as the input file, so is the gif. The png and jpg become very bright and clearly different from the original that was converted. Im not fussed on converting to sRGB on resize or resample thumbnail but ive had terrible color results when dong so ... It might be my script infact im sure it is .. but i do know that IM cannot think like a human when it isnt given the correct parameters from the start ( no profile psd) So it presumes on convert "ill just make this a non linear srgb png" .. result = color of course is out of whack, thats not IM's fault, its ours for not providing a profile within the original PSD that is given to it, we cannot change that part of our workflow, so im trying to work with IM to get the result we want ( color correct png tif gif jpg bmap) on resize or resample when a rgb.psd is given OR cmyk.psd is given.



I dont have clearance for images to show you but if you have a test.psd (rgb+cmyk) i can open and convert id be happy to show you ..

Re: Converting anything to RGB correctly

Posted: 2013-07-15T19:51:13-07:00
by fmw42
The PNG issue is likely like I said. It does not support CMYK. JPG should work fine, but there have been problem in IM and viewers differ when viewing CMYK jpg.

That is why I asked what version of IM you are using and what platform.

You should be able to download any image from the web and convert it to PSD using Photoshop. Then remove any profile and send back a link to it. You can post the image on any free image hosting service and just post a link here to it.

If you do not have PS then let me know and I can post one or just process it.

You appear to be adding USWebCoatedSwop CMYK profile to your results when you have a CMYK input. Is that correct? What do you do with an RGB PSD? Do you convert that also by adding a CMYK profile?

Please clarify what you do for each input PSD format. In what colorspace are you resizing? I would be concerned it you process in CMYK colorspace and get good results.

Re: Converting anything to RGB correctly

Posted: 2013-07-15T19:55:57-07:00
by fmw42
Lets just use this thread viewtopic.php?f=1&t=23608 from now on. Answer my post on that thread and lets continue there and discontinue here.

Re: Converting anything to RGB correctly

Posted: 2015-05-16T10:22:43-07:00
by Albert25
Just in case someone comes across this thread through Google, there is a newer version of the "any2srgb" script here: http://alma.ch/scripts/linux/any2srgb

Re: Converting anything to RGB correctly

Posted: 2017-05-08T11:24:03-07:00
by FrontierDK
I guess I'm one of those who know nothing about ICC...other than it's a pain.

If I use a simple convert to any other format (from dng) - the colours are all wrong, in comparison to what Photoshop shows.

Original test image here: https://bornholm.mobi/test.zip

If opened in Photoshop, it looks like this (OK):
Image

If converted, to any other image format, using either ImageMagic or Gflax SDK, I get this (not OK):
Image

Does anyone know how to make ImageMagic convert OK?

Re: Converting anything to RGB correctly

Posted: 2017-05-08T11:29:29-07:00
by fmw42
What is your IM version and platform and your exact IM command line. Please always provide this information.

Photoshop typically uses an Adobe RGB profile. IM typically uses sRGB. That could be the difference.