Last active
September 2, 2018 17:48
-
-
Save ty0x2333/634390382054ba90649c58ed08671b33 to your computer and use it in GitHub Desktop.
Stitching Images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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