~asdfghjkl-deactivatedaccount1/python-imaging/gif-fix

« back to all changes in this revision

Viewing changes to PIL/SpiderImagePlugin.py

  • Committer: effbot
  • Date: 2006-07-05 20:36:11 UTC
  • Revision ID: svn-v4:be285980-f00d-0410-a9fe-d4747b46ecd0:pil:348
Load Imaging-1.1.6b1 into pil.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
# History:
7
7
# 2004-08-02    Created BB
8
8
# 2006-03-02    added save method
 
9
# 2006-03-13    added support for stack images
9
10
#
10
11
# Copyright (c) 2004 by Health Research Inc. (HRI) RENSSELAER, NY 12144.
11
12
# Copyright (c) 2004 by William Baxter.
53
54
def isSpiderHeader(t):
54
55
    h = (99,) + t   # add 1 value so can use spider header index start=1
55
56
    # header values 1,2,5,12,13,22,23 should be integers
56
 
    if not isInt(h[1]): return 0
57
 
    if not isInt(h[2]): return 0
58
 
    if not isInt(h[5]): return 0
59
 
    if not isInt(h[12]): return 0
60
 
    if not isInt(h[13]): return 0
61
 
    if not isInt(h[22]): return 0
62
 
    if not isInt(h[23]): return 0
 
57
    for i in [1,2,5,12,13,22,23]:
 
58
        if not isInt(h[i]): return 0
63
59
    # check iform
64
60
    iform = int(h[5])
65
61
    if not iform in iforms: return 0
93
89
 
94
90
    def _open(self):
95
91
        # check header
96
 
        n = 23 * 4  # read 23 float values
 
92
        n = 27 * 4  # read 27 float values
97
93
        f = self.fp.read(n)
98
94
 
99
95
        try:
100
96
            self.bigendian = 1
101
 
            t = struct.unpack('>23f',f)    # try big-endian first
 
97
            t = struct.unpack('>27f',f)    # try big-endian first
102
98
            hdrlen = isSpiderHeader(t)
103
99
            if hdrlen == 0:
104
100
                self.bigendian = 0
105
 
                t = struct.unpack('<23f',f)  # little-endian
 
101
                t = struct.unpack('<27f',f)  # little-endian
106
102
                hdrlen = isSpiderHeader(t)
107
103
            if hdrlen == 0:
108
104
                raise SyntaxError, "not a valid Spider file"
109
105
        except struct.error:
110
106
            raise SyntaxError, "not a valid Spider file"
111
107
 
112
 
        # size in pixels (width, height)
113
 
        h = (99,) + t   # add 1 value cos' spider header index starts at 1
 
108
        h = (99,) + t   # add 1 value : spider header index starts at 1
114
109
        iform = int(h[5])
115
110
        if iform != 1:
116
111
            raise SyntaxError, "not a Spider 2D image"
117
112
 
118
 
        self.size = int(h[12]), int(h[2])
 
113
        self.size = int(h[12]), int(h[2]) # size in pixels (width, height)
 
114
        self.istack = int(h[24])
 
115
        self.imgnumber = int(h[27])
119
116
 
 
117
        if self.istack == 0 and self.imgnumber == 0:
 
118
            # stk=0, img=0: a regular 2D image
 
119
            offset = hdrlen
 
120
            self.nimages = 1
 
121
        elif self.istack > 0 and self.imgnumber == 0:
 
122
            # stk>0, img=0: Opening the stack for the first time
 
123
            self.imgbytes = int(h[12]) * int(h[2]) * 4
 
124
            self.hdrlen = hdrlen
 
125
            self.nimages = int(h[26])
 
126
            # Point to the first image in the stack
 
127
            offset = hdrlen * 2
 
128
            self.imgnumber = 1
 
129
        elif self.istack == 0 and self.imgnumber > 0:
 
130
            # stk=0, img>0: an image within the stack
 
131
            offset = hdrlen + self.stkoffset
 
132
            self.istack = 2  # So Image knows it's still a stack
 
133
        else:
 
134
            raise SyntaxError, "inconsistent stack header values"
 
135
        
120
136
        if self.bigendian:
121
137
            self.rawmode = "F;32BF"
122
138
        else:
123
139
            self.rawmode = "F;32F"
124
 
 
125
140
        self.mode = "F"
126
 
        self.tile = [("raw", (0, 0) + self.size, hdrlen,
 
141
            
 
142
        self.tile = [("raw", (0, 0) + self.size, offset,
127
143
                    (self.rawmode, 0, 1))]
 
144
        self.__fp = self.fp # FIXME: hack
 
145
 
 
146
    # 1st image index is zero (although SPIDER imgnumber starts at 1)
 
147
    def tell(self):
 
148
        if self.imgnumber < 1:
 
149
            return 0
 
150
        else:
 
151
            return self.imgnumber - 1
 
152
 
 
153
    def seek(self, frame):
 
154
        if self.istack == 0:
 
155
            return
 
156
        if frame >= self.nimages:
 
157
            raise EOFError, "attempt to seek past end of file"
 
158
        self.stkoffset = self.hdrlen + frame * (self.hdrlen + self.imgbytes)
 
159
        self.fp = self.__fp
 
160
        self.fp.seek(self.stkoffset)
 
161
        self._open()
128
162
 
129
163
    # returns a byte image after rescaling to 0..255
130
164
    def convert2byte(self, depth=255):
177
211
    nvalues = labbyt / 4
178
212
    for i in range(nvalues):
179
213
        hdr.append(0.0)
180
 
 
 
214
        
181
215
    if len(hdr) < 23:
182
216
        return []
183
217
 
184
218
    # NB these are Fortran indices
185
 
    hdr[1]  = 1.0           # nslice (=1 for an image)
 
219
    hdr[1]  = 1.0           # nslice (=1 for an image) 
186
220
    hdr[2]  = float(nrow)   # number of rows per slice
187
221
    hdr[5]  = 1.0           # iform for 2D image
188
222
    hdr[12] = float(nsam)   # number of pixels per line
202
236
def _save(im, fp, filename):
203
237
    if im.mode[0] != "F":
204
238
        im = im.convert('F')
205
 
 
 
239
    
206
240
    hdr = makeSpiderHeader(im)
207
241
    if len(hdr) < 256:
208
242
        raise IOError, "Error creating Spider header"
240
274
    if not isSpiderImage(filename):
241
275
        print "input image must be in Spider format"
242
276
        sys.exit()
243
 
 
 
277
        
244
278
    outfile = ""
245
279
    if len(sys.argv[1:]) > 1:
246
280
        outfile = sys.argv[2]
258
292
        im = im.transpose(Image.FLIP_LEFT_RIGHT)
259
293
        print "saving a flipped version of %s as %s " % (os.path.basename(filename), outfile)
260
294
        im.save(outfile, "SPIDER")
 
295