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