| Home | Trees | Indices | Help |
|---|
|
|
1 ## Automatically adapted for numpy.oldnumeric Oct 05, 2007 by alter_code1.py
2
3 #!/usr/bin/env python
4 """
5
6 Authors: Henning O. Sorensen & Erik Knudsen
7 Center for Fundamental Research: Metal Structures in Four Dimensions
8 Risoe National Laboratory
9 Frederiksborgvej 399
10 DK-4000 Roskilde
11 email:henning.sorensen@risoe.dk
12
13 mods for fabio by JPW
14
15 """
16
17 import Image
18 import numpy , logging
19
20 from fabioimage import fabioimage
21
22 TIFF_TO_NUMERIC = { "I;16": numpy.int16 ,
23
24 }
25
26
28 """
29 Images in TIF format
30 Wraps PIL
31 """
32 _need_a_seek_to_read = True
33
35 """ Tifimage constructor adds an nbits member attribute """
36 self.nbits = None
37 fabioimage.__init__(self, *args, **kwds)
38
40 """
41 Don't know how to read tiff tags yet...
42 """
43 try:
44 self.header = { "filename" : infile.name }
45 except:
46 self.header = {}
47
48
49 # read the first 32 bytes to determine size
50 header = numpy.fromstring(infile.read(64), numpy.uint16)
51 self.dim1 = int(header[9])
52 self.dim2 = int(header[15])
53 # nbits is not a fabioimage attribute...
54 self.nbits = int(header[21]) # number of bits
55
56
57
59 """
60 The fabian read was reading a PIL image
61 We convert this to a numpy array
62 """
63 infile = self._open(fname, "rb")
64 self._readheader(infile)
65 infile.seek(0)
66 try:
67 self.pilimage = Image.open(infile)
68 except:
69 infile.seek(0)
70 raw_data = infile.read()
71 header_bytes = len(raw_data) - (self.dim1 * self.dim2 * self.nbits) / 8
72 if self.nbits == 16: # Probably uint16
73 # logging.warn('USING FIT2D 16 BIT TIFF READING - EXPERIMENTAL')
74 self.pilimage = Image.frombuffer("F",
75 (self.dim1, self.dim2),
76 raw_data[header_bytes:],
77 "raw",
78 "I;16",
79 0, 1)
80 self.bpp = 2
81 self.data = numpy.fromstring(raw_data[header_bytes:],
82 numpy.uint16)
83
84 elif self.nbits == 32: # Probably uint16
85 # logging.warn('USING FIT2D 32 BIT FLOAT TIFF READING -' +
86 # ' EXPERIMENTAL')
87 self.pilimage = Image.frombuffer("F",
88 (self.dim1, self.dim2),
89 raw_data[header_bytes:],
90 "raw",
91 "F",
92 0, 1)
93 self.bpp = 4
94 self.data = numpy.fromstring(raw_data[header_bytes:],
95 numpy.float32)
96 self.data = numpy.reshape(self.data, (self.dim2, self.dim1))
97 self.resetvals()
98 return self
99
100
101 # For some odd reason the getextrema does not work on unsigned 16 bit
102 # but it does on 32 bit images, hence convert if 16 bit
103 if TIFF_TO_NUMERIC.has_key(self.pilimage.mode) and \
104 self.pilimage.mode != "I;16":
105 self.data = numpy.fromstring(
106 self.pilimage.tostring(),
107 TIFF_TO_NUMERIC[self.pilimage.mode])
108 self.bpp = len(numpy.ones(1,
109 TIFF_TO_NUMERIC[self.pilimage.mode]).tostring())
110 else:
111 temp = self.pilimage.convert("I") # 32 bit signed
112 self.data = numpy.fromstring(
113 temp.tostring(),
114 numpy.int32)
115 self.bpp = 4
116 self.pilimage = temp
117 self.dim1, self.dim2 = self.pilimage.size
118 # PIL is transposed compared to numpy?
119 self.data = numpy.reshape(self.data, (self.dim2, self.dim1))
120 self.resetvals()
121 return self
122
124 """
125 ... at least try ...
126 """
127 if self.pilimage is None:
128 if self.data.dtype != numpy.dtype('uint16'):
129 # Use toPIL16 if you which to save a non 16 bit image
130 # Odd, but this is how it apparently works
131 self.toPIL16()
132 else:
133 # to save 16 bit tif we have to use "I;16" not "F;16"
134 size = self.data.shape[:2][::-1]
135 self.pilimage = Image.frombuffer('I',
136 size,
137 self.data.tostring(),
138 "raw",
139 'I;16',
140 0, 1)
141
142 self.pilimage.save(fname, "TIFF")
143
| Home | Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Wed May 25 09:06:11 2011 | http://epydoc.sourceforge.net |