Загрузка данных


from PIL import Image
import json

class Matrix:
    def __init__(s, f):
        s.i = Image.open(f)
        w, h = s.i.size
        if w != h:
            raise TypeError("The image dimensions must be the same.")

    def cube(s):
        p = s.i.load()
        w, h = s.i.size
        r = g = b = 0
        for y in range(h - 1):
            for x in range(w - 1):
                a = [p[x, y], p[x+1, y], p[x, y+1], p[x+1, y+1]]
                if max(c[0] for c in a) - min(c[0] for c in a) <= 5:
                    r += 1
                if max(c[1] for c in a) - min(c[1] for c in a) <= 5:
                    g += 1
                if max(c[2] for c in a) - min(c[2] for c in a) <= 5:
                    b += 1
        return json.dumps({"red": r, "green": g, "blue": b})

    def boojums(s, *a):
        c = []
        for x, y in a:
            t = s.i.crop((x, y, x + 100, y + 100))
            t = t.rotate(-90, expand=True)
            c.append(t)
        n = Image.new(s.i.mode, (100 * len(c), 100))
        for i, t in enumerate(c):
            n.paste(t, (i * 100, 0))
        return n

    def save(s, i, f):
        i.save(f)