Home > Blockchain >  Automated Script to Rename and Resize jpg Images
Automated Script to Rename and Resize jpg Images

Time:05-13

I have a bit of a difficult problem to solve.

I have a folder that contains a large amount of image files, and new image files are added daily (all JPG) that are named in a way that ensures there are no duplicates. The file names never change.

I need a way to have three separate copies of each image placed into a new folder, whereby the first copy is 100x100px, the second 200x200px and the third 400x400px, and lastly I need each image of a different size to be named 100-filename.jpg, 200-filename.jpg and 400-filename.jpg respectively.

I'm using Windows 11, and also aspect ratio isn't a problem as they are all square images and cropped correctly.

I know this could be done using a script and possibly Imagemagick/Irfanview, but I am unsure how I would set it up so that it is fully automated, and exactly how I'd do such a thing, being new to scripting.

The only things I could find were how to do it for one image rather than all images in a folder, and the other didn't suggest how I could also rename the files respectively.

Any help would be greatly appreciated.

Thanks!

CodePudding user response:

In Imagemagick 6 on Windows, you can do a FOR loop over each file in your folder and do:

convert image.suffix ^
(  clone -resize 400x400  write path_to_folder1/image.suffix ) ^
(  clone -resize 200x200  write path_to_folder2/image.suffix ) ^
(  clone -resize 100x100  write path_to_folder3/image.suffix ) ^
null:

Which will resize successively from the previously resize version

Or

convert image.suffix ^
( -clone 0 -resize 400x400  write path_to_folder1/image.suffix ) ^
( -clone 0 -resize 200x200  write path_to_folder2/image.suffix ) ^
( -clone 0 -resize 100x100  write path_to_folder3/image.suffix ) ^
null:

which will resize from the original for each size.

For Imagemagick 7, replace "convert" with "magick" and you can then make the output automatic.

magick path_to_infolder/image.suffix ^
-set filename:name "%t" ^
( -clone 0 -resize 400x400  write "path_to_outfolder/400-%[filename:name].jpg" ) ^
( -clone 0 -resize 200x200  write "path_to_outfolder/200-%[filename:name].jpg" ) ^
( -clone 0 -resize 100x100  write "path_to_outfolder/100-%[filename:name].jpg" ) ^
null:
  • Related