Skip to main content

Pilmoji for nonebot-plugin-parser

Project description

Pilmoji for Parser

A simplified emoji renderer for Pillow, Python's imaging library. This is a streamlined fork optimized for use with nonebot-plugin-parser.

โœจ Features

  • ๐ŸŽจ Unicode Emoji Support - Render standard Unicode emojis
  • ๐Ÿ’ฌ Discord Emoji Support - Render custom Discord emojis
  • ๐Ÿ“ Multi-line Text - Automatic line break handling
  • ๐ŸŽญ Multiple Emoji Styles - Apple, Google, Twitter, Facebook
  • โšก Async/Await - Built with modern async Python
  • ๐Ÿ’พ Smart Caching - Local file caching for better performance
  • ๐Ÿ”ง Simple API - Easy-to-use interface with sensible defaults

๐Ÿ“ฆ Installation

Requirements: Python 3.10 or higher

pip install pilmoji-for-parser

Or install from source:

git clone https://github.com/fllesser/pilmoji-for-parser.git
cd pilmoji-for-parser
pip install .

๐Ÿš€ Quick Start

Basic Usage (Unicode Emoji Only)

import asyncio
from pilmoji import Pilmoji
from PIL import Image, ImageFont

async def main():
    text = '''
    Hello, world! ๐Ÿ‘‹ 
    Here are some emojis: ๐ŸŽจ ๐ŸŒŠ ๐Ÿ˜Ž
    Multi-line support! ๐Ÿš€ โœจ
    '''
    
    # Create image
    image = Image.new('RGB', (550, 150), (255, 255, 255))
    font = ImageFont.truetype('arial.ttf', 24)
    
    # Render text with emojis
    async with Pilmoji() as pilmoji:
        await pilmoji.text(image, (10, 10), text.strip(), font, fill=(0, 0, 0))
    
    image.save('output.png')
    image.show()

asyncio.run(main())

With Discord Emoji Support

async def main():
    text = '''
    Unicode emoji: ๐Ÿ‘‹ ๐ŸŽจ ๐Ÿ˜Ž
    Discord emoji: <:custom:123456789012345678>
    '''
    
    image = Image.new('RGB', (550, 100), (255, 255, 255))
    font = ImageFont.truetype('arial.ttf', 24)
    
    async with Pilmoji() as pilmoji:
        await pilmoji.text_with_discord_emoji(
            image, (10, 10), text.strip(), font, fill=(0, 0, 0)
        )
    
    image.save('output.png')

asyncio.run(main())

๐ŸŽจ Emoji Styles

Choose from different emoji styles:

from pilmoji import Pilmoji, EmojiStyle, EmojiCDNSource

# Apple style (default)
source = EmojiCDNSource(style=EmojiStyle.APPLE)

# Google style
source = EmojiCDNSource(style=EmojiStyle.GOOGLE)

# Twitter style
source = EmojiCDNSource(style=EmojiStyle.TWITTER)

# Facebook style
source = EmojiCDNSource(style=EmojiStyle.FACEBOOK)

async with Pilmoji(source=source) as pilmoji:
    await pilmoji.text(image, (10, 10), "Hello ๐Ÿ‘‹", font)

๐Ÿ”ง API Reference

Pilmoji

Main class for rendering text with emojis.

Constructor:

Pilmoji(
    source: BaseSource = EmojiCDNSource(),
    cache: bool = True
)

Parameters:

  • source: Emoji source to use (default: EmojiCDNSource())
  • cache: Enable emoji caching (default: True)

Methods:

async text(image, xy, text, font, fill=None)

Render text with Unicode emoji support.

  • image: PIL Image object to render onto
  • xy: Tuple of (x, y) coordinates for text position
  • text: Text string to render (supports multiple lines)
  • font: PIL Font object
  • fill: Text color (default: black)

async text_with_discord_emoji(image, xy, text, font, fill=None)

Render text with both Unicode and Discord emoji support.

Parameters are the same as text().

EmojiCDNSource

Default emoji source using emojicdn.elk.sh.

Constructor:

EmojiCDNSource(
    style: EmojiStyle = EmojiStyle.APPLE,
    cache_dir: Path | None = None
)

Parameters:

  • style: Emoji style to use (Apple, Google, Twitter, Facebook)
  • cache_dir: Custom cache directory (default: ~/.cache/pilmoji)

๐Ÿ”Œ Custom Emoji Sources

Create your own emoji source by subclassing BaseSource:

from pilmoji import BaseSource
from io import BytesIO

class CustomEmojiSource(BaseSource):
    async def get_emoji(self, emoji: str) -> BytesIO | None:
        # Your custom emoji fetching logic
        pass
    
    async def get_discord_emoji(self, id: int) -> BytesIO | None:
        # Your custom Discord emoji fetching logic
        pass

# Use your custom source
async with Pilmoji(source=CustomEmojiSource()) as pilmoji:
    await pilmoji.text(image, (10, 10), "Hello ๐Ÿ‘‹", font)

๐Ÿ“ Examples

Different Text Colors

# Red text
await pilmoji.text(image, (10, 10), "Red text ๐Ÿ”ด", font, fill=(255, 0, 0))

# RGB tuple
await pilmoji.text(image, (10, 50), "Blue text ๐Ÿ”ต", font, fill=(0, 0, 255))

# RGBA tuple with transparency
await pilmoji.text(image, (10, 90), "Semi-transparent ๐Ÿ‘ป", font, fill=(0, 0, 0, 128))

Multi-line Text

text = """Line 1 with emoji ๐ŸŽจ
Line 2 with emoji ๐ŸŒŠ
Line 3 with emoji ๐Ÿ˜Ž"""

await pilmoji.text(image, (10, 10), text, font, fill=(0, 0, 0))

Without Context Manager

pilmoji = Pilmoji()
await pilmoji.text(image, (10, 10), "Hello ๐Ÿ‘‹", font)
await pilmoji.aclose()  # Don't forget to close!

๐Ÿงช Development

Setup

# Clone the repository
git clone https://github.com/fllesser/pilmoji-for-parser.git
cd pilmoji-for-parser

# Install dependencies
uv sync --dev

# Run tests
uv run pytest

# Run linting
uv run ruff check src/

Running Tests

# Run all tests
uv run poe test

# Run with coverage
uv run pytest --cov=src --cov-report=html

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

๐Ÿ”— Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pilmoji_for_parser-0.1.6.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pilmoji_for_parser-0.1.6-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file pilmoji_for_parser-0.1.6.tar.gz.

File metadata

  • Download URL: pilmoji_for_parser-0.1.6.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pilmoji_for_parser-0.1.6.tar.gz
Algorithm Hash digest
SHA256 f4ae88d2b7f2af7b76df4ce9eab8a2248e9a99e7a5683b7e40f805c90ed3787a
MD5 c465494253a5f8809479e20c248e022a
BLAKE2b-256 ae0465c29fbdf7a6b371ba89d9d6b53579d1135253ef55905ccb4602851576b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pilmoji_for_parser-0.1.6.tar.gz:

Publisher: release.yml on fllesser/pilmoji-for-parser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pilmoji_for_parser-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for pilmoji_for_parser-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 044a4b890b3ef81a0cdadc4b6d181c52cc6b2192f4c4250e28181bcb0abe3c94
MD5 e6e16a8f19248d216d7b5e6995949d3c
BLAKE2b-256 39e3a09b0ac431224a3678e4a65c2b62c1d97f8711271e0163df713599abdd23

See more details on using hashes here.

Provenance

The following attestation bundles were made for pilmoji_for_parser-0.1.6-py3-none-any.whl:

Publisher: release.yml on fllesser/pilmoji-for-parser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page