mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-27 10:26:19 +03:00
commit
45df9ad882
|
@ -46,6 +46,13 @@ class TestFilePalm(PillowTestCase):
|
||||||
self.skipKnownBadTest("Palm P image is wrong")
|
self.skipKnownBadTest("Palm P image is wrong")
|
||||||
self.roundtrip(mode)
|
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):
|
def test_rgb_ioerror(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
mode = "RGB"
|
mode = "RGB"
|
||||||
|
|
|
@ -141,6 +141,18 @@ class TestImageFont(PillowTestCase):
|
||||||
|
|
||||||
ImageFont.truetype(tempfile, FONT_SIZE)
|
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):
|
def _render(self, font):
|
||||||
txt = "Hello World!"
|
txt = "Hello World!"
|
||||||
ttf = ImageFont.truetype(font, FONT_SIZE,
|
ttf = ImageFont.truetype(font, FONT_SIZE,
|
||||||
|
@ -410,6 +422,7 @@ class TestImageFont(PillowTestCase):
|
||||||
|
|
||||||
# Act/Assert
|
# Act/Assert
|
||||||
self.assertRaises(IOError, ImageFont.load_path, filename)
|
self.assertRaises(IOError, ImageFont.load_path, filename)
|
||||||
|
self.assertRaises(IOError, ImageFont.truetype, filename)
|
||||||
|
|
||||||
def test_default_font(self):
|
def test_default_font(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
|
|
|
@ -135,7 +135,7 @@ class FreeTypeFont(object):
|
||||||
layout_engine = LAYOUT_BASIC
|
layout_engine = LAYOUT_BASIC
|
||||||
if core.HAVE_RAQM:
|
if core.HAVE_RAQM:
|
||||||
layout_engine = LAYOUT_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
|
layout_engine = LAYOUT_BASIC
|
||||||
|
|
||||||
self.layout_engine = layout_engine
|
self.layout_engine = layout_engine
|
||||||
|
@ -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,28 +130,23 @@ def _save(im, fp, filename):
|
||||||
bpp = 8
|
bpp = 8
|
||||||
version = 1
|
version = 1
|
||||||
|
|
||||||
elif (im.mode == "L" and
|
elif im.mode == "L":
|
||||||
"bpp" in im.encoderinfo and
|
if im.encoderinfo.get("bpp") in (1, 2, 4):
|
||||||
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
|
||||||
# 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))
|
||||||
# we ignore the palette here
|
elif im.info.get("bpp") in (1, 2, 4):
|
||||||
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,
|
# 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:
|
||||||
|
raise IOError("cannot write mode %s as Palm" % im.mode)
|
||||||
|
|
||||||
# we ignore the palette here
|
# we ignore the palette here
|
||||||
im.mode = "P"
|
im.mode = "P"
|
||||||
rawmode = "P;" + str(bpp)
|
rawmode = "P;" + str(bpp)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user