Files

33 lines
1017 B
Bash

#!/bin/bash
python3 - <<'EOF'
import zlib, struct
def ppm2png(src, dst):
d=open(src,'rb').read()
# parse header
parts=[]; i=0
while len(parts)<4:
while d[i:i+1].isspace(): i+=1
if d[i:i+1]==b'#':
while d[i:i+1] not in (b'\n',b'\r'): i+=1
continue
j=i
while not d[j:j+1].isspace(): j+=1
parts.append(d[i:j]); i=j
i+=1
w=int(parts[1]); h=int(parts[2])
px=d[i:i+w*h*3]
raw=b''.join(b'\x00'+px[y*w*3:(y+1)*w*3] for y in range(h))
def chunk(t,data):
c=t+data
return struct.pack('>I',len(data))+c+struct.pack('>I',zlib.crc32(c)&0xffffffff)
png=b'\x89PNG\r\n\x1a\n'
png+=chunk(b'IHDR',struct.pack('>IIBBBBB',w,h,8,2,0,0,0))
png+=chunk(b'IDAT',zlib.compress(raw,9))
png+=chunk(b'IEND',b'')
open(dst,'wb').write(png)
print(dst,w,h,len(png))
ppm2png('/tmp/shot.ppm','/path/to/pf/super/preview.png')
ppm2png('/tmp/shot2.ppm','/path/to/pf/super/preview2.png')
EOF
ls -l /path/to/pf/super/