Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use newest Pillow methods if 9.2.0 is installed #21

Merged
merged 8 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions pilmoji/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import math

import PIL
from PIL import Image, ImageDraw, ImageFont

from typing import Dict, Optional, SupportsInt, TYPE_CHECKING, Tuple, Type, TypeVar, Union

from .helpers import NodeType, getsize, to_nodes
Expand Down Expand Up @@ -75,11 +77,11 @@ def __init__(

self.source: BaseSource = source

self._cache: bool = bool(cache)
self._cache: bool = cache
self._closed: bool = False
self._new_draw: bool = False

self._render_discord_emoji: bool = bool(render_discord_emoji)
self._render_discord_emoji: bool = render_discord_emoji
self._default_emoji_scale_factor: float = emoji_scale_factor
self._default_emoji_position_offset: Tuple[int, int] = emoji_position_offset

Expand Down Expand Up @@ -250,7 +252,7 @@ def text(
The rescaling factor for emojis. This can be used for fine adjustments.
Defaults to the factor given in the class constructor, or `1`.
emoji_position_offset: Tuple[int, int]
The emoji position offset for emojis. The can be used for fine adjustments.
The emoji position offset for emojis. This can be used for fine adjustments.
Defaults to the offset given in the class constructor, or `(0, 0)`.
"""

Expand Down Expand Up @@ -287,7 +289,11 @@ def text(

for node in line:
content = node.content
width, height = font.getsize(content)

if PIL.__version__ >= "9.2.0":
width = int(font.getlength(content))
else:
width, _ = font.getsize(content)

if node.type is NodeType.text:
self.draw.text((x, y), content, *args, **kwargs)
Expand All @@ -309,7 +315,7 @@ def text(
with Image.open(stream).convert('RGBA') as asset:
width = int(emoji_scale_factor * font.size)
size = width, math.ceil(asset.height / asset.width * width)
asset = asset.resize(size, Image.ANTIALIAS)
asset = asset.resize(size, Image.Resampling.LANCZOS)

ox, oy = emoji_position_offset
self.image.paste(asset, (x + ox, y + oy), asset)
Expand Down
6 changes: 5 additions & 1 deletion pilmoji/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from enum import Enum

from emoji import unicode_codes

import PIL
from PIL import ImageFont

from typing import Dict, Final, List, NamedTuple, TYPE_CHECKING
from typing import Dict, Final, List, NamedTuple, TYPE_CHECKING, Tuple

if TYPE_CHECKING:
from .core import FontT
Expand Down Expand Up @@ -144,6 +146,8 @@ def getsize(

if node.type is not NodeType.text:
width = int(emoji_scale_factor * font.size)
elif PIL.__version__ >= "9.2.0":
width = int(font.getlength(content))
else:
width, _ = font.getsize(content)

Expand Down