1
votes

I'm using GIMP to do a simple but tedious operation for many images so I'd like to know how I can make a script that will do it for me, or find some simpler way of doing this.

I import an image file that has large dimensions but is mostly square, sometimes like 550x564, 580x596, etc., which may be jpg, bmp, or png, and I want to export a 192x192 PNG file, resized and cropped on both ends of the longer dimension, which is usually vertical but can be horizontal.

I perform the following steps for an image that is longer vertically than it is horizontally:

  1. Image->Scale Image->Set Width to 192 (Locked, so Height will automatically be set to something like 196)
  2. Click Scale
  3. Image->Canvas Size->Set Height to 192
  4. Click Center
  5. Click Resize
  6. File->Export As->same file name but extension changed to .png, and PNG image selected from the dropdown list (usually the original files are jpg, but sometimes they are png, so overwrite if necessary)
  7. Click Export (Click Load Defaults)
  8. Click Export again

How can I do this in script-fu or python for GIMP? Or in ImageMagick? Or is there a better way of making a macro for this?

1
Did my answer sort out your problem? If so, please consider accepting it as your answer - by clicking the hollow tick/checkmark beside the vote count. If not, please say what didn't work so that I, or someone else, can assist you further. Thanks. meta.stackexchange.com/questions/5234/…Mark Setchell

1 Answers

2
votes

I think I understood you. Try this with ImageMagick to convert a single image:

convert input.jpg -gravity center -resize 192x192^ -extent 192x192 -set filename:base "%[basename]" "%[filename:base].png"

You could also potentially leverage the power of mogrify to do a whole directory full of images in one go, but be very careful, and try it out on a COPY of your files somewhere away from your main work. That will actually be easier, because you can let mogrify work out the filenames itself and you don't need a loop:

# Process all JPEGs in current directory
mogrify -format png -gravity center -resize 192x192^ -extent 192x192 *jpg

If you are on Windows, you may need to escape the caret (^), I don't use Windows, but I think you will either need to add an extra caret before it, or maybe enclose the whole -resize parameter in double quotes:

mogrify ... -resize "192x192^" ...

Note that if you are on ImageMagick v7+, mogrify ... becomes magick mogrify ...