Skip to content

Instantly share code, notes, and snippets.

@ty0x2333
Last active September 2, 2018 17:48
Show Gist options
  • Select an option

  • Save ty0x2333/634390382054ba90649c58ed08671b33 to your computer and use it in GitHub Desktop.

Select an option

Save ty0x2333/634390382054ba90649c58ed08671b33 to your computer and use it in GitHub Desktop.
Stitching Images
# Python 3
# requirements:
# wand==0.4.1
from wand.image import Image
import os
from math import ceil, floor
NUMBER_OF_COL = 17
ITEM_WIDTH = 260
ITEM_HEIGHT = 248
DIR = "/Users/tyy/Downloads/inputs"
OUT_PUT_DIR = "/Users/tyy/Downloads"
files = os.listdir(DIR)
image_paths = []
for filename in files:
path = os.path.join(DIR, filename)
image_paths.append(path)
# print(path)
#print(image_paths)
print("total: {}".format(len(image_paths)))
number_of_row = ceil(len(image_paths) / 17.0)
print("number of row {}".format(number_of_row))
with Image() as resultImage:
resultImage.blank(ITEM_WIDTH * NUMBER_OF_COL, ITEM_HEIGHT * number_of_row)
for idx, path in enumerate(image_paths):
try:
with Image(filename=path) as img:
img.resize(ITEM_WIDTH, ITEM_HEIGHT)
offset = (idx % NUMBER_OF_COL, floor(idx / NUMBER_OF_COL))
print("offset: {}".format(offset))
resultImage.composite(img, offset[0] * ITEM_WIDTH, offset[1] * ITEM_HEIGHT)
# print("idx: {}, path: {}".format(idx, path))
except Exception:
print("open image error: {}, path: {}".format(error, path))
resultImage.save(filename = os.path.join(OUT_PUT_DIR, "output.tif"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment