Skip to content

Instantly share code, notes, and snippets.

@skuschel
Last active February 9, 2017 15:41
Show Gist options
  • Save skuschel/87960f78c7a4a42eb042 to your computer and use it in GitHub Desktop.
Save skuschel/87960f78c7a4a42eb042 to your computer and use it in GitHub Desktop.
Reading pngs with variable bit depth (for example: LabView wrote a png from a 12-bit camera).
def readpng(filename, hotpixelremove=False):
'''
Reads a png file and returns appropriate count vales, even if a bit depth
other than 8 or 16 is used. An example this might be needed is having a
12-bit png recorded from a 12-bit camera using LabViews IMAQ toolset.
In this case the PIL (python image library) fails to retrieve the
original count values.
'''
import png # pypng
import scipy.misc as sm
import numpy as np
meta = png.Reader(filename)
meta.preamble()
significant_bits = ord(meta.sbit)
ret = sm.imread(filename)
ret >>= 16 - significant_bits
if hotpixelremove:
import scipy.ndimage
ret = scipy.ndimage.morphology.grey_opening(ret, size=(3,3))
return np.float64(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment