Page 1 of 1

Inserting '-write some.jpg' statement into long command line changes final result

Posted: 2014-11-13T07:29:13-07:00
by pipitas
I'm preparing an ImageMagick workshop aimed to introduce inexperienced IM users to some more advanced techniques. So, I'm trying to demo how one could put '-write intermediate.jpg' in the middle of a long command line in order to...
  1. ...inspect intermediate results of the command pipeline;
  2. ...help understand what happens on a long command line;
  3. ...provide some sort of "debugging" technique when one constructs one's own commands.
However, I found that this may change the end result, though in theory it shouldn't. Here is the command I'm using right now (taken from the Fred's "tidbits" site) -- reformatted for better readability:

Code: Select all

convert                     \
    -verbose                \
    -size 30x600            \
     xc:"rgb(100%,0%,0%)"   \
    \    
    -colorspace hsl         \
    gradient:               \
    \    
    -compose CopyRed        \
    \    
    -composite              \
    \    
    -colorspace RGB         \
    -rotate 90 \ 
     rainbow1.jpg
This works, and 'rainbow1.jpg' indeed displays the expected rainbow colors:

Image

When I insert '-write ...' commands into the same command, like in the following example, the end result changes:

Code: Select all

convert                     \
    -verbose                \
    -size 30x600            \
     xc:"rgb(100%,0%,0%)"   \
                          -write intermediate-xc-red.jpg               \
    -colorspace hsl         \
     gradient:              \
                          -write intermediate-colorsp-hsl-gradient.jpg \
    -compose CopyRed        \
                          -write intermediate-compose-copyred.jpg      \
    -composite              \
                          -write intermediate-composite.jpg            \
    -colorspace RGB         \
                          -write intermediate-colorsp-rgb.jpg          \
    -rotate 90 \ 
     rainbow2.jpg
The result looks like a black-red-gradient:

Image

The end-result 'rainbow2.jpg' looks identical -- apart from the 90° rotation -- to the intermediate files 'intermediate-rainbow-colorsp-rgb.jpg' and 'intermediate-rainbow-composite.jpg' (though closer inspection with the help of 'compare' does show a few pixels do differ in between the 3 files).

I'm somehow stunned. I may well have overlooked something very obvious and committed some error. But I don't see it...

Or did I stumble over a bug?

----

This is a MacPorts-based installation on Mac OSX 10.9.5:

Code: Select all

$> convert -version
Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-10-03 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib cairo djvu fftw fontconfig freetype gslib gvc jbig jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png ps rsvg tiff webp wmf x xml zlib
P.S.: I also changed output formats to PNG, which didn't make a difference.

Re: Inserting '-write some.jpg' statement into long command line changes final result

Posted: 2014-11-13T08:09:28-07:00
by pipitas
Never mind: Found out now that I have to use '+write ...' (instead of '-write ...') in order to enforce the restoration of each image after it is written...

Still not sure what exactly happens when using '-write' though.

After all, the IM website says this:
The image sequence preceding the -write filename option is written out, and processing continues with the same image in its current state if there are additional options.

Re: Inserting '-write some.jpg' statement into long command line changes final result

Posted: 2014-11-13T09:35:46-07:00
by snibgo
I generally use +write, not -write. I would go as far as to say that -write should never be used.

+write XX is equivalent to, umm, ( -clone 0--1 -write XX -delete 0--1 )

Without investigating thoroughly, I think the problem in your example is HSL colorspace. Neither JPG nor PNG can store images in HSL, so they are converted to sRGB. When you use -write, the image(s) that are saved are converted to sRGB. +write effectively makes a copy, and does any required changes only to that.

Writing to .miff wouldn't (I think) have this problem.