Skip to content

Commit

Permalink
Move pnm_file to jbimage
Browse files Browse the repository at this point in the history
  • Loading branch information
jabriffa committed Jun 13, 2018
1 parent 573447f commit 296b920
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 66 deletions.
61 changes: 61 additions & 0 deletions pyshared/jbimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,67 @@
import numpy as np
from PIL import Image

class pnm_file():

@staticmethod
def read(fid):
# read header (assume separate lines for id, size, and depth)
tmp = fid.readline().strip()
if tmp == "P5":
ch = 1
elif tmp == "P6":
ch = 3
else:
raise ValueError("Cannot handle files of type %s" % tmp)
tmp = fid.readline().strip().split()
if len(tmp) == 2: # width,height in same line
w = int(tmp[0])
h = int(tmp[1])
else: # width, height in separate lines
assert len(tmp) == 1
w = int(tmp[0])
tmp = fid.readline().strip().split()
assert len(tmp) == 1
h = int(tmp[0])
tmp = fid.readline().strip()
if tmp == "255":
dtype = np.dtype('uint8')
elif tmp == "65535":
dtype = np.dtype('>H')
else:
raise ValueError("Cannot handle files with %s colors" % tmp)
# read pixels
I = np.fromfile(fid, count=h*w*ch, dtype=dtype)
I = I.reshape((h,w,ch)).squeeze()
return I

@staticmethod
def write(image, fid):
# determine dimensions, channels, and bit depth
if len(image.shape) == 2:
h,w = image.shape
ch = 1
elif len(image.shape) == 3:
h,w,ch = image.shape
else:
raise ValueError("Cannot handle input arrays of size %s" % image.shape)
if image.dtype == np.dtype('uint8'):
depth = 8
elif image.dtype == np.dtype('>H'):
depth = 16
else:
raise ValueError("Cannot handle input arrays of type %s" % image.dtype)
# write header
if ch == 1:
print >> fid, "P5"
else:
print >> fid, "P6"
print >> fid, w, h
print >> fid, (1<<depth)-1
# write pixels
image.tofile(fid)
return

## class to read and write general image files (using PIL)

class image_file():
Expand Down
61 changes: 0 additions & 61 deletions pyshared/jbtiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,67 +116,6 @@ def flatten(self):
def display(self):
return ', '.join(['%d-%d' % (a,b) if b>a else '%d' % a for a,b in self.data])

class pnm_file():

@staticmethod
def read(fid):
# read header (assume separate lines for id, size, and depth)
tmp = fid.readline().strip()
if tmp == "P5":
ch = 1
elif tmp == "P6":
ch = 3
else:
raise ValueError("Cannot handle files of type %s" % tmp)
tmp = fid.readline().strip().split()
if len(tmp) == 2: # width,height in same line
w = int(tmp[0])
h = int(tmp[1])
else: # width, height in separate lines
assert len(tmp) == 1
w = int(tmp[0])
tmp = fid.readline().strip().split()
assert len(tmp) == 1
h = int(tmp[0])
tmp = fid.readline().strip()
if tmp == "255":
dtype = np.dtype('uint8')
elif tmp == "65535":
dtype = np.dtype('>H')
else:
raise ValueError("Cannot handle files with %s colors" % tmp)
# read pixels
I = np.fromfile(fid, count=h*w*ch, dtype=dtype)
I = I.reshape((h,w,ch)).squeeze()
return I

@staticmethod
def write(image, fid):
# determine dimensions, channels, and bit depth
if len(image.shape) == 2:
h,w = image.shape
ch = 1
elif len(image.shape) == 3:
h,w,ch = image.shape
else:
raise ValueError("Cannot handle input arrays of size %s" % image.shape)
if image.dtype == np.dtype('uint8'):
depth = 8
elif image.dtype == np.dtype('>H'):
depth = 16
else:
raise ValueError("Cannot handle input arrays of type %s" % image.dtype)
# write header
if ch == 1:
print >> fid, "P5"
else:
print >> fid, "P6"
print >> fid, w, h
print >> fid, (1<<depth)-1
# write pixels
image.tofile(fid)
return

class tiff_file():

## constants
Expand Down
3 changes: 2 additions & 1 deletion raw_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),'pyshared'))
import jbtiff
import jbimage
import jbcr2

## main program
Expand Down Expand Up @@ -55,7 +56,7 @@ def main():
I = jbcr2.unslice_image(a, width, height, slices)

# save result
jbtiff.pnm_file.write(I.astype('>H'), open(args.output,'wb'))
jbimage.pnm_file.write(I.astype('>H'), open(args.output,'wb'))

# show user what we've done, as needed
if args.display:
Expand Down
5 changes: 3 additions & 2 deletions rgb_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),'pyshared'))
import jbtiff
import jbimage

## main program

Expand Down Expand Up @@ -59,7 +60,7 @@ def main():
model = tiff.get_model(0)

# load sensor image
I = jbtiff.pnm_file.read(open(args.input,'rb'))
I = jbimage.pnm_file.read(open(args.input,'rb'))
assert len(I.shape) == 2 # must be a one-channel image
assert I.shape == (height,width) # image size must be exact

Expand Down Expand Up @@ -121,7 +122,7 @@ def main():
I *= (1<<16)-1

# save result
jbtiff.pnm_file.write(I.astype('>H'), open(args.output,'wb'))
jbimage.pnm_file.write(I.astype('>H'), open(args.output,'wb'))

# show user what we've done, as needed
if args.display:
Expand Down
5 changes: 3 additions & 2 deletions rgb_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),'pyshared'))
import jbtiff
import jbimage

## main program

Expand Down Expand Up @@ -69,7 +70,7 @@ def main():
iheight = y2-y1+1

# load colour image
I = jbtiff.pnm_file.read(open(args.input,'r'))
I = jbimage.pnm_file.read(open(args.input,'r'))
assert len(I.shape) == 3 and I.shape[2] == 3 # must be a three-channel image
assert I.shape == (iheight,iwidth,3) # image size must be exact

Expand Down Expand Up @@ -135,7 +136,7 @@ def main():
a[1::2,0::2] = I[1::2,0::2,cmap[args.bayer[2]]]
a[1::2,1::2] = I[1::2,1::2,cmap[args.bayer[3]]]
# save result
jbtiff.pnm_file.write(a, open(args.output,'w'))
jbimage.pnm_file.write(a, open(args.output,'w'))

# show user what we've done, as needed
if args.display:
Expand Down

0 comments on commit 296b920

Please sign in to comment.