Correct Use of MPR

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
bigd14usa
Posts: 2
Joined: 2014-02-23T22:32:17-07:00
Authentication code: 6789

Correct Use of MPR

Post by bigd14usa »

Hello,

I am new to Imagemagick and before I run a batch process I wanted to make sure I am using/understand MPR correctly.

Basic goal:
1. Load a source image and crop it to its destination aspect ratio
2. Load an overlay image and composite the two
3. Resize and write to a file
4. Repeat

I currently have the following as a command:

convert original.jpg -write mpr:src +delete overlay.jpg -write mpr:overlay +delete \
mpr:src {image conversion options} -write mpr:temp
mpr:overlay {image conversion options} -composite -write mpr:temp
{image conversion options} -write new_file_name.jpg +delete

My concern is with making sure I'm releasing the memory properly, specifically mpr:temp as +delete is never called on it. Does it need to be freed at the very end?

Forums references:
http://stackoverflow.com/questions/6584 ... rt-command
http://www.multipole.org/discourse-serv ... =1&t=20639

Here is the really long command version if anyone is interested:
convert ${src} -write mpr:src +delete ${overlay} -write mpr:overlay +delete \\
mpr:src -interpolate bicubic -filter Lagrange -gravity center -crop ${crop_w}x${crop_h}+0+0 +repage -write mpr:temp \\
mpr:overlay -gravity southeast -geometry ${overlay_w}x${overlay_h}+37+74 -composite -write mpr:temp \\
-interpolate bicubic -filter Lagrange -resize 75% +repage -unsharp 0.4x0.4+0.4+0.008 -resize ${dst_w}x${dst_h}! +repage -unsharp 0.4x0.4+0.4+0.008 -unsharp 0.4x0.4+0.4+0.008 -density 72x72 +repage -quality 100 -write WxH.jpg +delete

Thanks in advance for any help!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Correct Use of MPR

Post by fmw42 »

convert original.jpg -write mpr:src +delete overlay.jpg -write mpr:overlay +delete \
mpr:src {image conversion options} -write mpr:temp
mpr:overlay {image conversion options} -composite -write mpr:temp
{image conversion options} -write new_file_name.jpg +delete

+delete frees the original disk image from memory and leaves the MPR image around instead. So you only need to free the original image once. You do not have to free the MPR image, since it is freed at the end of the command automatically. I am not sure you can even free it using +delete. You do not need to -write the output. Just the output filename is fine.

You do not have to use MPR. You can just use -clone and/or parenthesis processing if you want. I do not think you are really saving memory or much speed by using MPR instead of the loaded image.

see
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/basics/#list_ops

Most of the time you do not need to use MPR. Just a few cases where it is needed or helpful. For example tiling. See http://www.imagemagick.org/Usage/canvas/#tile_memory
http://www.fmwconcepts.com/imagemagick/ ... und_shadow

It is useful when you need to use the same image over and over. You can also read an image into MPC format (memory mapped) and use that over and over. Once the image is converted to mpc format, reading it again and again is very fast. But it takes extra time to write to it the first time or any time you write over it.
bigd14usa
Posts: 2
Joined: 2014-02-23T22:32:17-07:00
Authentication code: 6789

Re: Correct Use of MPR

Post by bigd14usa »

Thank you for your quick response and the clarification!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Correct Use of MPR

Post by fmw42 »

Your original command can be done as follows

convert background foreground -compose over -composite -resize XXxYY result

or


convert background foreground -compose over -composite \
\( -clone 0 -resize XX1xYY1 -write result1 \) \
\( -clone 0 -resize XX2xYY2 -write result2 \) \
...
\( -clone 0 -resize XXnxYYn -write resultn \) \
null:


The null:, just throws away all the temps from the clones. The result from the -composite, becomes the 0th image in the chain, each clone afterwards adds one more image to the chain. That is why they need to be delete or use null: to get rid of them all.

see also
http://www.imagemagick.org/Usage/layers/#convert
Post Reply