Menu

Image transformation batch processing

Help
2015-05-10
2015-05-10
  • Lloyd Borrett

    Lloyd Borrett - 2015-05-10

    G'day,

    I have many directories of images, totaling more than 5000 images in total.
    Each image might be in a jpg, gif or png file format.
    I want to change all images to have a 1:1 aspect ratio, on a white background.
    I want no image to exceed 1000 x 1000 pixels at 72 dpi.
    Images already within the 1000x1000 limit should be extended to have a 1:1 aspect ratio, as required.
    At no point is any image scaled upwards in size.
    All images should be saved in a suitably compressed way for displaying on web sites.
    The new images should be written into a sub-directory, with the original images being left in place and unchanged.
    Happy to do this as a batch process per directory.

    Here are some examples:

    1200x1200: Image re-sized to 1000x1000. Save compressed.

    1600x1200: Image re-sized to 1000x750. Then extended to 1000x1000, centred on a white background. Save compressed.

    1200x1600: Image re-sized to 750x1000. Then extended to 1000x1000, centred on a white background. Save compressed.

    750x750: Save compressed.

    800x600: Image extended to 800x800, centred on a white background. Save compressed.

    600x800: Image extended to 800x800, centred on a white background. Save compressed.

    Is it possible to achieve the above using GraphicsMagick?

    If so, how?

    If not, can you recommend a image processing tool that can do this?

    Best regards, Lloyd Borrett.

     

    Last edit: Lloyd Borrett 2015-05-10
    • Bob Friesenhahn

      Bob Friesenhahn - 2015-05-10

      On Sun, 10 May 2015, Lloyd Borrett wrote:

      Is it possible to achieve the following using GraphicsMagick?

      Yes.

      Here are some examples:

      1200x1200: Image re-sized to 1000x1000. Save compressed.

      1600x1200: Image re-sized to 1000x750. Then extended to 1000x1000, centred on a white background. Save
      compressed.

      1200x1600: Image re-sized to 750x1000. Then extended to 1000x1000, centred on a white background. Save
      compressed.

      750x750: Save compressed.

      gm convert infile.jpg -resize "1000x1000>" -background white -gravity center -extent 1000x1000 outfile.jpg
      

      or

      gm montage infile.jpg "1000x1000>" -background white -gravity center outfile.jpg
      

      800x600: Image extended to 800x800, centred on a white background. Save compressed.

      600x800: Image extended to 800x800, centred on a white background. Save compressed.

      The conditional extension of image size (make a fitted square around
      the possibly resized image) is not supported directly within
      GraphicsMagick (to my knowlege, but I might be missing something) but
      anything is possible with a bit of scripting. This is particularly
      true using the Perl, Python, or Tcl scripting extensions which have
      direct access to the image data and so they can accomplish any desired
      effect. It is also possible to use simple Unix or Windows scripting
      (with two steps rather than one) since the original input size may be
      evaluated using:

      gm identify -format "%wx%h" infile.jpg
      

      or the resized image size (to use again with -extent) may be
      determined in advance like

      gm convert infile.jpg -sample "1000x1000>" -format "%wx%h" info:-
      1000x667
      

      Bob

      --
      Bob Friesenhahn
      bfriesen@simple.dallas.tx.us, http://www.simplesystems.org/users/bfriesen/
      GraphicsMagick Maintainer, http://www.GraphicsMagick.org/

       

Log in to post a comment.