resize and rename a folder of images

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
geomcd
Posts: 3
Joined: 2012-11-20T09:06:12-07:00
Authentication code: 6789

resize and rename a folder of images

Post by geomcd »

Using Ubuntu Linux 12.04 LTS, I want to resize a folder of images and also rename those images in sequence. That is, the folder contains (only) images numbered in sequence named IMG_0545.jpg to IMG_0742.jpg. The images are all 4288x2848. I want to resize them all to 1000x664, and rename them, sequentially, that is, campus1.jpg, campus2.jpg, etc.

Thanks very much.

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

Re: resize and rename a folder of images

Post by fmw42 »

I think you will have to write a script loop to loop over all the files in the directory and use convert to resize and rename them. Mogrify will resize them just fine, but I do not believe you can renumber them in that tool.

If you have too many large files, then convert may not work well as it may have to swap to disk if you do not have enough memory to hold them all and their resized images. Convert always loads all the images first.

Mogrify is better as it process each image one at a time. But I do not know how you would automatically renumber them.

Thus to avoid loading all the images in memory with convert, you need to script a loop over all the files you want so that it does one convert for each image.

something like

cd to directory
list=`ls`
i=1
for image in $list; do
convert $image -resize 1000x664 campus$i.jpg
i=$((i+1))
done

Note however if the input image aspect ratio is not the same as 1000:664, then you will not get exactly 1000x664. But your 4288x2848 does seem to have the same aspect ratio. So you should be fine.

see
http://www.imagemagick.org/Usage/basics/#mogrify
http://www.imagemagick.org/Usage/basics/#mogrify_not
http://www.imagemagick.org/script/comma ... p#geometry
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: resize and rename a folder of images

Post by glennrp »

I would create a simple shell script to do the renaming and numbering, and after running that, use "mogrify -resize WxH *.jpg" to resize them.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: resize and rename a folder of images

Post by anthony »

renumbering file is best done using a different tool.

For example I have a script "mv_renum" which replaces numbers in filenames with a fix length numbers.
By default the longest number of digits found, but yu can specify number of digits with an option.

If the same program is named (or link from) "mv_reseq" it will re-order the files to remove number gaps.
or leave regular gaps (so say all file numbers increment by 5). It does this so as to not overwrite any files.

Another 'perl' based script "mv_perl" can do similar (but less versite with respect to numbers) but it can do a lot more such as: substitute strings, lowercase, uppercase, replace spaces capitalise words, space out words, etc.

See my software export
http://www.ict.griffith.edu.au/anthony/ ... /#mv_renum

Of course this assumes a UNIX-like environment (linux, mac, cygwin)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply