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

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
pipitas
Posts: 168
Joined: 2012-07-15T14:06:46-07:00
Authentication code: 15

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

Post 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.
pipitas
Posts: 168
Joined: 2012-07-15T14:06:46-07:00
Authentication code: 15

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply