Skip to content

Commit

Permalink
Added functions to read/write images using PIL
Browse files Browse the repository at this point in the history
  • Loading branch information
jabriffa committed Jun 13, 2018
1 parent 8cf7c7f commit e056de0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
43 changes: 43 additions & 0 deletions pyshared/jbimage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2015-2018 Johann A. Briffa
#
# This file is part of CR2_Scripts.
#
# CR2_Scripts is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CR2_Scripts is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CR2_Scripts. If not, see <http:https://www.gnu.org/licenses/>.

import numpy as np
from PIL import Image

## read and write image files

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

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
3 changes: 2 additions & 1 deletion raw_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),'pyshared'))
import jbtiff
import jbcr2
import jbimage

## main program

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

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

0 comments on commit e056de0

Please sign in to comment.