Simplified code

This commit is contained in:
Andrew Murray 2019-04-25 16:56:18 +10:00
parent 82d9ea5eac
commit 568fc2def8
3 changed files with 10 additions and 16 deletions

View File

@ -472,9 +472,10 @@ def truetype(font=None, size=10, index=0, encoding="",
:return: A font object.
:exception IOError: If the file could not be read.
"""
try:
def freetype(font):
return FreeTypeFont(font, size, index, encoding, layout_engine)
try:
return freetype(font)
except IOError:
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 walkfilename in walkfilenames:
if ext and walkfilename == ttf_filename:
fontpath = os.path.join(walkroot, walkfilename)
return FreeTypeFont(fontpath, size, index,
encoding, layout_engine)
return freetype(os.path.join(walkroot, walkfilename))
elif (not ext and
os.path.splitext(walkfilename)[0] == ttf_filename):
fontpath = os.path.join(walkroot, walkfilename)
if os.path.splitext(fontpath)[1] == '.ttf':
return FreeTypeFont(fontpath, size, index,
encoding, layout_engine)
return freetype(fontpath)
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 FreeTypeFont(first_font_with_a_different_extension, size,
index, encoding, layout_engine)
return freetype(first_font_with_a_different_extension)
raise

View File

@ -18,15 +18,14 @@
from . import Image
import sys
if sys.platform not in ["win32", "darwin"]:
raise ImportError("ImageGrab is macOS and Windows only")
if sys.platform == "win32":
grabber = Image.core.grabscreen
elif sys.platform == "darwin":
import os
import tempfile
import subprocess
else:
raise ImportError("ImageGrab is macOS and Windows only")
def grab(bbox=None, include_layered_windows=False):

View File

@ -130,9 +130,7 @@ def _save(im, fp, filename):
bpp = 8
version = 1
elif (im.mode == "L" and
"bpp" in im.encoderinfo and
im.encoderinfo["bpp"] in (1, 2, 4)):
elif im.mode == "L" and im.encoderinfo.get("bpp") in (1, 2, 4):
# this is 8-bit grayscale, so we shift it to get the high-order bits,
# and invert it because
@ -145,7 +143,7 @@ def _save(im, fp, filename):
rawmode = "P;" + str(bpp)
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,
# only the lower bpp bits are significant.