mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-13 05:06:49 +03:00
Format with Black
This commit is contained in:
parent
169455f924
commit
cab7231e2a
|
@ -153,7 +153,7 @@ class IcoFile(object):
|
||||||
|
|
||||||
def getentryindex(self, size, bpp=False):
|
def getentryindex(self, size, bpp=False):
|
||||||
for (i, h) in enumerate(self.entry):
|
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 i
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@ -297,9 +297,9 @@ class IcoImageFile(ImageFile.ImageFile):
|
||||||
warnings.warn("Image was not the expected size")
|
warnings.warn("Image was not the expected size")
|
||||||
|
|
||||||
index = self.ico.getentryindex(self.size)
|
index = self.ico.getentryindex(self.size)
|
||||||
sizes = list(self.info['sizes'])
|
sizes = list(self.info["sizes"])
|
||||||
sizes[index] = im.size
|
sizes[index] = im.size
|
||||||
self.info['sizes'] = set(sizes)
|
self.info["sizes"] = set(sizes)
|
||||||
|
|
||||||
self.size = im.size
|
self.size = im.size
|
||||||
|
|
||||||
|
|
|
@ -351,12 +351,12 @@ def _save(im, fp, filename):
|
||||||
|
|
||||||
fp.write(("Image type: %s image\r\n" % image_type).encode("ascii"))
|
fp.write(("Image type: %s image\r\n" % image_type).encode("ascii"))
|
||||||
if filename:
|
if filename:
|
||||||
fp.write(("Name: %s\r\n" % filename).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(("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(("File size (no of images): %d\r\n" % frames).encode("ascii"))
|
||||||
if im.mode in ["P", "PA"]:
|
if im.mode in ["P", "PA"]:
|
||||||
fp.write(b"Lut: 1\r\n")
|
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"]:
|
if im.mode in ["P", "PA"]:
|
||||||
fp.write(im.im.getpalette("RGB", "RGB;L")) # 768 bytes
|
fp.write(im.im.getpalette("RGB", "RGB;L")) # 768 bytes
|
||||||
ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, -1))])
|
ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, -1))])
|
||||||
|
|
|
@ -1824,21 +1824,23 @@ class Image(object):
|
||||||
:returns: An :py:class:`~PIL.Image.Image` object.
|
:returns: An :py:class:`~PIL.Image.Image` object.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if resample not in (
|
if resample not in (NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING):
|
||||||
NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING,
|
|
||||||
):
|
|
||||||
message = "Unknown resampling filter ({}).".format(resample)
|
message = "Unknown resampling filter ({}).".format(resample)
|
||||||
|
|
||||||
filters = ["{} ({})".format(filter[1], filter[0]) for filter in (
|
filters = [
|
||||||
(NEAREST, "Image.NEAREST"),
|
"{} ({})".format(filter[1], filter[0])
|
||||||
(LANCZOS, "Image.LANCZOS"),
|
for filter in (
|
||||||
(BILINEAR, "Image.BILINEAR"),
|
(NEAREST, "Image.NEAREST"),
|
||||||
(BICUBIC, "Image.BICUBIC"),
|
(LANCZOS, "Image.LANCZOS"),
|
||||||
(BOX, "Image.BOX"),
|
(BILINEAR, "Image.BILINEAR"),
|
||||||
(HAMMING, "Image.HAMMING")
|
(BICUBIC, "Image.BICUBIC"),
|
||||||
)]
|
(BOX, "Image.BOX"),
|
||||||
|
(HAMMING, "Image.HAMMING"),
|
||||||
|
)
|
||||||
|
]
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
message+" Use "+", ".join(filters[:-1])+" or "+filters[-1])
|
message + " Use " + ", ".join(filters[:-1]) + " or " + filters[-1]
|
||||||
|
)
|
||||||
|
|
||||||
size = tuple(size)
|
size = tuple(size)
|
||||||
|
|
||||||
|
@ -2330,18 +2332,22 @@ class Image(object):
|
||||||
message = {
|
message = {
|
||||||
BOX: "Image.BOX",
|
BOX: "Image.BOX",
|
||||||
HAMMING: "Image.HAMMING",
|
HAMMING: "Image.HAMMING",
|
||||||
LANCZOS: "Image.LANCZOS/Image.ANTIALIAS"
|
LANCZOS: "Image.LANCZOS/Image.ANTIALIAS",
|
||||||
}[resample]+" ({}) cannot be used.".format(resample)
|
}[resample] + " ({}) cannot be used.".format(resample)
|
||||||
else:
|
else:
|
||||||
message = "Unknown resampling filter ({}).".format(resample)
|
message = "Unknown resampling filter ({}).".format(resample)
|
||||||
|
|
||||||
filters = ["{} ({})".format(filter[1], filter[0]) for filter in (
|
filters = [
|
||||||
(NEAREST, "Image.NEAREST"),
|
"{} ({})".format(filter[1], filter[0])
|
||||||
(BILINEAR, "Image.BILINEAR"),
|
for filter in (
|
||||||
(BICUBIC, "Image.BICUBIC")
|
(NEAREST, "Image.NEAREST"),
|
||||||
)]
|
(BILINEAR, "Image.BILINEAR"),
|
||||||
|
(BICUBIC, "Image.BICUBIC"),
|
||||||
|
)
|
||||||
|
]
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
message+" Use "+", ".join(filters[:-1])+" or "+filters[-1])
|
message + " Use " + ", ".join(filters[:-1]) + " or " + filters[-1]
|
||||||
|
)
|
||||||
|
|
||||||
image.load()
|
image.load()
|
||||||
|
|
||||||
|
|
|
@ -143,21 +143,23 @@ class FreeTypeFont(object):
|
||||||
def load_from_bytes(f):
|
def load_from_bytes(f):
|
||||||
self.font_bytes = f.read()
|
self.font_bytes = f.read()
|
||||||
self.font = core.getfont(
|
self.font = core.getfont(
|
||||||
"", size, index, encoding, self.font_bytes, layout_engine)
|
"", size, index, encoding, self.font_bytes, layout_engine
|
||||||
|
)
|
||||||
|
|
||||||
if isPath(font):
|
if isPath(font):
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
font_bytes_path = font if isinstance(font, bytes) else font.encode()
|
font_bytes_path = font if isinstance(font, bytes) else font.encode()
|
||||||
try:
|
try:
|
||||||
font_bytes_path.decode('ascii')
|
font_bytes_path.decode("ascii")
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
# FreeType cannot load fonts with non-ASCII characters on Windows
|
# FreeType cannot load fonts with non-ASCII characters on Windows
|
||||||
# So load it into memory first
|
# So load it into memory first
|
||||||
with open(font, 'rb') as f:
|
with open(font, "rb") as f:
|
||||||
load_from_bytes(f)
|
load_from_bytes(f)
|
||||||
return
|
return
|
||||||
self.font = core.getfont(font, size, index, encoding,
|
self.font = core.getfont(
|
||||||
layout_engine=layout_engine)
|
font, size, index, encoding, layout_engine=layout_engine
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
load_from_bytes(font)
|
load_from_bytes(font)
|
||||||
|
|
||||||
|
@ -221,8 +223,9 @@ class FreeTypeFont(object):
|
||||||
size, offset = self.font.getsize(text, direction, features, language)
|
size, offset = self.font.getsize(text, direction, features, language)
|
||||||
return (size[0] + offset[0], size[1] + offset[1])
|
return (size[0] + offset[0], size[1] + offset[1])
|
||||||
|
|
||||||
def getsize_multiline(self, text, direction=None, spacing=4,
|
def getsize_multiline(
|
||||||
features=None, language=None):
|
self, text, direction=None, spacing=4, features=None, language=None
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Returns width and height (in pixels) of given text if rendered in font
|
Returns width and height (in pixels) of given text if rendered in font
|
||||||
with provided direction, features, and language, while respecting
|
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
|
:return: An internal PIL storage memory instance as defined by the
|
||||||
:py:mod:`PIL.Image.core` interface module.
|
:py:mod:`PIL.Image.core` interface module.
|
||||||
"""
|
"""
|
||||||
return self.getmask2(text, mode, direction=direction, features=features,
|
return self.getmask2(
|
||||||
language=language)[0]
|
text, mode, direction=direction, features=features, language=language
|
||||||
|
)[0]
|
||||||
|
|
||||||
def getmask2(self, text, mode="", fill=Image.core.fill, direction=None,
|
def getmask2(
|
||||||
features=None, language=None, *args, **kwargs):
|
self,
|
||||||
|
text,
|
||||||
|
mode="",
|
||||||
|
fill=Image.core.fill,
|
||||||
|
direction=None,
|
||||||
|
features=None,
|
||||||
|
language=None,
|
||||||
|
*args,
|
||||||
|
**kwargs
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Create a bitmap for the text.
|
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.
|
:return: A font object.
|
||||||
:exception IOError: If the file could not be read.
|
:exception IOError: If the file could not be read.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def freetype(font):
|
def freetype(font):
|
||||||
return FreeTypeFont(font, size, index, encoding, layout_engine)
|
return FreeTypeFont(font, size, index, encoding, layout_engine)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return freetype(font)
|
return freetype(font)
|
||||||
except IOError:
|
except IOError:
|
||||||
|
@ -508,13 +523,11 @@ def truetype(font=None, size=10, index=0, encoding="", layout_engine=None):
|
||||||
for walkfilename in walkfilenames:
|
for walkfilename in walkfilenames:
|
||||||
if ext and walkfilename == ttf_filename:
|
if ext and walkfilename == ttf_filename:
|
||||||
return freetype(os.path.join(walkroot, walkfilename))
|
return freetype(os.path.join(walkroot, walkfilename))
|
||||||
elif (not ext and
|
elif not ext and os.path.splitext(walkfilename)[0] == ttf_filename:
|
||||||
os.path.splitext(walkfilename)[0] == ttf_filename):
|
|
||||||
fontpath = os.path.join(walkroot, walkfilename)
|
fontpath = os.path.join(walkroot, walkfilename)
|
||||||
if os.path.splitext(fontpath)[1] == '.ttf':
|
if os.path.splitext(fontpath)[1] == ".ttf":
|
||||||
return freetype(fontpath)
|
return freetype(fontpath)
|
||||||
if not ext \
|
if not ext and first_font_with_a_different_extension is None:
|
||||||
and first_font_with_a_different_extension is None:
|
|
||||||
first_font_with_a_different_extension = fontpath
|
first_font_with_a_different_extension = fontpath
|
||||||
if first_font_with_a_different_extension:
|
if first_font_with_a_different_extension:
|
||||||
return freetype(first_font_with_a_different_extension)
|
return freetype(first_font_with_a_different_extension)
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
from . import Image
|
from . import Image
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
grabber = Image.core.grabscreen
|
grabber = Image.core.grabscreen
|
||||||
elif sys.platform == "darwin":
|
elif sys.platform == "darwin":
|
||||||
|
|
|
@ -132,13 +132,14 @@ def _save(im, fp, filename):
|
||||||
# Palm does greyscale from white (0) to black (1)
|
# Palm does greyscale from white (0) to black (1)
|
||||||
bpp = im.encoderinfo["bpp"]
|
bpp = im.encoderinfo["bpp"]
|
||||||
im = im.point(
|
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):
|
elif im.info.get("bpp") in (1, 2, 4):
|
||||||
# here we assume that even though the inherent mode is 8-bit grayscale,
|
# here we assume that even though the inherent mode is 8-bit grayscale,
|
||||||
# only the lower bpp bits are significant.
|
# only the lower bpp bits are significant.
|
||||||
# We invert them to match the Palm.
|
# We invert them to match the Palm.
|
||||||
bpp = im.info["bpp"]
|
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:
|
else:
|
||||||
raise IOError("cannot write mode %s as Palm" % im.mode)
|
raise IOError("cannot write mode %s as Palm" % im.mode)
|
||||||
|
|
||||||
|
|
|
@ -251,7 +251,6 @@ OPEN_INFO = {
|
||||||
(MM, 5, (1,), 1, (8, 8, 8, 8, 8), (0,)): ("CMYK", "CMYKX"),
|
(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"),
|
(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"),
|
(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"),
|
(II, 5, (1,), 1, (16, 16, 16, 16), ()): ("CMYK", "CMYK;16L"),
|
||||||
# JPEG compressed images handled by LibTiff and auto-converted to RGBX
|
# JPEG compressed images handled by LibTiff and auto-converted to RGBX
|
||||||
# Minimal Baseline TIFF requires YCbCr images to have 3 SamplesPerPixel
|
# Minimal Baseline TIFF requires YCbCr images to have 3 SamplesPerPixel
|
||||||
|
|
|
@ -104,9 +104,9 @@ TAGS_V2 = {
|
||||||
"YCbCr": 6,
|
"YCbCr": 6,
|
||||||
"CieLAB": 8,
|
"CieLAB": 8,
|
||||||
"CFA": 32803, # TIFF/EP, Adobe DNG
|
"CFA": 32803, # TIFF/EP, Adobe DNG
|
||||||
"LinearRaw": 32892,
|
"LinearRaw": 32892, # Adobe DNG
|
||||||
},
|
},
|
||||||
), # Adobe DNG
|
),
|
||||||
263: ("Threshholding", SHORT, 1),
|
263: ("Threshholding", SHORT, 1),
|
||||||
264: ("CellWidth", SHORT, 1),
|
264: ("CellWidth", SHORT, 1),
|
||||||
265: ("CellLength", SHORT, 1),
|
265: ("CellLength", SHORT, 1),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user