cutout.py (3729B)
1 #!/usr/bin/env python3 2 # makes the white around the logo transparent, then crops it square 3 # usage: cutout.py in.png out.png (8-bit rgb/rgba png) 4 import struct, sys, zlib 5 from collections import deque 6 7 8 def read_png(path): 9 data = open(path, 'rb').read() 10 pos, idat = 8, b'' 11 while pos < len(data): 12 n, kind = struct.unpack('>I4s', data[pos:pos + 8]) 13 body = data[pos + 8:pos + 8 + n] 14 if kind == b'IHDR': 15 w, h, depth, ctype = struct.unpack('>IIBB', body[:10]) 16 elif kind == b'IDAT': 17 idat += body 18 pos += 12 + n 19 bpp = 4 if ctype == 6 else 3 20 raw, stride, prev, px = zlib.decompress(idat), w * bpp, bytearray(w * bpp), [] 21 for y in range(h): 22 f = raw[y * (stride + 1)] 23 line = bytearray(raw[y * (stride + 1) + 1:(y + 1) * (stride + 1)]) 24 for i in range(stride): 25 a = line[i - bpp] if i >= bpp else 0 26 b = prev[i] 27 c = prev[i - bpp] if i >= bpp else 0 28 if f == 1: 29 line[i] = (line[i] + a) & 255 30 elif f == 2: 31 line[i] = (line[i] + b) & 255 32 elif f == 3: 33 line[i] = (line[i] + (a + b) // 2) & 255 34 elif f == 4: 35 p = a + b - c 36 pa, pb, pc = abs(p - a), abs(p - b), abs(p - c) 37 line[i] = (line[i] + (a if pa <= pb and pa <= pc else b if pb <= pc else c)) & 255 38 prev = line 39 for x in range(w): 40 r, g, bl = line[x * bpp:x * bpp + 3] 41 px.append([r, g, bl, line[x * bpp + 3] if bpp == 4 else 255]) 42 return w, h, px 43 44 45 def write_png(path, w, h, px): 46 raw = b''.join(b'\0' + bytes(v for p in px[y * w:(y + 1) * w] for v in p) for y in range(h)) 47 def chunk(t, b): 48 return struct.pack('>I', len(b)) + t + b + struct.pack('>I', zlib.crc32(t + b) & 0xffffffff) 49 with open(path, 'wb') as f: 50 f.write(b'\x89PNG\r\n\x1a\n') 51 f.write(chunk(b'IHDR', struct.pack('>IIBBBBB', w, h, 8, 6, 0, 0, 0))) 52 f.write(chunk(b'IDAT', zlib.compress(raw, 9))) 53 f.write(chunk(b'IEND', b'')) 54 55 56 if __name__ == '__main__': 57 w, h, px = read_png(sys.argv[1]) 58 white = lambda p: min(p[:3]) >= 232 59 60 # flood fill from the edges, the hole and the gaps are connected to it 61 bg = bytearray(w * h) 62 q = deque(i for i in range(w * h) if (i < w or i >= w * (h - 1) or i % w in (0, w - 1)) and white(px[i])) 63 for i in q: 64 bg[i] = 1 65 while q: 66 i = q.popleft() 67 x = i % w 68 for j in (i - 1 if x else -1, i + 1 if x < w - 1 else -1, i - w, i + w): 69 if 0 <= j < w * h and not bg[j] and white(px[j]): 70 bg[j] = 1 71 q.append(j) 72 73 for i, p in enumerate(px): 74 if bg[i]: 75 p[3] = 0 76 continue 77 # soft edge: take the white back out of the blended pixels 78 if min(p[:3]) > 150 and any(0 <= j < w * h and bg[j] for j in (i - 1, i + 1, i - w, i + w)): 79 a = max(255 - c for c in p[:3]) / 255 80 if a < 0.02: 81 p[3] = 0 82 else: 83 p[:3] = [max(0, min(255, round((c - 255 * (1 - a)) / a))) for c in p[:3]] 84 p[3] = round(255 * a) 85 86 xs = [i % w for i, p in enumerate(px) if p[3]] 87 ys = [i // w for i, p in enumerate(px) if p[3]] 88 x0, x1, y0, y1 = min(xs), max(xs), min(ys), max(ys) 89 side = int(max(x1 - x0, y1 - y0) * 1.04) + 1 90 ox, oy = (side - (x1 - x0 + 1)) // 2, (side - (y1 - y0 + 1)) // 2 91 out = [[0, 0, 0, 0] for _ in range(side * side)] 92 for y in range(y0, y1 + 1): 93 for x in range(x0, x1 + 1): 94 out[(y - y0 + oy) * side + (x - x0 + ox)] = px[y * w + x] 95 write_png(sys.argv[2], side, side, out)