95 lines
3.2 KiB
Python
95 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""纯标准库 PNG 裁剪 + 整数倍放大(最近邻),用于放大数据手册小字核对。
|
||
|
||
用法:python _pngcrop.py <in.png> <out.png> <x> <y> <w> <h> <scale>
|
||
"""
|
||
import sys, zlib, struct
|
||
|
||
|
||
def read_png(path):
|
||
data = open(path, 'rb').read()
|
||
assert data[:8] == b'\x89PNG\r\n\x1a\n'
|
||
pos = 8
|
||
idat = b''
|
||
w = h = bd = ct = None
|
||
plte = None
|
||
while pos < len(data):
|
||
ln = struct.unpack('>I', data[pos:pos + 4])[0]
|
||
tag = data[pos + 4:pos + 8]
|
||
payload = data[pos + 8:pos + 8 + ln]
|
||
if tag == b'IHDR':
|
||
w, h, bd, ct, comp, filt, il = struct.unpack('>IIBBBBB', payload)
|
||
elif tag == b'IDAT':
|
||
idat += payload
|
||
elif tag == b'PLTE':
|
||
plte = payload
|
||
elif tag == b'IEND':
|
||
break
|
||
pos += 12 + ln
|
||
raw = zlib.decompress(idat)
|
||
ncomp = {0: 1, 2: 3, 3: 1, 4: 2, 6: 4}[ct]
|
||
stride = w * ncomp
|
||
rows = []
|
||
prev = bytearray(stride)
|
||
p = 0
|
||
bpp = ncomp
|
||
for _ in range(h):
|
||
ft = raw[p]
|
||
p += 1
|
||
line = bytearray(raw[p:p + stride])
|
||
p += stride
|
||
if ft == 1:
|
||
for i in range(bpp, stride):
|
||
line[i] = (line[i] + line[i - bpp]) & 0xff
|
||
elif ft == 2:
|
||
for i in range(stride):
|
||
line[i] = (line[i] + prev[i]) & 0xff
|
||
elif ft == 3:
|
||
for i in range(stride):
|
||
a = line[i - bpp] if i >= bpp else 0
|
||
line[i] = (line[i] + ((a + prev[i]) >> 1)) & 0xff
|
||
elif ft == 4:
|
||
for i in range(stride):
|
||
a = line[i - bpp] if i >= bpp else 0
|
||
b = prev[i]
|
||
c = prev[i - bpp] if i >= bpp else 0
|
||
pa, pb, pc = abs(b - c), abs(a - c), abs(a + b - 2 * c)
|
||
pr = a if (pa <= pb and pa <= pc) else (b if pb <= pc else c)
|
||
line[i] = (line[i] + pr) & 0xff
|
||
rows.append(bytes(line))
|
||
prev = line
|
||
if ct == 3 and plte:
|
||
rgb = [tuple(plte[i * 3:i * 3 + 3]) for i in range(len(plte) // 3)]
|
||
rows = [b''.join(bytes(rgb[v]) for v in r) for r in rows]
|
||
ncomp = 3
|
||
return w, h, ncomp, rows
|
||
|
||
|
||
def write_png(path, w, h, ncomp, rows):
|
||
def chunk(tag, payload):
|
||
return (struct.pack('>I', len(payload)) + tag + payload
|
||
+ struct.pack('>I', zlib.crc32(tag + payload) & 0xffffffff))
|
||
ctype = {1: 0, 3: 2, 4: 6}[ncomp]
|
||
raw = b''.join(b'\x00' + r for r in rows)
|
||
with open(path, 'wb') as f:
|
||
f.write(b'\x89PNG\r\n\x1a\n')
|
||
f.write(chunk(b'IHDR', struct.pack('>IIBBBBB', w, h, 8, ctype, 0, 0, 0)))
|
||
f.write(chunk(b'IDAT', zlib.compress(raw, 6)))
|
||
f.write(chunk(b'IEND', b''))
|
||
|
||
|
||
src, dst = sys.argv[1], sys.argv[2]
|
||
x, y, cw, ch, sc = (int(v) for v in sys.argv[3:8])
|
||
W, H, ncomp, rows = read_png(src)
|
||
out = []
|
||
for yy in range(y, min(y + ch, H)):
|
||
r = rows[yy]
|
||
line = bytearray()
|
||
for xx in range(x, min(x + cw, W)):
|
||
px = r[xx * ncomp:(xx + 1) * ncomp]
|
||
line += bytes(px) * sc
|
||
for _ in range(sc):
|
||
out.append(bytes(line))
|
||
write_png(dst, len(out[0]) // ncomp, len(out), ncomp, out)
|
||
print('ok %dx%d -> %dx%d' % (cw, ch, len(out[0]) // ncomp, len(out)))
|