Skip to content

Commit e12acfc

Browse files
committed
Add the ability to disable Discord emojis
1 parent cdaa1e9 commit e12acfc

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

pilmoji/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
from .aio import AsyncPilmoji, AsyncRequester
66

77

8-
__version__ = '1.3.1'
8+
__version__ = '1.3.2'
99
__author__ = 'jay3332'

pilmoji/aio/core.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@ class AsyncPilmoji(BasePilmoji):
1717
"""
1818
The synchronous emoji renderer.
1919
"""
20-
def __init__(self, image: Image.Image, *, session: typing.Optional[ClientSession] = None, loop: typing.Optional[asyncio.AbstractEventLoop] = None, use_microsoft_emoji: bool = False):
20+
def __init__(
21+
self,
22+
image: Image.Image,
23+
*,
24+
session: typing.Optional[ClientSession] = None,
25+
loop: typing.Optional[asyncio.AbstractEventLoop] = None,
26+
use_microsoft_emoji: bool = False,
27+
render_discord_emoji: bool = True
28+
):
2129
if not isinstance(image, Image.Image):
2230
raise TypeError(f'Image must be of type Image, got {type(image).__name__!r} instead.')
2331

32+
self.render_discord_emoji: bool = render_discord_emoji
2433
self.http: AsyncRequester = AsyncRequester(session=session, loop=loop, _microsoft=use_microsoft_emoji)
2534
self.image: Image.Image = image
2635
self.draw = ImageDraw.Draw(image)
@@ -84,7 +93,10 @@ async def text(self,
8493
if node['type'] == 'twemoji':
8594
stream = await self.http.get_twemoji(content)
8695
else:
87-
stream = await self.http.get_discord_emoji(content)
96+
stream = (
97+
await self.http.get_discord_emoji(content)
98+
if self.render_discord_emoji else None
99+
)
88100

89101
if not stream:
90102
self.draw.text((x, y), content, *args, **kwargs)

pilmoji/sync/core.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ class Pilmoji(BasePilmoji):
1616
"""
1717
The synchronous emoji renderer.
1818
"""
19-
def __init__(self, image: Image.Image, *, session: typing.Optional[Session] = None, use_microsoft_emoji: bool = False):
19+
def __init__(
20+
self,
21+
image: Image.Image,
22+
*,
23+
session: typing.Optional[Session] = None,
24+
use_microsoft_emoji: bool = False,
25+
render_discord_emoji: bool = True
26+
):
2027
if not isinstance(image, Image.Image):
2128
raise TypeError(f'Image must be of type Image, got {type(image).__name__!r} instead.')
2229

30+
self.render_discord_emoji: bool = render_discord_emoji
2331
self.http: Requester = Requester(session=session, _microsoft=use_microsoft_emoji)
2432
self.image: Image.Image = image
2533
self.draw = ImageDraw.Draw(image)
@@ -83,7 +91,10 @@ def text(self,
8391
if node['type'] == 'twemoji':
8492
stream = self.http.get_twemoji(content)
8593
else:
86-
stream = self.http.get_discord_emoji(content)
94+
stream = (
95+
self.http.get_discord_emoji(content)
96+
if self.render_discord_emoji else None
97+
)
8798

8899
if not stream:
89100
self.draw.text((x, y), content, *args, **kwargs)

tests/tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from pilmoji import Pilmoji
2+
from PIL import Image, ImageFont
3+
4+
5+
my_string = '😎😎😎😎😎😎'
6+
7+
with Image.new('RGB', (550, 80), (255, 255, 255)) as image:
8+
font = ImageFont.truetype('arial.ttf', 24)
9+
10+
with Pilmoji(image, use_microsoft_emoji=True) as pilmoji:
11+
w, h = Pilmoji.getsize(my_string, font)
12+
pilmoji.text((10, 10), my_string, (0, 0, 0), font)
13+
pilmoji.text((10+w, 10), 'hi', (0, 0, 0), font)
14+
15+
image.show()

0 commit comments

Comments
 (0)