Format with Black

This commit is contained in:
Hugo 2019-06-11 11:42:05 +03:00
parent 169455f924
commit cab7231e2a
8 changed files with 68 additions and 48 deletions

View File

@ -153,7 +153,7 @@ class IcoFile(object):
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 @@ class IcoImageFile(ImageFile.ImageFile):
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

View File

@ -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))])

View File

@ -1824,21 +1824,23 @@ class Image(object):
: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 @@ class Image(object):
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()

View File

@ -143,21 +143,23 @@ class FreeTypeFont(object):
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 @@ class FreeTypeFont(object):
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 @@ class FreeTypeFont(object):
: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]
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):
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 truetype(font=None, size=10, index=0, encoding="", layout_engine=None):
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)

View File

@ -18,6 +18,7 @@
from . import Image
import sys
if sys.platform == "win32":
grabber = Image.core.grabscreen
elif sys.platform == "darwin":

View File

@ -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)

View File

@ -251,7 +251,6 @@ OPEN_INFO = {
(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

View File

@ -104,9 +104,9 @@ TAGS_V2 = {
"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),