Skip to content

Instantly share code, notes, and snippets.

@shakes76
Last active August 29, 2015 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shakes76/08cac163a7acf1ad35ac to your computer and use it in GitHub Desktop.
Save shakes76/08cac163a7acf1ad35ac to your computer and use it in GitHub Desktop.
Simple Image IO Module for Python and the Finite Transform Library (FTL)
'''
This module extends scipy modules for saving and loading images losslessly.
It ensures files are written and arrays returned are in 32-bit integer format
PIL Modes: L - 8 bit, I - 32 bit, RGB, RGBA, 1 - 1 bit
@author: Shekhar S. Chandra
'''
import Image #PIL
import numpy as np
import scipy.misc
def lena(n, m=0, out_dtype=np.uint32, center=True):
'''
Return image of lena as numpy array of size nx, ny from the centre.
Lena will be padded to m if provided
Example: crop_lena = imageio.lena(p)
'''
lena = scipy.misc.lena() # load lena
lx, ly = lena.shape
mid = int(n/2.0)
if n % 2 == 0:
newLengthX = lx/2 - (mid)
else: #prime size
newLengthX = lx/2 - (mid+1)
newLengthY = ly/2 + mid
crop_lena = lena[newLengthX:newLengthY, newLengthX:newLengthY]
if m > 0:
#zero pad to larger prime size to allow non-periodic rotations.
image = np.zeros((m, m), dtype=out_dtype)
lx, ly = image.shape
mid = int(n/2.0)
if center:
newLengthX = lx/2 - (mid)
newLengthY = ly/2 + mid
else:
newLengthX = 0
newLengthY = n
image[newLengthX:newLengthY, newLengthX:newLengthY] = crop_lena.astype(out_dtype)
else:
image = crop_lena.astype(out_dtype)
return image
def readPGM(name):
'''
Read a PGM image, where the PGM format is that of the FTL library for NTTs and FRTs.
This is a slightly modified format where values are not limited to 8-bit values.
Returns array of image and bit depth (image, depth)
'''
inFile = open(name,"r")
#read header
formatLine = inFile.readline()
commentLine = inFile.readline()
if not "P2" in formatLine:
print "Error: PGM not in correct format (P2)"
print "Comment:", commentLine
width, height = [int(x) for x in inFile.readline().split()] # read dimensions
print "PGM Size:", width, "x", height
bitDepth = [int(x) for x in inFile.readline().split()] # read bit Depth
imageList = []
for line in inFile: # read remaining lines
valueList = [int(x) for x in line.split()] #read integers on each line
for value in valueList:
imageList.append(value) #append as 1D list
# print imageList
#store as array
image = np.array(imageList).reshape(height, width)
return image, bitDepth
def imread(name):
'''
Load image into an array losslessly for 32-bit integers.
Returns the numpy array object created
'''
im = Image.open(name)
arr = scipy.misc.fromimage(im)
return arr
def imsave(name, arr, datamode=False):
'''
Save an array to an image file losslessly for 32-bit integers.
Returns the PIL object used. Data Mode is whether to use large integers
in the file rather than 8-bit integers. The former will not be displayable
via standard image viewers
'''
if datamode:
im = scipy.misc.toimage(arr, high=np.max(arr), low=np.min(arr), mode='I')
else:
im = scipy.misc.toimage(arr, high=np.max(arr), low=np.min(arr))
im.save(name)
return im
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment