From cab7231e2ab92126a05beb06c92f11a1724acc74 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 11 Jun 2019 11:42:05 +0300 Subject: [PATCH] Format with Black --- src/PIL/IcoImagePlugin.py | 6 ++--- src/PIL/ImImagePlugin.py | 8 +++---- src/PIL/Image.py | 46 +++++++++++++++++++++---------------- src/PIL/ImageFont.py | 47 ++++++++++++++++++++++++-------------- src/PIL/ImageGrab.py | 1 + src/PIL/PalmImagePlugin.py | 5 ++-- src/PIL/TiffImagePlugin.py | 1 - src/PIL/TiffTags.py | 4 ++-- 8 files changed, 69 insertions(+), 49 deletions(-) diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py index b520f194b5e..5ffc86ea7a2 100644 --- a/src/PIL/IcoImagePlugin.py +++ b/src/PIL/IcoImagePlugin.py @@ -153,7 +153,7 @@ def sizes(self): def getentryindex(self, size, bpp=False): for (i, h) in enumerate(self.entry): - if size == h['dim'] and (bpp is False or bpp == h['color_depth']): + if size == h["dim"] and (bpp is False or bpp == h["color_depth"]): return i return 0 @@ -297,9 +297,9 @@ def load(self): warnings.warn("Image was not the expected size") index = self.ico.getentryindex(self.size) - sizes = list(self.info['sizes']) + sizes = list(self.info["sizes"]) sizes[index] = im.size - self.info['sizes'] = set(sizes) + self.info["sizes"] = set(sizes) self.size = im.size diff --git a/src/PIL/ImImagePlugin.py b/src/PIL/ImImagePlugin.py index 1f6a631f918..46deb29a020 100644 --- a/src/PIL/ImImagePlugin.py +++ b/src/PIL/ImImagePlugin.py @@ -351,12 +351,12 @@ def _save(im, fp, filename): fp.write(("Image type: %s image\r\n" % image_type).encode("ascii")) if filename: - fp.write(("Name: %s\r\n" % filename).encode('ascii')) - fp.write(("Image size (x*y): %d*%d\r\n" % im.size).encode('ascii')) - fp.write(("File size (no of images): %d\r\n" % frames).encode('ascii')) + fp.write(("Name: %s\r\n" % filename).encode("ascii")) + fp.write(("Image size (x*y): %d*%d\r\n" % im.size).encode("ascii")) + fp.write(("File size (no of images): %d\r\n" % frames).encode("ascii")) if im.mode in ["P", "PA"]: fp.write(b"Lut: 1\r\n") - fp.write(b"\000" * (511-fp.tell()) + b"\032") + fp.write(b"\000" * (511 - fp.tell()) + b"\032") if im.mode in ["P", "PA"]: fp.write(im.im.getpalette("RGB", "RGB;L")) # 768 bytes ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, -1))]) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 0ba9a4ddd00..ee7d2c523f6 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1824,21 +1824,23 @@ def resize(self, size, resample=NEAREST, box=None): :returns: An :py:class:`~PIL.Image.Image` object. """ - if resample not in ( - NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING, - ): + if resample not in (NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING): message = "Unknown resampling filter ({}).".format(resample) - filters = ["{} ({})".format(filter[1], filter[0]) for filter in ( - (NEAREST, "Image.NEAREST"), - (LANCZOS, "Image.LANCZOS"), - (BILINEAR, "Image.BILINEAR"), - (BICUBIC, "Image.BICUBIC"), - (BOX, "Image.BOX"), - (HAMMING, "Image.HAMMING") - )] + filters = [ + "{} ({})".format(filter[1], filter[0]) + for filter in ( + (NEAREST, "Image.NEAREST"), + (LANCZOS, "Image.LANCZOS"), + (BILINEAR, "Image.BILINEAR"), + (BICUBIC, "Image.BICUBIC"), + (BOX, "Image.BOX"), + (HAMMING, "Image.HAMMING"), + ) + ] raise ValueError( - message+" Use "+", ".join(filters[:-1])+" or "+filters[-1]) + message + " Use " + ", ".join(filters[:-1]) + " or " + filters[-1] + ) size = tuple(size) @@ -2330,18 +2332,22 @@ def __transformer(self, box, image, method, data, resample=NEAREST, fill=1): message = { BOX: "Image.BOX", HAMMING: "Image.HAMMING", - LANCZOS: "Image.LANCZOS/Image.ANTIALIAS" - }[resample]+" ({}) cannot be used.".format(resample) + LANCZOS: "Image.LANCZOS/Image.ANTIALIAS", + }[resample] + " ({}) cannot be used.".format(resample) else: message = "Unknown resampling filter ({}).".format(resample) - filters = ["{} ({})".format(filter[1], filter[0]) for filter in ( - (NEAREST, "Image.NEAREST"), - (BILINEAR, "Image.BILINEAR"), - (BICUBIC, "Image.BICUBIC") - )] + filters = [ + "{} ({})".format(filter[1], filter[0]) + for filter in ( + (NEAREST, "Image.NEAREST"), + (BILINEAR, "Image.BILINEAR"), + (BICUBIC, "Image.BICUBIC"), + ) + ] raise ValueError( - message+" Use "+", ".join(filters[:-1])+" or "+filters[-1]) + message + " Use " + ", ".join(filters[:-1]) + " or " + filters[-1] + ) image.load() diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 638a104c158..3cdd5d80a33 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -143,21 +143,23 @@ def __init__(self, font=None, size=10, index=0, encoding="", layout_engine=None) def load_from_bytes(f): self.font_bytes = f.read() self.font = core.getfont( - "", size, index, encoding, self.font_bytes, layout_engine) + "", size, index, encoding, self.font_bytes, layout_engine + ) if isPath(font): if sys.platform == "win32": font_bytes_path = font if isinstance(font, bytes) else font.encode() try: - font_bytes_path.decode('ascii') + font_bytes_path.decode("ascii") except UnicodeDecodeError: # FreeType cannot load fonts with non-ASCII characters on Windows # So load it into memory first - with open(font, 'rb') as f: + with open(font, "rb") as f: load_from_bytes(f) return - self.font = core.getfont(font, size, index, encoding, - layout_engine=layout_engine) + self.font = core.getfont( + font, size, index, encoding, layout_engine=layout_engine + ) else: load_from_bytes(font) @@ -221,8 +223,9 @@ def getsize(self, text, direction=None, features=None, language=None): size, offset = self.font.getsize(text, direction, features, language) return (size[0] + offset[0], size[1] + offset[1]) - def getsize_multiline(self, text, direction=None, spacing=4, - features=None, language=None): + def getsize_multiline( + self, text, direction=None, spacing=4, features=None, language=None + ): """ Returns width and height (in pixels) of given text if rendered in font with provided direction, features, and language, while respecting @@ -327,11 +330,21 @@ def getmask(self, text, mode="", direction=None, features=None, language=None): :return: An internal PIL storage memory instance as defined by the :py:mod:`PIL.Image.core` interface module. """ - return self.getmask2(text, mode, direction=direction, features=features, - language=language)[0] - - def getmask2(self, text, mode="", fill=Image.core.fill, direction=None, - features=None, language=None, *args, **kwargs): + return self.getmask2( + text, mode, direction=direction, features=features, language=language + )[0] + + def getmask2( + self, + text, + mode="", + fill=Image.core.fill, + direction=None, + features=None, + language=None, + *args, + **kwargs + ): """ Create a bitmap for the text. @@ -472,8 +485,10 @@ def truetype(font=None, size=10, index=0, encoding="", layout_engine=None): :return: A font object. :exception IOError: If the file could not be read. """ + def freetype(font): return FreeTypeFont(font, size, index, encoding, layout_engine) + try: return freetype(font) except IOError: @@ -508,13 +523,11 @@ def freetype(font): for walkfilename in walkfilenames: if ext and walkfilename == ttf_filename: return freetype(os.path.join(walkroot, walkfilename)) - elif (not ext and - os.path.splitext(walkfilename)[0] == ttf_filename): + elif not ext and os.path.splitext(walkfilename)[0] == ttf_filename: fontpath = os.path.join(walkroot, walkfilename) - if os.path.splitext(fontpath)[1] == '.ttf': + if os.path.splitext(fontpath)[1] == ".ttf": return freetype(fontpath) - if not ext \ - and first_font_with_a_different_extension is None: + if not ext and first_font_with_a_different_extension is None: first_font_with_a_different_extension = fontpath if first_font_with_a_different_extension: return freetype(first_font_with_a_different_extension) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index c1de31fc242..faaa6546759 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -18,6 +18,7 @@ from . import Image import sys + if sys.platform == "win32": grabber = Image.core.grabscreen elif sys.platform == "darwin": diff --git a/src/PIL/PalmImagePlugin.py b/src/PIL/PalmImagePlugin.py index 3e0b05ee280..dd068d79412 100644 --- a/src/PIL/PalmImagePlugin.py +++ b/src/PIL/PalmImagePlugin.py @@ -132,13 +132,14 @@ def _save(im, fp, filename): # Palm does greyscale from white (0) to black (1) bpp = im.encoderinfo["bpp"] im = im.point( - lambda x, shift=8-bpp, maxval=(1 << bpp)-1: maxval - (x >> shift)) + lambda x, shift=8 - bpp, maxval=(1 << bpp) - 1: maxval - (x >> shift) + ) elif im.info.get("bpp") in (1, 2, 4): # here we assume that even though the inherent mode is 8-bit grayscale, # only the lower bpp bits are significant. # We invert them to match the Palm. bpp = im.info["bpp"] - im = im.point(lambda x, maxval=(1 << bpp)-1: maxval - (x & maxval)) + im = im.point(lambda x, maxval=(1 << bpp) - 1: maxval - (x & maxval)) else: raise IOError("cannot write mode %s as Palm" % im.mode) diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 6a6016bea63..7596aae655f 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -251,7 +251,6 @@ (MM, 5, (1,), 1, (8, 8, 8, 8, 8), (0,)): ("CMYK", "CMYKX"), (II, 5, (1,), 1, (8, 8, 8, 8, 8, 8), (0, 0)): ("CMYK", "CMYKXX"), (MM, 5, (1,), 1, (8, 8, 8, 8, 8, 8), (0, 0)): ("CMYK", "CMYKXX"), - (II, 5, (1,), 1, (16, 16, 16, 16), ()): ("CMYK", "CMYK;16L"), # JPEG compressed images handled by LibTiff and auto-converted to RGBX # Minimal Baseline TIFF requires YCbCr images to have 3 SamplesPerPixel diff --git a/src/PIL/TiffTags.py b/src/PIL/TiffTags.py index 5482ad214eb..d0c98aa5acd 100644 --- a/src/PIL/TiffTags.py +++ b/src/PIL/TiffTags.py @@ -104,9 +104,9 @@ def lookup(tag): "YCbCr": 6, "CieLAB": 8, "CFA": 32803, # TIFF/EP, Adobe DNG - "LinearRaw": 32892, + "LinearRaw": 32892, # Adobe DNG }, - ), # Adobe DNG + ), 263: ("Threshholding", SHORT, 1), 264: ("CellWidth", SHORT, 1), 265: ("CellLength", SHORT, 1),