Skip to content

Commit

Permalink
Encapsulate imread/imwrite in class
Browse files Browse the repository at this point in the history
  • Loading branch information
jabriffa committed Jun 13, 2018
1 parent 43612f4 commit 573447f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cr2_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def main():
os.remove(tmpfile)

# read sensor image file
sensor = jbimage.imread(args.sensor).squeeze()
sensor = jbimage.image_file.read(args.sensor).squeeze()

# obtain required parameters from RAW file
width,height = tiff.get_sensor_size()
Expand Down
40 changes: 22 additions & 18 deletions pyshared/jbimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@
import numpy as np
from PIL import Image

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

def imwrite(I,outfile):
# Works with PIL 1.1.6 upwards
assert list(map(int, Image.VERSION.split('.'))) >= [1,1,6]
# convert to image of the correct type based on shape and dtype
im = Image.fromarray(I.squeeze())
# save to file
im.save(outfile, optimize=True)
return
class image_file():

def imread(infile):
im = Image.open(infile)
ch = len(im.mode)
(x,y) = im.size
# Works with PIL 1.1.6 upwards
# return array is read-only!
assert list(map(int, Image.VERSION.split('.'))) >= [1,1,6]
I = np.asarray(im).reshape(y,x,ch)
return I
@staticmethod
def read(infile):
im = Image.open(infile)
ch = len(im.mode)
(x,y) = im.size
# Works with PIL 1.1.6 upwards
# return array is read-only!
assert list(map(int, Image.VERSION.split('.'))) >= [1,1,6]
I = np.asarray(im).reshape(y,x,ch)
return I

@staticmethod
def write(I,outfile):
# Works with PIL 1.1.6 upwards
assert list(map(int, Image.VERSION.split('.'))) >= [1,1,6]
# convert to image of the correct type based on shape and dtype
im = Image.fromarray(I.squeeze())
# save to file
im.save(outfile, optimize=True)
return
4 changes: 2 additions & 2 deletions raw_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

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

## main program

Expand All @@ -54,7 +54,7 @@ def main():
# read input raw file
tiff = jbtiff.tiff_file(open(args.raw, 'rb'))
# load sensor image
sensor = jbimage.imread(args.input).squeeze()
sensor = jbimage.image_file.read(args.input).squeeze()

# obtain required parameters from RAW file
width,height = tiff.get_sensor_size()
Expand Down

0 comments on commit 573447f

Please sign in to comment.