Spaces:
Sleeping
Sleeping
File size: 1,303 Bytes
346533a 1d20b52 346533a 5938649 346533a 5938649 346533a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
from PIL import Image, ImageDraw
from pil_utils import BuildImage
from pil_utils.fonts import Font
from meme_generator import add_meme
from meme_generator.utils import make_jpg_or_gif
def charpic(images: list[BuildImage], texts, args):
img = images[0]
str_map = "@@$$&B88QMMGW##EE93SPPDOOU**==()+^,\"--''. "
num = len(str_map)
font = Font.find("Consolas").load_font(15)
def make(img: BuildImage) -> BuildImage:
img = img.convert("RGBA").resize_width(150).convert("L")
img = img.resize((img.width, img.height // 2))
lines = []
for y in range(img.height):
line = ""
for x in range(img.width):
gray = img.image.getpixel((x, y))
line += str_map[int(num * gray / 256)] if gray != 0 else " "
lines.append(line)
text = "\n".join(lines)
text_img = Image.new("RGB", (2000, 2000), "white")
draw = ImageDraw.Draw(text_img)
_, _, w, h = draw.multiline_textbbox((0, 0), text, font=font)
draw.multiline_text((0, 0), text, font=font, fill="black")
text_img = text_img.crop((0, 0, w, h))
return BuildImage(text_img)
return make_jpg_or_gif(img, make)
add_meme("charpic", charpic, min_images=1, max_images=1, keywords=["字符画"])
|