~ubuntu-branches/ubuntu/wily/python-imaging/wily

« back to all changes in this revision

Viewing changes to PIL/SpiderImagePlugin.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (27.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130131204920-b5zshy6vgfvdionl
Tags: 1.1.7+1.7.8-1ubuntu1
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
# http://www.wadsworth.org/spider_doc/spider/docs/image_doc.html
34
34
#
35
35
 
36
 
import Image, ImageFile
 
36
from __future__ import print_function
 
37
 
 
38
from . import Image, ImageFile
37
39
import os, struct, sys
38
40
 
39
41
def isInt(f):
101
103
                t = struct.unpack('<27f',f)  # little-endian
102
104
                hdrlen = isSpiderHeader(t)
103
105
            if hdrlen == 0:
104
 
                raise SyntaxError, "not a valid Spider file"
 
106
                raise SyntaxError("not a valid Spider file")
105
107
        except struct.error:
106
 
            raise SyntaxError, "not a valid Spider file"
 
108
            raise SyntaxError("not a valid Spider file")
107
109
 
108
110
        h = (99,) + t   # add 1 value : spider header index starts at 1
109
111
        iform = int(h[5])
110
112
        if iform != 1:
111
 
            raise SyntaxError, "not a Spider 2D image"
 
113
            raise SyntaxError("not a Spider 2D image")
112
114
 
113
115
        self.size = int(h[12]), int(h[2]) # size in pixels (width, height)
114
116
        self.istack = int(h[24])
131
133
            offset = hdrlen + self.stkoffset
132
134
            self.istack = 2  # So Image knows it's still a stack
133
135
        else:
134
 
            raise SyntaxError, "inconsistent stack header values"
 
136
            raise SyntaxError("inconsistent stack header values")
135
137
 
136
138
        if self.bigendian:
137
139
            self.rawmode = "F;32BF"
154
156
        if self.istack == 0:
155
157
            return
156
158
        if frame >= self.nimages:
157
 
            raise EOFError, "attempt to seek past end of file"
 
159
            raise EOFError("attempt to seek past end of file")
158
160
        self.stkoffset = self.hdrlen + frame * (self.hdrlen + self.imgbytes)
159
161
        self.fp = self.__fp
160
162
        self.fp.seek(self.stkoffset)
171
173
 
172
174
    # returns a ImageTk.PhotoImage object, after rescaling to 0..255
173
175
    def tkPhotoImage(self):
174
 
        import ImageTk
 
176
        from . import ImageTk
175
177
        return ImageTk.PhotoImage(self.convert2byte(), palette=256)
176
178
 
177
179
# --------------------------------------------------------------------
186
188
    imglist = []
187
189
    for img in filelist:
188
190
        if not os.path.exists(img):
189
 
            print "unable to find %s" % img
 
191
            print("unable to find %s" % img)
190
192
            continue
191
193
        try:
192
194
            im = Image.open(img).convert2byte()
193
195
        except:
194
196
            if not isSpiderImage(img):
195
 
                print img + " is not a Spider image file"
 
197
                print(img + " is not a Spider image file")
196
198
            continue
197
199
        im.info['filename'] = img
198
200
        imglist.append(im)
239
241
 
240
242
    hdr = makeSpiderHeader(im)
241
243
    if len(hdr) < 256:
242
 
        raise IOError, "Error creating Spider header"
 
244
        raise IOError("Error creating Spider header")
243
245
 
244
246
    # write the SPIDER header
245
247
    try:
246
248
        fp = open(filename, 'wb')
247
249
    except:
248
 
        raise IOError, "Unable to open %s for writing" % filename
 
250
        raise IOError("Unable to open %s for writing" % filename)
249
251
    fp.writelines(hdr)
250
252
 
251
253
    rawmode = "F;32NF"  #32-bit native floating point
267
269
if __name__ == "__main__":
268
270
 
269
271
    if not sys.argv[1:]:
270
 
        print "Syntax: python SpiderImagePlugin.py Spiderimage [outfile]"
 
272
        print("Syntax: python SpiderImagePlugin.py Spiderimage [outfile]")
271
273
        sys.exit()
272
274
 
273
275
    filename = sys.argv[1]
274
276
    if not isSpiderImage(filename):
275
 
        print "input image must be in Spider format"
 
277
        print("input image must be in Spider format")
276
278
        sys.exit()
277
279
 
278
280
    outfile = ""
280
282
        outfile = sys.argv[2]
281
283
 
282
284
    im = Image.open(filename)
283
 
    print "image: " + str(im)
284
 
    print "format: " + str(im.format)
285
 
    print "size: " + str(im.size)
286
 
    print "mode: " + str(im.mode)
287
 
    print "max, min: ",
288
 
    print im.getextrema()
 
285
    print("image: " + str(im))
 
286
    print("format: " + str(im.format))
 
287
    print("size: " + str(im.size))
 
288
    print("mode: " + str(im.mode))
 
289
    print("max, min: ", end=' ')
 
290
    print(im.getextrema())
289
291
 
290
292
    if outfile != "":
291
293
        # perform some image operation
292
294
        im = im.transpose(Image.FLIP_LEFT_RIGHT)
293
 
        print "saving a flipped version of %s as %s " % (os.path.basename(filename), outfile)
 
295
        print("saving a flipped version of %s as %s " % (os.path.basename(filename), outfile))
294
296
        im.save(outfile, "SPIDER")