Merge pull request #5826 from hugovk/pickle-font

Add support for pickling TrueType fonts
This commit is contained in:
Andrew Murray 2021-11-12 23:03:20 +11:00 committed by GitHub
commit 937be347df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 5 deletions

View File

@ -2,9 +2,12 @@ import pickle
import pytest
from PIL import Image
from PIL import Image, ImageDraw, ImageFont
from .helper import skip_unless_feature
from .helper import assert_image_equal, skip_unless_feature
FONT_SIZE = 20
FONT_PATH = "Tests/fonts/DejaVuSans/DejaVuSans.ttf"
def helper_pickle_file(tmp_path, pickle, protocol, test_file, mode):
@ -92,3 +95,48 @@ def test_pickle_tell():
# Assert
assert unpickled_image.tell() == 0
def helper_assert_pickled_font_images(font1, font2):
# Arrange
im1 = Image.new(mode="RGBA", size=(300, 100))
im2 = Image.new(mode="RGBA", size=(300, 100))
draw1 = ImageDraw.Draw(im1)
draw2 = ImageDraw.Draw(im2)
txt = "Hello World!"
# Act
draw1.text((10, 10), txt, font=font1)
draw2.text((10, 10), txt, font=font2)
# Assert
assert_image_equal(im1, im2)
@pytest.mark.parametrize("protocol", list(range(0, pickle.HIGHEST_PROTOCOL + 1)))
def test_pickle_font_string(protocol):
# Arrange
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
# Act: roundtrip
pickled_font = pickle.dumps(font, protocol)
unpickled_font = pickle.loads(pickled_font)
# Assert
helper_assert_pickled_font_images(font, unpickled_font)
@pytest.mark.parametrize("protocol", list(range(0, pickle.HIGHEST_PROTOCOL + 1)))
def test_pickle_font_file(tmp_path, protocol):
# Arrange
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
filename = str(tmp_path / "temp.pkl")
# Act: roundtrip
with open(filename, "wb") as f:
pickle.dump(font, f, protocol)
with open(filename, "rb") as f:
unpickled_font = pickle.load(f)
# Assert
helper_assert_pickled_font_images(font, unpickled_font)

View File

@ -83,7 +83,18 @@ TODO
Other Changes
=============
TODO
^^^^
Added support for pickling TrueType fonts
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TODO
TrueType fonts may now be pickled and unpickled. For example:
.. code-block:: python
import pickle
from PIL import ImageFont
font = ImageFont.truetype("arial.ttf", size=30)
pickled_font = pickle.dumps(font, protocol=pickle.HIGHEST_PROTOCOL)
# Later...
unpickled_font = pickle.loads(pickled_font)

View File

@ -196,6 +196,13 @@ class FreeTypeFont:
else:
load_from_bytes(font)
def __getstate__(self):
return [self.path, self.size, self.index, self.encoding, self.layout_engine]
def __setstate__(self, state):
path, size, index, encoding, layout_engine = state
self.__init__(path, size, index, encoding, layout_engine)
def _multiline_split(self, text):
split_character = "\n" if isinstance(text, str) else b"\n"
return text.split(split_character)