Skip to content

Instantly share code, notes, and snippets.

@SleepyBrett
Last active September 29, 2021 22:00
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 SleepyBrett/9a15a3a5cb453dde29cd44d450575601 to your computer and use it in GitHub Desktop.
Save SleepyBrett/9a15a3a5cb453dde29cd44d450575601 to your computer and use it in GitHub Desktop.
Simple python script using imagemagick that cuts regular rects out of a larger image
import sys
from wand.image import Image
if __name__ == "__main__":
sourceImageName = sys.argv[1]
outputNamePattern = "%d-%d-%s"
# how many pixel into the image should we start cutting
siStartX = 6
siStartY = 6
# cutter size
siCropW = 242
siCropH = 242
# gutter sizes (number of pixels to skip after the last cut to get to the new cut, do not include image h/w)
siGutterW = 16
siGutterH = 16
# number of rows/cols
siRows = 3
siCols = 3
for r in range(siRows):
for c in range(siCols):
x = siStartX + (c * (siCropW + siGutterW))
y = siStartY + (r * (siCropH + siGutterH))
fn = outputNamePattern % (r, c, sourceImageName)
print("Cropping col:%d x:%d x row:%d y:%d" % (c,x,r,y))
outputImage = Image(filename = sourceImageName)
outputImage.crop(x, y, width=siCropW, height=siCropH)
outputImage.save(filename=fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment