Help needed with implementing color profile support
Posted: 2015-06-11T04:26:09-07:00
Hello!
I wish to implement color profile support in a simple bash script of mine which makes use of ImagMagick's convert.
The script currently looks as follows:
and you may assume that "$InputDir/$Img" is an URI pointing to an existing image in portrait orientation (2848x4288) and that "$sRGBColorProfile" is an URI pointing to a generic sRGB ICC profile.
I have already consulted the documentation about how one should deal with color profiles and have come up with a solution (attempt), where I am replacing the convert-call with the following:
Now the following should happen:
If the image has no embedded profile, it will simply assign one without changing the pixels' values. It will then convert to linear RGB and do an upscaling transformation inspired by http://www.imagemagick.org/Usage/filter/nicolas/ (there is also some cropping and extending going on, but I do not think I have to explain the purpose of this for my question. The fact that those commands are present might be part of the problem, however) and then convert to the generic sRGB profile afterwards.
If, on the other hand, the image has an embedded profile, the first profile call will convert the pixels' values to what they should look like given the generic sRGB profile (in order to approximate what I get by -colorspace sRGB; as far as I understand, if I omit this step, the conversion to the linear RGB space should produce faulty values) and only then convert to linear RGB color space and proceed with the same transformations as above. Afterwards, it will convert from linear RGB color space back to the original color profile.
What happens in practice, is that the image will loose its color profile. However, a minimal example, where I am omitting the cropping, upsampling and extent-call, does work as expected.
During further research in the forums, I found out that one has to be careful with the command line options and should adhere to the structure described in http://www.imagemagick.org/script/comma ... essing.php. According to this page, the two +repage settings, are illegal, since they should be given before the operators. However, they have to be there for the script to work. And while I have encountered examples in the documentation that differ from the anatomy described in the above link (for example the -fill setting in the first example given in this very linked page), I do believe that this is the root cause of the problem.
In an attempt to solve the problem, I then tried to cluster the different parts (cropping, upsampling, extent-call) using composite (http://www.imagemagick.org/Usage/compose/), however I was not able to come up with a working solution. (That was some time ago and sadly I did not save my attempts, so I have nothing to show.)
I was hoping that one of you might be able to help me identify and solve the problem?
I wish to implement color profile support in a simple bash script of mine which makes use of ImagMagick's convert.
The script currently looks as follows:
Code: Select all
local ImgColorSpace=$(identify -format "%[colorspace]" "$InputDir/$Img")
local ImgColorProfile="$sRGBColorProfile"
if [ -n "$(identify -format "%[profile:icc]" "$InputDir/$Img")" ]; then
convert "$InputDir/$Img" "$InputDir/$Img.icc"
ImgColorProfile="$InputDir/$Img.icc"
elif [ -n "$(identify -format "%[profile:icm]" "$InputDir/$Img")" ]; then
convert "$InputDir/$Img" "$InputDir/$Img.icm"
mv "$InputDir/$Img.icm" "$InputDir/$Img.icc"
ImgColorProfile="$InputDir/$Img.icc"
fi
# Call to convert:
convert "$InputDir/$Img" \
-quality 100% \
-depth 16 \
-colorspace RGB \
+sigmoidal-contrast 6.5 \
-gravity Center -crop 'x4272+0+0>' +repage \
-filter Mitchell -resize 'x4731<' \
-background White -gravity Center -extent '3197x4803' +repage \
-sigmoidal-contrast 6.5 \
-colorspace "$ImgColorSpace" \
"$InputDir/$Img.tmp"
I have already consulted the documentation about how one should deal with color profiles and have come up with a solution (attempt), where I am replacing the convert-call with the following:
Code: Select all
convert "$InputDir/$Img" \
-quality 100% \
-depth 16 \
-profile "$sRGBColorProfile" \
-colorspace RGB \
+sigmoidal-contrast 6.5 \
-gravity Center -crop 'x4272+0+0>' +repage \
-filter Mitchell -resize 'x4731<' \
-background White -gravity Center -extent '3197x4803' +repage \
-sigmoidal-contrast 6.5 \
-profile "$ImgColorProfile" \
"$InputDir/$Img.tmp"
If the image has no embedded profile, it will simply assign one without changing the pixels' values. It will then convert to linear RGB and do an upscaling transformation inspired by http://www.imagemagick.org/Usage/filter/nicolas/ (there is also some cropping and extending going on, but I do not think I have to explain the purpose of this for my question. The fact that those commands are present might be part of the problem, however) and then convert to the generic sRGB profile afterwards.
If, on the other hand, the image has an embedded profile, the first profile call will convert the pixels' values to what they should look like given the generic sRGB profile (in order to approximate what I get by -colorspace sRGB; as far as I understand, if I omit this step, the conversion to the linear RGB space should produce faulty values) and only then convert to linear RGB color space and proceed with the same transformations as above. Afterwards, it will convert from linear RGB color space back to the original color profile.
What happens in practice, is that the image will loose its color profile. However, a minimal example, where I am omitting the cropping, upsampling and extent-call, does work as expected.
During further research in the forums, I found out that one has to be careful with the command line options and should adhere to the structure described in http://www.imagemagick.org/script/comma ... essing.php. According to this page, the two +repage settings, are illegal, since they should be given before the operators. However, they have to be there for the script to work. And while I have encountered examples in the documentation that differ from the anatomy described in the above link (for example the -fill setting in the first example given in this very linked page), I do believe that this is the root cause of the problem.
In an attempt to solve the problem, I then tried to cluster the different parts (cropping, upsampling, extent-call) using composite (http://www.imagemagick.org/Usage/compose/), however I was not able to come up with a working solution. (That was some time ago and sadly I did not save my attempts, so I have nothing to show.)
I was hoping that one of you might be able to help me identify and solve the problem?