forked from jbresearch/cr2_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jbimage.py
115 lines (104 loc) · 3.41 KB
/
jbimage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/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
# determine bit depth required to hold saturation value
def get_precision(saturation):
return int(np.ceil(np.log2(saturation)))
## class to read and write PNM image files
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():
@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