0

I am running imagemagick (CLI tool) to splice an image into 16 equal parts.

I am using it like so :

convert inputfile.jpg -crop 4x4@ +repage +adjoin -resize 256x256 output_4x4_%03d.jpg

I'd like to keep the name of the original file, inputfile.jpg, and use that as the output instead of "output_4x4..." automatically i.e without having to change it every time, so I can run this automatically on a bunch of files, and keep their original names.

Is this possible?

Thanks.

migrated from stackoverflow.com Dec 20 '18 at 16:16

This question came from our site for professional and enthusiast programmers.

0

In ImageMagick 6 and 7, you need to get the name first.

name=$(convert inputfile.jpg -format "%t" info:) 
convert inputfile.jpg -crop 4x4@ +repage +adjoin -resize 256x256 ${name}_4x4_%03d.jpg


One might think you could use the -set filename option as for example:

convert inputfile.jpg -set filename:fn "%t" -crop 4x4@ +repage +adjoin -resize 256x256 "%[filename:fn]_4x4_%03d.jpg"


But this will not resolve the %03d. It will give results such as

inputfile_4x4_%03d.0.jpg
inputfile_4x4_%03d.1.jpg
inputfile_4x4_%03d.2.jpg
inputfile_4x4_%03d.3.jpg


If you remove the _%03d, you will simply get

inputfile_4x4.jpg


At least that is what happens on my Mac.

  • 1
    If you set the filename with "-set filename:fn '%t'" after the crop, and write the final output files like "%[filename:fn]%02d.png", it -almost- works. An apparent bug truncates the original input name. However, if you put the number first like "%03d%[filename:fn].png", the multiple output files are named as expected. – GeeMack Dec 21 '18 at 1:45
  • Greg, that is interesting. It does indeed work if you put the %03d at the beginning. I tried "%03d_%[filename:fn].png" and that does work. But odd that it won't work at the end even if one does _%[filename:fn]_%03d.png, which produces names like _lena_%03000.png – fmw42 Dec 21 '18 at 2:02

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.