#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2021/10/13 09:06:03 +0900> """ Split images and save pattern (8 x 8 tile = 64 x 64 dot). Windows10 x64 21H1 + Python 3.9.7 64bit """ import glob from PIL import Image, ImageFilter import numpy infiles = "../output_images/*.png" bw, bh = 64, 64 def main(): l = glob.glob(infiles) imgs = [] for s in l: im = Image.open(s) iw, ih = im.size print("%s, w x h = %d x %d" % (s, iw, ih)) for y in range(0, ih, bh): for x in range(0, iw, bw): c = im.crop((x, y, x + bw, y + bh)) n = numpy.array(c) imgs.append(n) nimgs = [] for n in imgs: for o in nimgs: if numpy.array_equal(n, o): break else: nimgs.append(n) cnt = 0 for n in nimgs: fn = "pat_%06d.png" % cnt im = Image.fromarray(n) im.save(fn) cnt += 1 if __name__ == '__main__': main()