Make ImageMagick an external hard drive for temporary files
I want to use ImageMagick convert to add many PNGs to one huge PNG.
When I do this on a PC with a lot of free memory it works, but I need to do it on my laptop now and there is not enough space. I also tried to define -limit memory
, -limit space
etc., but there is not enough space.
How can I set the path to temporary ImageMagick files on an external hard drive?
source to share
The ImageMagick command line tools recognize the following environment variable:
MAGICK_TEMPORARY_PATH
The path for storing temporary files is set here. It is valid for all commands run during the setting of this variable.
An alternative method is to add -define registry:temporary-path=...
to a specific command, for example:
convert \
-define registry:temporary-path=/Volumes/external/tmp \
wizard: \
-resize 250000x250000 \
wizard-250000x250000px.miff
Then it is only valid for that single command. (BTW, this wizard:
is a built-in sample in ImageMagick - you can always use it as an example to test ImageMagick commands if you play with them.)
To prevent large images from consuming all the memory on your system, you can force the image pixels to a resource constrained memory mapped disk by adding -limit memory ...
:
convert \
-define registry:temporary-path=/Volumes/external/tmp \
-limit memory 16mb \
wizard: \
-resize 250000x250000 \
wizard-250000x250000px.miff
You can even force all image pixels to disk using -limit area 0
:
convert \
-define registry:temporary-path=/Volumes/external/tmp \
-limit area 0 \
wizard: \
-resize 250000x250000 \
wizard-250000x250000px.miff
However, keep this in mind: caching pixels to disk is orders of magnitude slower than using RAM. The coefficient is around 1000! (The SSD is faster, of course.) Expect seconds, which otherwise takes milliseconds and hours, which usually takes minutes ...
For these cases, it is very useful to follow -monitor
the processing flow:
convert \
-monitor \
-limit memory 1GiB \
-limit map 2GiB \
-define registry:temporary-path=/Volumes/external/tmp \
wizard: \
-resize 250000x250000 \
logo-250000x250000px.miff
The add-ons -monitor ...
will display a dynamically changing progress bar on your terminal saying something like
resize image[WIZARD]: 24999 of 43750, 57% complete
Setting -limit
can be applied to area
, disk
, file
, map
, memory
, thread
or time
. file
sets the number of files area
, memory
and map
sets the number of bytes (SI prefixes are allowed), time
- in seconds.
You can request an ImageMagick installation for the current (or default) settings by running
identify -list resource
I get:
File Area Memory Map Disk Thread Throttle Time
-------------------------------------------------------------------------------
192 4.295GB 2GiB 4GiB unlimited 1 0 unlimited
There are -limit
also corresponding environment variables for command line parameters . It:
MAGICK_AREA_LIMIT
MAGICK_DISK_LIMIT
MAGICK_FILE_LIMIT
MAGICK_MEMORY_LIMIT
MAGICK_MAP_LIMIT
MAGICK_THREAD_LIMIT
MAGICK_TIME_LIMIT
They limit resources for used image area, disk space, open files, heap memory, memory card, number of threads of execution, and maximum elapsed time in seconds.
source to share