Merge pull request #3807 from radarhere/code

Simplified code
This commit is contained in:
Hugo 2019-05-04 18:57:40 +03:00 committed by GitHub
commit 45df9ad882
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 34 deletions

View File

@ -46,6 +46,13 @@ class TestFilePalm(PillowTestCase):
self.skipKnownBadTest("Palm P image is wrong")
self.roundtrip(mode)
def test_l_ioerror(self):
# Arrange
mode = "L"
# Act / Assert
self.assertRaises(IOError, self.helper_save_as_palm, mode)
def test_rgb_ioerror(self):
# Arrange
mode = "RGB"

View File

@ -141,6 +141,18 @@ class TestImageFont(PillowTestCase):
ImageFont.truetype(tempfile, FONT_SIZE)
def test_unavailable_layout_engine(self):
have_raqm = ImageFont.core.HAVE_RAQM
ImageFont.core.HAVE_RAQM = False
try:
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE,
layout_engine=ImageFont.LAYOUT_RAQM)
finally:
ImageFont.core.HAVE_RAQM = have_raqm
self.assertEqual(ttf.layout_engine, ImageFont.LAYOUT_BASIC)
def _render(self, font):
txt = "Hello World!"
ttf = ImageFont.truetype(font, FONT_SIZE,
@ -410,6 +422,7 @@ class TestImageFont(PillowTestCase):
# Act/Assert
self.assertRaises(IOError, ImageFont.load_path, filename)
self.assertRaises(IOError, ImageFont.truetype, filename)
def test_default_font(self):
# Arrange

View File

@ -135,7 +135,7 @@ class FreeTypeFont(object):
layout_engine = LAYOUT_BASIC
if core.HAVE_RAQM:
layout_engine = LAYOUT_RAQM
if layout_engine == LAYOUT_RAQM and not core.HAVE_RAQM:
elif layout_engine == LAYOUT_RAQM and not core.HAVE_RAQM:
layout_engine = LAYOUT_BASIC
self.layout_engine = layout_engine
@ -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,28 +130,23 @@ 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":
if 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
# 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))
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))
else:
raise IOError("cannot write mode %s as Palm" % im.mode)
# this is 8-bit grayscale, so we shift it to get the high-order bits,
# and invert it because
# 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))
# we ignore the palette here
im.mode = "P"
rawmode = "P;" + str(bpp)
version = 1
elif im.mode == "L" and "bpp" in im.info and im.info["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))
# we ignore the palette here
im.mode = "P"
rawmode = "P;" + str(bpp)