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

« back to all changes in this revision

Viewing changes to PIL/Image.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:
1
1
#
2
2
# The Python Imaging Library.
3
 
# $Id: Image.py 2651 2006-03-03 23:22:37Z fredrik $
 
3
# $Id: Image.py 2759 2006-06-19 13:31:12Z fredrik $
4
4
#
5
5
# the Image class wrapper
6
6
#
15
15
# 2002-03-15 fl   PIL release 1.1.3
16
16
# 2003-05-10 fl   PIL release 1.1.4
17
17
# 2005-03-28 fl   PIL release 1.1.5
18
 
# 2006-03-03 fl   PIL release 1.1.6a2
 
18
# 2006-06-20 fl   PIL release 1.1.6b1
19
19
#
20
20
# Copyright (c) 1997-2006 by Secret Labs AB.  All rights reserved.
21
21
# Copyright (c) 1995-2006 by Fredrik Lundh.
23
23
# See the README file for information on usage and redistribution.
24
24
#
25
25
 
26
 
VERSION = "1.1.6a2-20060303"
 
26
VERSION = "1.1.6b1"
27
27
 
28
28
try:
29
29
    import warnings
65
65
            RuntimeWarning
66
66
            )
67
67
 
 
68
import ImageMode
68
69
import ImagePalette
 
70
 
69
71
import os, string, sys
70
72
 
71
73
# type stuff
164
166
# Modes supported by this version
165
167
 
166
168
_MODEINFO = {
 
169
    # NOTE: this table will be removed in future versions.  use
 
170
    # getmode* functions or ImageMode descriptors instead.
167
171
 
168
172
    # official modes
169
173
    "1": ("L", "L", ("1",)),
200
204
# @exception KeyError If the input mode was not a standard mode.
201
205
 
202
206
def getmodebase(mode):
203
 
    # corresponding "base" mode (grayscale or colour)
204
 
    if mode == "LA":
205
 
        return "L"
206
 
    if mode == "PA":
207
 
        return "RGB"
208
 
    return _MODEINFO[mode][0]
 
207
    return ImageMode.getmode(mode).basemode
209
208
 
210
209
##
211
210
# Gets the storage type mode.  Given a mode, this function returns a
216
215
# @exception KeyError If the input mode was not a standard mode.
217
216
 
218
217
def getmodetype(mode):
219
 
    # storage type (per band)
220
 
    if mode == "LA":
221
 
        return "L"
222
 
    if mode == "PA":
223
 
        return "L"
224
 
    return _MODEINFO[mode][1]
 
218
    return ImageMode.getmode(mode).basetype
225
219
 
226
220
##
227
221
# Gets a list of individual band names.  Given a mode, this function
234
228
#     gives the number of bands in an image of the given mode.
235
229
# @exception KeyError If the input mode was not a standard mode.
236
230
 
 
231
def getmodebandnames(mode):
 
232
    return ImageMode.getmode(mode).bands
 
233
 
 
234
##
 
235
# Gets the number of individual bands for this mode.
 
236
#
 
237
# @param mode Input mode.
 
238
# @return The number of bands in this mode.
 
239
# @exception KeyError If the input mode was not a standard mode.
 
240
 
237
241
def getmodebands(mode):
238
 
    # return list of subcomponents
239
 
    if mode == "LA":
240
 
        return "L", "A"
241
 
    if mode == "PA":
242
 
        return "P", "A"
243
 
    return len(_MODEINFO[mode][2])
 
242
    return len(ImageMode.getmode(mode).bands)
244
243
 
245
244
# --------------------------------------------------------------------
246
245
# Helpers
761
760
    def getbands(self):
762
761
        "Get band names"
763
762
 
764
 
        return _MODEINFO[self.mode][2]
 
763
        return ImageMode.getmode(self.mode).bands
765
764
 
766
765
    ##
767
766
    # Calculates the bounding box of the non-zero regions in the
1028
1027
            im = im.im
1029
1028
 
1030
1029
        self.load()
1031
 
 
1032
1030
        if self.readonly:
1033
1031
            self._copy()
1034
1032
 
1084
1082
        "Set alpha layer"
1085
1083
 
1086
1084
        self.load()
1087
 
 
1088
1085
        if self.readonly:
1089
1086
            self._copy()
1090
1087
 
1142
1139
    def putdata(self, data, scale=1.0, offset=0.0):
1143
1140
        "Put data from a sequence object into an image."
1144
1141
 
1145
 
        self.load() # hmm...
 
1142
        self.load()
 
1143
        if self.readonly:
 
1144
            self._copy()
 
1145
 
1146
1146
        self.im.putdata(data, scale, offset)
1147
1147
 
1148
1148
    ##
1188
1188
        "Set pixel value"
1189
1189
 
1190
1190
        self.load()
 
1191
        if self.readonly:
 
1192
            self._copy()
 
1193
 
1191
1194
        return self.im.putpixel(xy, value)
1192
1195
 
1193
1196
    ##
1240
1243
    #    (cubic spline interpolation in a 4x4 environment).
1241
1244
    #    If omitted, or if the image has mode "1" or "P", it is
1242
1245
    #    set <b>NEAREST</b>.
 
1246
    # @param expand Optional expansion flag.  If true, expands the output
 
1247
    #    image to make it large enough to hold the entire rotated image.
 
1248
    #    If false or omitted, make the output image the same size as the
 
1249
    #    input image.
1243
1250
    # @return An Image object.
1244
1251
 
1245
 
    def rotate(self, angle, resample=NEAREST):
 
1252
    def rotate(self, angle, resample=NEAREST, expand=0):
1246
1253
        "Rotate image.  Angle given as degrees counter-clockwise."
1247
1254
 
 
1255
        if expand:
 
1256
            import math
 
1257
            angle = -angle * math.pi / 180
 
1258
            matrix = [
 
1259
                 math.cos(angle), math.sin(angle), 0.0,
 
1260
                -math.sin(angle), math.cos(angle), 0.0
 
1261
                 ]
 
1262
            def transform(x, y, (a, b, c, d, e, f)=matrix):
 
1263
                return a*x + b*y + c, d*x + e*y + f
 
1264
 
 
1265
            # calculate output size
 
1266
            w, h = self.size
 
1267
            xx = []
 
1268
            yy = []
 
1269
            for x, y in ((0, 0), (w, 0), (w, h), (0, h)):
 
1270
                x, y = transform(x, y)
 
1271
                xx.append(x)
 
1272
                yy.append(y)
 
1273
            w = int(math.ceil(max(xx)) - math.floor(min(xx)))
 
1274
            h = int(math.ceil(max(yy)) - math.floor(min(yy)))
 
1275
 
 
1276
            # adjust center
 
1277
            x, y = transform(w / 2.0, h / 2.0)
 
1278
            matrix[2] = self.size[0] / 2.0 - x
 
1279
            matrix[5] = self.size[1] / 2.0 - y
 
1280
 
 
1281
            return self.transform((w, h), AFFINE, matrix)
 
1282
 
1248
1283
        if resample not in (NEAREST, BILINEAR, BICUBIC):
1249
1284
            raise ValueError("unknown resampling filter")
1250
1285
 
1913
1948
 
1914
1949
    if os.name == "nt":
1915
1950
        format = "BMP"
1916
 
        if not command:
1917
 
            command = "start"
1918
1951
    elif sys.platform == "darwin":
1919
1952
        format = "JPEG"
1920
1953
        if not command:
1939
1972
        file = image._dump(format=format)
1940
1973
 
1941
1974
    if os.name == "nt":
1942
 
        os.system("%s %s" % (command, file))
1943
 
        # FIXME: this leaves temporary files around...
 
1975
        command = "start /wait %s && del /f %s" % (file, file)
1944
1976
    elif sys.platform == "darwin":
1945
1977
        # on darwin open returns immediately resulting in the temp
1946
1978
        # file removal while app is opening
1947
 
        os.system("(%s %s; sleep 20; rm -f %s)&" % (command, file, file))
 
1979
        command = "(%s %s; sleep 20; rm -f %s)&" % (command, file, file)
1948
1980
    else:
1949
 
        os.system("(%s %s; rm -f %s)&" % (command, file, file))
 
1981
        command = "(%s %s; rm -f %s)&" % (command, file, file)
 
1982
 
 
1983
    os.system(command)