Skip to content

Instantly share code, notes, and snippets.

@TimSC
Last active December 15, 2015 03:59
Show Gist options
  • Save TimSC/5198211 to your computer and use it in GitHub Desktop.
Save TimSC/5198211 to your computer and use it in GitHub Desktop.
Convert from PIL Image and Numpy array to PyPNG format By Tim Sheerman-Chase, 2013 This code is released as CC0 and public domain code. Do what you will!
#Convert from PIL Image and Numpy array to PyPNG format
#By Tim Sheerman-Chase, 2013
#This code is released as CC0 and public domain code. Do what you will!
import png
from PIL import Image
import numpy as np
class NumpyArrayToPyPngAdapter:
def __init__(self, img):
self.img = img
def __len__(self):
return len(self.img)
def __getitem__(self, key):
row = self.img[key]
out = []
for p in row:
if hasattr(p, '__iter__'):
#Multi-channel image
out.extend(p)
else:
#Single channel image
out.append(p)
return out
class PilImageToPyPngAdapter:
def __init__(self, im):
self.im = im
self.iml = im.load()
def __len__(self):
return self.im.size[1]
def __getitem__(self, row):
out = []
for col in range(self.im.size[0]):
px = self.iml[col, row]
if hasattr(px, '__iter__'):
#Multi-channel image
out.extend(px)
else:
#Single channel image
out.append(px)
return out
def PyPngToPilImage(width, height, rows, args):
m = 'RGB'
planes = int(args['planes'])
if planes == 1: m = 'L'
if planes == 4: m = 'RGBA'
im = Image.new(m, (width, height))
iml = im.load()
for rowNum, row in enumerate(rows):
for colNum in range(width):
if planes > 1:
iml[colNum, rowNum] = tuple(row[colNum*planes:(colNum+1)*planes])
else:
iml[colNum, rowNum] = row[colNum]
return im
if __name__ == "__main__":
im = Image.open("input.jpg")
im = im.convert("RGB") # Single channel "L" should also work
ima = np.array(im)
fi = open("test.png","wb")
wri = png.Writer(size=im.size, greyscale=(len(im.mode)==1))
wri.write(fi, NumpyArrayToPyPngAdapter(ima))
#wri.write(fi, PilImageToPyPngAdapter(im))
fi.flush()
read = png.Reader(file=open("test.png","rb"))
readRet = read.read()
print readRet
pilim = PyPngToPilImage(*readRet)
pilim.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment