Append 2 images and generate result filename from source's filename

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?".
Locked
chavecat
Posts: 2
Joined: 2020-02-20T19:30:27-07:00
Authentication code: 1152

Append 2 images and generate result filename from source's filename

Post by chavecat »

I'm using this command to join 2 images together. It works perfectly.

Code: Select all

magick convert +append 014.jpg 013.jpg 013and014.jpg
But I would like to know if there's a way to get the filenames from the two source images and put them into the result filename automatically, that way I would just change the two source filenames and not have to also change the result's filename, that way I can save a little time when using this command repeatedly. Something like "image1.jpg" and "image2.jpg" would generate "image1andimage2.jpg".

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Append 2 images and generate result filename from source's filename

Post by fmw42 »

Your syntax is not proper for IM 7, it should be

Code: Select all

magick 014.jpg 013.jpg +append 013and014.jpg
For IM 7, use magick, not magick convert. Syntax should read the input images first, then the operator.

I believe you can only use one name automatically via -set filename (the first image in the list)

Code: Select all

magick 014.jpg -set filename:fn "%t" 013.jpg +append "%[filename:fn]_and014.jpg
The following does not work

Code: Select all

magick 014.jpg -set filename:fna "%t" 013.jpg -set filename:fnb "%t" +append "%[filename:fna]_and_%[filename:fnb].jpg

User avatar
GeeMack
Posts: 757
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Append 2 images and generate result filename from source's filename

Post by GeeMack »

chavecat wrote:
2020-02-20T19:44:22-07:00
Something like "image1.jpg" and "image2.jpg" would generate "image1andimage2.jpg".
With IMv7 you can build the output filename with a command like this...

Code: Select all

magick image1.jpg image2.jpg ^
   +swap -set option:f2 %[t] +swap ^
   -set filename:f %[t]and%[f2] +append %[filename:f].png
That reads in the two images and swaps their order to put the second file first in the list. Next it sets a variable "f2" to the filename of what is now the first file "%[t]". After it swaps the images back to the correct order, it assembles an output filename "filename:f" using the first image's filename "%[t]" and that "f2" variable. Finish by appending the images and writing the output file.

That command is in Windows syntax. For *nix change the continued-line carets "^" to a backslashes "\".
Last edited by GeeMack on 2020-02-20T22:28:02-07:00, edited 1 time in total.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Append 2 images and generate result filename from source's filename

Post by snibgo »

As GeeMack says. Another method, Windows CMD syntax:

Code: Select all

magick ^
  ( toes.png -set option:FIL1 %t ) ^
  ( toes_holed.png -set option:FIL2 %t ) ^
  -set filename:MYFILE %[FIL1]_and_%[FIL2] ^
  +append ^
  %[filename:MYFILE].png
The output file is named "toes_and_toes_holed.png".
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Append 2 images and generate result filename from source's filename

Post by fmw42 »

Very clever, you guys.

chavecat
Posts: 2
Joined: 2020-02-20T19:30:27-07:00
Authentication code: 1152

Re: Append 2 images and generate result filename from source's filename

Post by chavecat »

Didn't notice the syntax was wrong since it worked. Woops.
Both those solutions posted worked great, thanks.

Locked