mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 18:06:18 +03:00
Simplified code
This commit is contained in:
parent
82d9ea5eac
commit
568fc2def8
|
@ -472,9 +472,10 @@ def truetype(font=None, size=10, index=0, encoding="",
|
||||||
: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):
|
||||||
try:
|
|
||||||
return FreeTypeFont(font, size, index, encoding, layout_engine)
|
return FreeTypeFont(font, size, index, encoding, layout_engine)
|
||||||
|
try:
|
||||||
|
return freetype(font)
|
||||||
except IOError:
|
except IOError:
|
||||||
ttf_filename = os.path.basename(font)
|
ttf_filename = os.path.basename(font)
|
||||||
|
|
||||||
|
@ -504,21 +505,17 @@ def truetype(font=None, size=10, index=0, encoding="",
|
||||||
for walkroot, walkdir, walkfilenames in os.walk(directory):
|
for walkroot, walkdir, walkfilenames in os.walk(directory):
|
||||||
for walkfilename in walkfilenames:
|
for walkfilename in walkfilenames:
|
||||||
if ext and walkfilename == ttf_filename:
|
if ext and walkfilename == ttf_filename:
|
||||||
fontpath = os.path.join(walkroot, walkfilename)
|
return freetype(os.path.join(walkroot, walkfilename))
|
||||||
return FreeTypeFont(fontpath, size, index,
|
|
||||||
encoding, layout_engine)
|
|
||||||
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 FreeTypeFont(fontpath, size, index,
|
return freetype(fontpath)
|
||||||
encoding, layout_engine)
|
|
||||||
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 FreeTypeFont(first_font_with_a_different_extension, size,
|
return freetype(first_font_with_a_different_extension)
|
||||||
index, encoding, layout_engine)
|
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,15 +18,14 @@
|
||||||
from . import Image
|
from . import Image
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
if sys.platform not in ["win32", "darwin"]:
|
|
||||||
raise ImportError("ImageGrab is macOS and Windows only")
|
|
||||||
|
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
grabber = Image.core.grabscreen
|
grabber = Image.core.grabscreen
|
||||||
elif sys.platform == "darwin":
|
elif sys.platform == "darwin":
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
import subprocess
|
import subprocess
|
||||||
|
else:
|
||||||
|
raise ImportError("ImageGrab is macOS and Windows only")
|
||||||
|
|
||||||
|
|
||||||
def grab(bbox=None, include_layered_windows=False):
|
def grab(bbox=None, include_layered_windows=False):
|
||||||
|
|
|
@ -130,9 +130,7 @@ def _save(im, fp, filename):
|
||||||
bpp = 8
|
bpp = 8
|
||||||
version = 1
|
version = 1
|
||||||
|
|
||||||
elif (im.mode == "L" and
|
elif im.mode == "L" and im.encoderinfo.get("bpp") in (1, 2, 4):
|
||||||
"bpp" in im.encoderinfo and
|
|
||||||
im.encoderinfo["bpp"] in (1, 2, 4)):
|
|
||||||
|
|
||||||
# this is 8-bit grayscale, so we shift it to get the high-order bits,
|
# this is 8-bit grayscale, so we shift it to get the high-order bits,
|
||||||
# and invert it because
|
# and invert it because
|
||||||
|
@ -145,7 +143,7 @@ def _save(im, fp, filename):
|
||||||
rawmode = "P;" + str(bpp)
|
rawmode = "P;" + str(bpp)
|
||||||
version = 1
|
version = 1
|
||||||
|
|
||||||
elif im.mode == "L" and "bpp" in im.info and im.info["bpp"] in (1, 2, 4):
|
elif im.mode == "L" and 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.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user