Naming issue when converting multiple files

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
kansandhaus
Posts: 1
Joined: 2014-05-14T19:56:25-07:00
Authentication code: 6789

Naming issue when converting multiple files

Post by kansandhaus »

Hello ImageMagick forum, I am experiencing a strange problem. I have a directory of tiff files: 'tiff2'. For each tiff in this directory, I want to create two thumbnails, one of height 360px, one of height 240px and store them in an output directory 'resize'. The input directory contains 4 files.

Code: Select all

PA00001.tiff  PA00002.tiff  PA00003.tiff  PA00004.tiff


I run the command given below on.

Code: Select all

convert \
	tiff2/*.tiff \
	-define registry:temporary-path=/mnt/magick-tmp \
	-limit memory 4GiB \
	-colorspace rgb \
	+profile '*' \
	-filter triangle\
	-set pagename "%t" \
	-write mpr:'%[pagename]' +delete \
	mpr:'%[pagename]' \
		-resize x360 \
		-set filename:title '%t_%h' \
		-write 'resize/%[filename:title].png' +delete \
	mpr:'%[pagename]' \
		-resize x240 \
		-set filename:title '%t_%h' 'resize/%[filename:title].png'
And the output directory contains the following files.

Code: Select all

PA00001_240.png  PA00002_240.png  PA00003_240.png  %[pagename]_240.png
PA00001_360.png  PA00002_360.png  PA00003_360.png  %[pagename]_360.png
It seems that the %[pagename] variable is not being appropriately unpacked for the final image (alphabetically) in my input directory.

Is this expected behavior? Have I discovered a bug? Should I be doing something different?

I'm using the following version of ImageMagick

Code: Select all

Version: ImageMagick 6.8.7-10 Q16 x86_64 2014-05-11 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC OpenCL OpenMP
Delegates: bzlib djvu fontconfig freetype jng jp2 jpeg lcms lqr openexr pangocairo png tiff x xml zlib
Thanks so much for your help!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Naming issue when converting multiple files

Post by snibgo »

I think the command doesn't do what you think it does.

The line "tiff2/*.tiff \" reads all the tiff files, in this case four. So you now have a list of four images in memory. Following commands then operate on all 4 images, down to "-write mpr:'%[pagename]' " which saves the 4 images into registers.

The next operation "+delete" removes the last image (just one image) from the list of four, so your list has 3 images.

"mpr:'%[pagename]' \" reads just a single image from mpr. So now your list has 4 images again: the first 3 from tiff files, the 4th from mpr. The first 3 have filenames; the 4th doesn't.

Hence the problem.

What is the solution? I wouldn't read in all the images at once, because my image tend to be large. I would use a shell loop to process each image separately. But you can do them all together. You don't need mpr. Just clone the entire list for the first resize and write, like this:

Code: Select all

%IM%convert ^
  *.jpeg ^
  ( -clone 0--1 ^
    -resize x360 ^
    -set filename:title %%t_%%h ^
    -write resize/%%[filename:title].png ^
    -delete 0--1 ^
  ) ^
  -resize x240 ^
  -set filename:title %%t_%%h ^
  resize/%%[filename:title].png
(Sorry, that is Windows BAT syntax. For Unix, change each %% to % and end-of-line ^ to \ .)

"-clone 0--1" means "clone images from number 'zero' to number 'minus one'", in other words "clone all the images". Likewise for "-delete".
snibgo's IM pages: im.snibgo.com
Post Reply