Skip to content

Instantly share code, notes, and snippets.

@heaversm
Created May 4, 2018 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heaversm/735e521c2dd76fd37d7741dea1b104ac to your computer and use it in GitHub Desktop.
Save heaversm/735e521c2dd76fd37d7741dea1b104ac to your computer and use it in GitHub Desktop.
Pix2Pix image processing commands with ImageMagick and Montage

Image Processing with ImageMagick

Grid of Images to Individual Images

Given a grid of images, generate single images

convert image.png -crop 2x3-8-8@ +repage +adjoin tile-%d.jpg

where:

  • image.png is your screenshot file name
  • 6x3 is your grid layout
  • -8-8 is the amount of horizontal and vertical padding around each image
  • tile-%d.jpg will be the name of each image, with a number appended for each one generated, replacing the %d (e.g. tile-1.jpg)

Individual Images to Image Pairs

Given a directory of single images, generate side-by-side image pairs for pix2pix model generation using montage:

montage tile-*.jpg -tile 2x1 -geometry 418x418+0+0 multi-%d.jpg

where:

  • tile-* is the repeatable name of each image in the directory (e.g. tile-1.jpg, tile-2.jpg, etc)
  • 2x1 creates sets of images with 1 row and 2 columns
  • 418x418 is the dimensions of each individual image
  • 0+0 is the amount of horizontal and vertical padding you want in between each image (we don't want any)
  • multi-%d.jpg is the output file name for each image pair.

Test Images with Blank Pair

For generating new images, we want to run pix2pix on a test set of images with a blank image as its pair:

for i in *.jpg; do
    convert \
       ${i} \
      -gravity west \
      -background white \
      -extent 836x418 \
       $(convert ${i} -format "pair-%t.%e" info:)
 done

where:

  • i in *.jpg loops through all of our jpg images in order to run imagemagick's convert command
  • ${i} becomes the placeholder for that image path
  • -gravity west \ implies we will be expanding our image canvas to the right (our image will be on the left / west side)
  • -extent 836x418 \ gives the final desired dimensions of our images (418x2=836 pixels in width)
  • $(convert ${i} -format "pair-%t.%e" info:) runs the actual convert command, generating images with a name like pair-1.jpg where %t is the iterator, and %e is the extension
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment