From 31a33e35cea2a0c88c1e8e89b06203166d8f80c4 Mon Sep 17 00:00:00 2001 From: amoibos Date: Thu, 1 May 2014 20:22:10 +0200 Subject: [PATCH 1/3] Update Image.py removed unnecessary self.load call --- PIL/Image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 333397701..8d278317e 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -918,10 +918,10 @@ class Image: :returns: An :py:class:`~PIL.Image.Image` object. """ - self.load() if box is None: return self.copy() - + else: + self.load() # lazy operation return _ImageCrop(self, box) From 050e81221479707a962a8a6df64c34c03a6fe286 Mon Sep 17 00:00:00 2001 From: Daniel Oelschlegel Date: Thu, 1 May 2014 21:10:27 +0200 Subject: [PATCH 2/3] cleaned up double or replaced slow string addition --- PIL/BmpImagePlugin.py | 2 +- PIL/IcoImagePlugin.py | 2 +- PIL/ImImagePlugin.py | 6 +++--- PIL/ImageOps.py | 6 +++--- PIL/IptcImagePlugin.py | 2 +- PIL/JpegImagePlugin.py | 2 +- PIL/OleFileIO.py | 8 ++++---- PIL/PngImagePlugin.py | 5 +++-- PIL/SgiImagePlugin.py | 2 +- PIL/TiffImagePlugin.py | 2 +- 10 files changed, 19 insertions(+), 18 deletions(-) diff --git a/PIL/BmpImagePlugin.py b/PIL/BmpImagePlugin.py index 436ca5dce..cd6dc49a2 100644 --- a/PIL/BmpImagePlugin.py +++ b/PIL/BmpImagePlugin.py @@ -133,7 +133,7 @@ class BmpImageFile(ImageFile.ImageFile): greyscale = 1 if colors == 2: indices = (0, 255) - elif colors > 2**16 or colors <=0: #We're reading a i32. + elif not 0 < colors <= 2**16: #We're reading a i32. raise IOError("Unsupported BMP Palette size (%d)" % colors) else: indices = list(range(colors)) diff --git a/PIL/IcoImagePlugin.py b/PIL/IcoImagePlugin.py index 268e93d6c..b7f3cf16a 100644 --- a/PIL/IcoImagePlugin.py +++ b/PIL/IcoImagePlugin.py @@ -102,7 +102,7 @@ class IcoFile: Get an image from the icon """ for (i, h) in enumerate(self.entry): - if size == h['dim'] and (bpp == False or bpp == h['color_depth']): + if size == h['dim'] and bpp in (h['color_depth'], False): return self.frame(i) return self.frame(0) diff --git a/PIL/ImImagePlugin.py b/PIL/ImImagePlugin.py index 1f8f011dc..aab0c13a9 100644 --- a/PIL/ImImagePlugin.py +++ b/PIL/ImImagePlugin.py @@ -134,7 +134,7 @@ class ImImageFile(ImageFile.ImageFile): if s == b"\r": continue - if not s or s == b'\0' or s == b'\x1A': + if not s or s in (b'\0', b'\x1A'): break # FIXME: this may read whole file if not a text file @@ -212,7 +212,7 @@ class ImImageFile(ImageFile.ImageFile): linear = 0 else: greyscale = 0 - if self.mode == "L" or self.mode == "LA": + if self.mode in ("L", "LA"): if greyscale: if not linear: self.lut = [i8(c) for c in palette[:256]] @@ -258,7 +258,7 @@ class ImImageFile(ImageFile.ImageFile): def seek(self, frame): - if frame < 0 or frame >= self.info[FRAMES]: + if not 0 <= frame < self.info[FRAMES]: raise EOFError("seek outside sequence") if self.frame == frame: diff --git a/PIL/ImageOps.py b/PIL/ImageOps.py index 0d22f8c64..b0e35fa00 100644 --- a/PIL/ImageOps.py +++ b/PIL/ImageOps.py @@ -272,12 +272,12 @@ def fit(image, size, method=Image.NEAREST, bleed=0.0, centering=(0.5, 0.5)): if not isinstance(centering, list): centering = [centering[0], centering[1]] - if centering[0] > 1.0 or centering[0] < 0.0: + if not 0.0 < centering[0] < 1.0: centering [0] = 0.50 - if centering[1] > 1.0 or centering[1] < 0.0: + if not 0.0 < centering[1] < 1.0: centering[1] = 0.50 - if bleed > 0.49999 or bleed < 0.0: + if not 0.0 < bleed < 0.49999: bleed = 0.0 # calculate the area to use for resizing and cropping, subtracting diff --git a/PIL/IptcImagePlugin.py b/PIL/IptcImagePlugin.py index 104153002..6de7de466 100644 --- a/PIL/IptcImagePlugin.py +++ b/PIL/IptcImagePlugin.py @@ -68,7 +68,7 @@ class IptcImageFile(ImageFile.ImageFile): tag = i8(s[1]), i8(s[2]) # syntax - if i8(s[0]) != 0x1C or tag[0] < 1 or tag[0] > 9: + if i8(s[0]) != 0x1C or not 1 <= tag[0] <= 9: raise SyntaxError("invalid IPTC/NAA file") # field size diff --git a/PIL/JpegImagePlugin.py b/PIL/JpegImagePlugin.py index da52006ca..d727863e0 100644 --- a/PIL/JpegImagePlugin.py +++ b/PIL/JpegImagePlugin.py @@ -307,7 +307,7 @@ class JpegImageFile(ImageFile.ImageFile): # self.__offset = self.fp.tell() break s = self.fp.read(1) - elif i == 0 or i == 65535: + elif i in (0, 65535): # padded marker or junk; move on s = "\xff" else: diff --git a/PIL/OleFileIO.py b/PIL/OleFileIO.py index 8a3c77be4..a75558a5a 100644 --- a/PIL/OleFileIO.py +++ b/PIL/OleFileIO.py @@ -699,7 +699,7 @@ class _OleStream(io.BytesIO): debug('sect=ENDOFCHAIN before expected size') raise IOError('incomplete OLE stream') # sector index should be within FAT: - if sect<0 or sect>=len(fat): + if not 0 <= sect < len(fat): debug('sect=%d (%X) / len(fat)=%d' % (sect, sect, len(fat))) debug('i=%d / nb_sectors=%d' %(i, nb_sectors)) ## tmp_data = b"".join(data) @@ -920,7 +920,7 @@ class _OleDirectoryEntry: if child_sid == NOSTREAM: return # check if child SID is in the proper range: - if child_sid<0 or child_sid>=len(self.olefile.direntries): + if not 0 <= child_sid < len(self.olefile.direntries): self.olefile._raise_defect(DEFECT_FATAL, 'OLE DirEntry index out of range') # get child direntry: child = self.olefile._load_direntry(child_sid) #direntries[child_sid] @@ -1385,7 +1385,7 @@ class OleFileIO: # The FAT is a sector chain starting at the first index of itself. for isect in fat1: #print("isect = %X" % isect) - if isect == ENDOFCHAIN or isect == FREESECT: + if isect in (ENDOFCHAIN, FREESECT): # the end of the sector chain has been reached break # read the FAT sector @@ -1572,7 +1572,7 @@ class OleFileIO: raise: IOError if the entry has always been referenced. """ # check if SID is OK: - if sid<0 or sid>=len(self.direntries): + if 0 <= sid < len(self.direntries): self._raise_defect(DEFECT_FATAL, "OLE directory index out of range") # check if entry was already referenced: if self.direntries[sid] is not None: diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index 2bdf74608..619ccfc20 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -555,8 +555,9 @@ def _save(im, fp, filename, chunk=putchunk, check=0): if im.mode == "P": palette_byte_number = (2 ** bits) * 3 palette_bytes = im.im.getpalette("RGB")[:palette_byte_number] - while len(palette_bytes) < palette_byte_number: - palette_bytes += b'\0' + diff = palette_byte_number - len(palette_bytes) + if diff > 0: + palette_bytes += b'\0' * diff chunk(fp, b"PLTE", palette_bytes) transparency = im.encoderinfo.get('transparency',im.info.get('transparency', None)) diff --git a/PIL/SgiImagePlugin.py b/PIL/SgiImagePlugin.py index b60df473c..484f2df72 100644 --- a/PIL/SgiImagePlugin.py +++ b/PIL/SgiImagePlugin.py @@ -53,7 +53,7 @@ class SgiImageFile(ImageFile.ImageFile): layout = i8(s[3]), i16(s[4:]), i16(s[10:]) # determine mode from bytes/zsize - if layout == (1, 2, 1) or layout == (1, 1, 1): + if layout in ((1, 2, 1), (1, 1, 1)): self.mode = "L" elif layout == (1, 3, 3): self.mode = "RGB" diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index fe658d22c..aab67fe6d 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -1043,7 +1043,7 @@ def _save(im, fp, filename): unit = im.encoderinfo["resolution unit"] if unit == "inch": ifd[RESOLUTION_UNIT] = 2 - elif unit == "cm" or unit == "centimeter": + elif unit in ("cm", "centimeter"): ifd[RESOLUTION_UNIT] = 3 else: ifd[RESOLUTION_UNIT] = 1 From fe72e26f94cc40837e2bd15f5e782ae048ce3b5c Mon Sep 17 00:00:00 2001 From: Daniel Oelschlegel Date: Thu, 1 May 2014 23:01:50 +0200 Subject: [PATCH 3/3] new style class definition performance improvements through using of list comprehension --- PIL/BmpImagePlugin.py | 3 +-- PIL/Image.py | 6 +++--- PIL/ImageOps.py | 29 +++++++---------------------- PIL/ImagePalette.py | 21 ++++++--------------- PIL/ImagePath.py | 2 +- PIL/ImageQt.py | 6 ++---- PIL/ImageSequence.py | 2 +- PIL/ImageShow.py | 4 ++-- PIL/ImageStat.py | 2 +- PIL/ImageTk.py | 4 ++-- PIL/ImageWin.py | 8 ++++---- PIL/PcxImagePlugin.py | 3 +-- 12 files changed, 31 insertions(+), 59 deletions(-) diff --git a/PIL/BmpImagePlugin.py b/PIL/BmpImagePlugin.py index cd6dc49a2..386010e50 100644 --- a/PIL/BmpImagePlugin.py +++ b/PIL/BmpImagePlugin.py @@ -232,8 +232,7 @@ def _save(im, fp, filename, check=0): for i in (0, 255): fp.write(o8(i) * 4) elif im.mode == "L": - for i in range(256): - fp.write(o8(i) * 4) + fp.write("".join(o8(i) * 4 for i in range(256))) elif im.mode == "P": fp.write(im.im.getpalette("RGB", "BGRX")) diff --git a/PIL/Image.py b/PIL/Image.py index 8d278317e..45480a7ee 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -233,7 +233,7 @@ _MODE_CONV = { "CMYK": ('|u1', 4), "YCbCr": ('|u1', 3), "LAB": ('|u1', 3), # UNDONE - unsigned |u1i1i1 - # I;16 == I;16L, and I;32 == I;32L + # I;16 == I;16L, and I;32 == I;32L "I;16": ('u2', None), "I;16L": ('= (3, 3)): +if sys.version_info >= (3, 3): from shlex import quote else: from pipes import quote @@ -52,7 +52,7 @@ def show(image, title=None, **options): ## # Base class for viewers. -class Viewer: +class Viewer(object): # main api diff --git a/PIL/ImageStat.py b/PIL/ImageStat.py index ef63b7857..e5ba3070b 100644 --- a/PIL/ImageStat.py +++ b/PIL/ImageStat.py @@ -26,7 +26,7 @@ import operator, math from functools import reduce -class Stat: +class Stat(object): def __init__(self, image_or_list, mask = None): try: diff --git a/PIL/ImageTk.py b/PIL/ImageTk.py index 1e81d240e..22653c159 100644 --- a/PIL/ImageTk.py +++ b/PIL/ImageTk.py @@ -54,7 +54,7 @@ def _pilbitmap_check(): # -------------------------------------------------------------------- # PhotoImage -class PhotoImage: +class PhotoImage(object): """ A Tkinter-compatible photo image. This can be used everywhere Tkinter expects an image object. If the image is an RGBA @@ -192,7 +192,7 @@ class PhotoImage: # BitmapImage -class BitmapImage: +class BitmapImage(object): """ A Tkinter-compatible bitmap image. This can be used everywhere Tkinter diff --git a/PIL/ImageWin.py b/PIL/ImageWin.py index aa90b887b..caaca822f 100644 --- a/PIL/ImageWin.py +++ b/PIL/ImageWin.py @@ -21,7 +21,7 @@ import warnings from PIL import Image -class HDC: +class HDC(object): """ Wraps a HDC integer. The resulting object can be passed to the :py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose` @@ -32,7 +32,7 @@ class HDC: def __int__(self): return self.dc -class HWND: +class HWND(object): """ Wraps a HWND integer. The resulting object can be passed to the :py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose` @@ -44,7 +44,7 @@ class HWND: return self.wnd -class Dib: +class Dib(object): """ A Windows bitmap with the given mode and size. The mode can be one of "1", "L", "P", or "RGB". @@ -207,7 +207,7 @@ class Dib: ## # Create a Window with the given title size. -class Window: +class Window(object): def __init__(self, title="PIL", width=None, height=None): self.hwnd = Image.core.createwindow( diff --git a/PIL/PcxImagePlugin.py b/PIL/PcxImagePlugin.py index 2496af676..c7025fecc 100644 --- a/PIL/PcxImagePlugin.py +++ b/PIL/PcxImagePlugin.py @@ -173,8 +173,7 @@ def _save(im, fp, filename, check=0): elif im.mode == "L": # greyscale palette fp.write(o8(12)) - for i in range(256): - fp.write(o8(i)*3) + fp.write("".join(o8(i)*3 for i in range(256))) # -------------------------------------------------------------------- # registry