output file name

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?".
rrodriguez

output file name

Post by rrodriguez »

Hi,

In a convert command, is it possible to add a stub to the output file names related, for instance with its sizes?

For instance: having a folder with some 200 files, image001.tiff to image200.tiff, I would like to get image001w800.png to image200w800.png.

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

Re: output file name

Post by fmw42 »

see http://www.imagemagick.org/script/comma ... 7tj5d6#set

convert rose: -set filename:mysize '%wx%h' 'rose_%[filename:mysize].png'

You should be able to sue "%w" only if you want.

But note you are creating a new file, not just renaming it. Some one can correct me if I am wrong about this latter part.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: output file name

Post by anthony »

See output filename percent escapes...
http://www.imagemagick.org/Usage/files/#save_escapes

And yes convert created (or silently replaces) the file of the filename of the name given (or calculated).

"mogrify" is a limited type of "convert" command that replaces the input file with the processed file, unless directed to use a different output directory or suffix.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
rrodriguez

Re: output file name

Post by rrodriguez »

Thanks! I am able to convert a single file following your messages and the links included, but I'm not able yet to understand how do the same with the whole content of a given folder. Let's say I've a folder with n pictures, image_1.tiff to image_n.tiff and want to get image_1_800x600.png to image_n_800x600.png. I am able to transform one file, for instance:

convert P1020159w1000.JPG -set filename:mysize '%wx%h' 'P1020159w1000_%[filename:mysize].png'

But, please, what is the right syntax to transform and add that text stub each and all files in a given folder?

I'm clearly failing at understanding how to reference a series of files.

Thanks for your help!

Ricardo
lwhistler
Posts: 23
Joined: 2010-09-15T15:33:29-07:00
Authentication code: 8675308

Re: output file name

Post by lwhistler »

rrodriguez wrote: But, please, what is the right syntax to transform and add that text stub each and all files in a given folder? I'm clearly failing at understanding how to reference a series of files.
If you are on Linux - such as Ubuntu - use an executable bash script and run the convert command in a loop. The code below is untested and there might be syntax errors, but it should get you started on looping through files in a folder.

file.sh

Code: Select all

#!/bin/bash
for image_file in *.JPG
do
convert $image_file -set filename:mysize '%wx%h' '[filename:mysize].png'
done
rrodriguez

Re: output file name

Post by rrodriguez »

Thanks!

I've read a number of references talking about sh scripts, but as far as I understand, it must be possible to reference a bunch of files by using just ImageMagick commands. But I don't know how neither!

As for your proposal: I'm not able to pass $image_file as stub for the first part of the output file. It reads now:

Code: Select all

#!/bin/bash
for image_file in *.JPG
do
convert $image_file -set filename:mysize '%wx%h' '$image_file_%[filename:mysize].png'
done
And I get:

Code: Select all

GMXUX-Ricardo-Rodriguez:cumpleCaiCai2010 rrodriguez$ ls -l *.png
-rw-r--r--  1 rrodriguez  staff  1851726 29 sep 01:07 $image_file_1333x1000.png
-rw-r--r--  1 rrodriguez  staff  2302865 29 sep 01:07 $image_file_1498x1000.png
-rw-r--r--  1 rrodriguez  staff   977684 29 sep 01:07 $image_file_750x1000.png
Just one file for each size. Of course each of them only has the content of the last transformed picture.

Please, do you know how must I modify the convert line to pass the name of each original file (image_file) to the output file name?

Thanks!
Last edited by rrodriguez on 2010-09-28T17:15:06-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: output file name

Post by fmw42 »

This works for me. I think the missing thing was the brackets.

Here I have 3 files in directory test1 and none to start in directory test2. I take the files from test1 and rename them into test2


lena.jpg
lena2.jpg
lena3.jpg

#!/bin/bash
cd test1
filelist=`ls | grep '.jpg'`
for image_file in $filelist
do
convert $image_file -set filename:mysize "%wx%h" ../test2/${image_file}_%[filename:mysize].png
done

results in
lena.jpg_256x256.png
lena3.jpg_128x128.png
lena2.jpg_128x128.png

in test2
rrodriguez

Re: output file name

Post by rrodriguez »

Brackets in, quotation marks out! Following the last post by fmw42, this also works for me now:

Code: Select all

#!/bin/bash
for image_file in *.JPG
do
convert $image_file -set filename:size '%wx%h' ${image_file}_%[filename:size].png
done
Please, why must I call the name of a file in three different way depending on the context: image_file in the "for" statemente, $image_file as input file in convert, and ${input_file} in the output filename variable. It will be great to understand this!

Thanks all for your great help!
Last edited by rrodriguez on 2010-09-29T00:06:01-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: output file name

Post by fmw42 »

rrodriguez wrote:Brackets in, quotation marks out! Following the last post by fmw42, this also works for me now:

Code: Select all

#!/bin/bash
for image_file in *.JPG
do
convert $image_file -set filename:size '%wx%h' ${image_file}_%[filename:size].png
done
Please, why must I call the name of a file in three different way depending on the context: image_file in the "for" statemente, $image_file as input file in convert, and ${input_file} in the output filename variable. It will be great to understand this!

Thanks all for your great help!
The quotes probably can be used. It was the missing brackets.

The image_file in the for statement, just assigns each file from *.JPG to the variable image_file.

The $image_file at the beginning of the convert uses the variable with each of your images as input to the convert.

The ${image_file}_ etc at the end assigns that name to the output.

But note, as it is, it also includes the .jpg in the name.


This is better:

#!/bin/bash
cd test1
filelist=`ls | grep '.jpg'`
for image_file in $filelist
do
inname=`convert $image_file -format "%t" info:`
convert $image_file -set filename:mysize "%wx%h" ../test2/${inname}_%[filename:mysize].png
done


Which then makes
lena2_128x128.png
lena3_128x128.png
lena_256x256.png

inname=`convert $image_file -format "%t" info:`

just gets the name part of the file without the suffix and assigns it to $inname variable.

see string format t at
http://www.imagemagick.org/script/escape.php
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: output file name

Post by anthony »

Also see
http://www.imagemagick.org/Usage/basics/#mogrify_not

Mogrify can not be used when also renaming the output file.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
CptWacko

Re: output file name

Post by CptWacko »

I got another question about the files output names...

Is it possible to use more than 1 digit for the image count ?

When converting a pdf to jpg, I'd like to have the files named image0001, image0002, etc. instead of image1, image2, etc.

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

Re: output file name

Post by fmw42 »

use _%04d

convert rose: rose: rose: tmp_%04d.png

generates:

tmp_0000.png
tmp_0001.png
tmp_0002.png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: output file name

Post by anthony »

fmw42 wrote:#!/bin/bash
cd test1
filelist=`ls | grep '.jpg'`
for image_file in $filelist
do
inname=`convert $image_file -format "%t" info:`
convert $image_file -set filename:mysize "%wx%h" ../test2/${inname}_%[filename:mysize].png
done
You could include the %t in the 'filename:mysize' setting :-)

Code: Select all

convert "$image_file" -set filename:myname '%t_%wx%h' "../test2/$[filename:myname].png"
I recommend quotes around the output filename to stop the shell trying to evaluate the $[...]
I also recommend quota around the input filename incase the filename has spaces in it!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: output file name

Post by anthony »

Unfortunately there is no string formatting for the % escapes :-(

As such %wx%h will not have any leading zeros
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
CptWacko

Re: output file name

Post by CptWacko »

Thanks a lot for the replies ! :D
Post Reply