Created
March 26, 2022 07:28
-
-
Save rocket-pig/bff10850d03806c571184dc542250a05 to your computer and use it in GitHub Desktop.
Minified version of pnger.py at https://gist.github.com/rocket-pig/c8e29296fa7f462d7fd8e8fe642ad5c2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q='RGB' | |
P=True | |
O='rb' | |
N=chr | |
L=bytes | |
K=float | |
G=range | |
I=open | |
C=print | |
F=int | |
E=len | |
import math as B | |
from PIL import Image as H | |
import sys as A | |
if E(A.argv)<2:C('Usage: %s <input file> <output file>'%A.argv[0]);C('if <input file> is a file, will convert to png.');C('if <input file> is a png, will convert back to file');A.exit(1) | |
def M(infile): | |
with I(infile,O)as C:D=C.read() | |
G=E(D);H=F(B.ceil(K(G)/3.0));J=B.sqrt(H);A=F(B.ceil(J));return A,A | |
def D(infile,outfile,no_progress=P,verbose=False): | |
F=no_progress;C=outfile;D=H.open(infile) | |
with I(C,'wb')as C: | |
M=D.convert(Q);B=0 | |
for J in G(D.size[1]): | |
if not F:O=K((J+1)//D.size[1]*100);A.stderr.write('\r%s%s'%(round(O,2),'%')) | |
for P in G(D.size[0]): | |
R=M.getpixel((P,J)) | |
for E in R: | |
if E==0:B+=1 | |
else: | |
if B!=0: | |
for T in G(B): | |
if A.version_info.major>=3:C.write(L([0])) | |
else:C.write(N(0)) | |
B=0 | |
if A.version_info.major>=3:C.write(L([E])) | |
else:C.write(N(E)) | |
if not F:A.stderr.write('\n') | |
if B!=0 and verbose: | |
S=B | |
if S==1:A.stderr.write('Omitting %s zero from end of file\n'%B) | |
else:A.stderr.write('Omitting %s zeroes from end of file\n'%B) | |
def J(infile,outfile): | |
J=infile;K=I(J,O);L=M(J);N=F(L[0]),F(L[1]);A=H.new(Q,N);R=A.load();C=0;D=-1 | |
while P: | |
B=K.read(3) | |
if not B:break | |
D+=1 | |
if D>=A.size[0]: | |
D=0;C+=1 | |
if C>=A.size[1]:raise Exception('Error: row %s is greater than maximum rows in image, %s.'%(C,A.size[1])) | |
G=[B[0],0,0] | |
if E(B)>1:G[1]=B[1] | |
if E(B)>2:G[2]=B[2] | |
if not C>=A.size[1]:R[(D,C)]=tuple(G) | |
A.save(outfile,format='PNG');K.close() | |
if __name__=='__main__': | |
if A.argv[1].endswith('.png'):D(A.argv[1],A.argv[2]) | |
else:J(A.argv[1],A.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment