Skip to content

Commit

Permalink
Format with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jun 11, 2019
1 parent 169455f commit cab7231
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/PIL/IcoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/PIL/ImImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))])
Expand Down
46 changes: 26 additions & 20 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand Down
47 changes: 30 additions & 17 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/PIL/ImageGrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from . import Image

import sys

if sys.platform == "win32":
grabber = Image.core.grabscreen
elif sys.platform == "darwin":
Expand Down
5 changes: 3 additions & 2 deletions src/PIL/PalmImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 0 additions & 1 deletion src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/TiffTags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit cab7231

Please sign in to comment.