Skip to content

Commit

Permalink
replace deprecated PIL functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetWq committed Aug 28, 2023
1 parent 478b43f commit 128325c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
4 changes: 2 additions & 2 deletions meme_generator/memes/bubble_tea/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
from typing import List, Literal

from PIL import Image
from PIL.Image import Transpose
from pil_utils import BuildImage
from pydantic import Field

Expand Down Expand Up @@ -43,7 +43,7 @@ def bubble_tea(images: List[BuildImage], texts, args: Model):
if right:
frame.paste(bubble_tea, alpha=True)
if left:
frame.paste(bubble_tea.transpose(Image.FLIP_LEFT_RIGHT), alpha=True)
frame.paste(bubble_tea.transpose(Transpose.FLIP_LEFT_RIGHT), alpha=True)
return frame.save_jpg()


Expand Down
5 changes: 3 additions & 2 deletions meme_generator/memes/charpic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ def make(img: BuildImage) -> BuildImage:
line += str_map[int(num * gray / 256)]
lines.append(line)
text = "\n".join(lines)
w, h = font.getsize_multiline(text)
text_img = Image.new("RGB", (w, h), "white")
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)
Expand Down
21 changes: 13 additions & 8 deletions meme_generator/memes/guichu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from collections import namedtuple
from typing import Dict, List, Literal
from typing import Dict, List, Literal, NamedTuple, Tuple

from PIL import Image
from PIL.Image import Image as IMG
from PIL.Image import Transpose
from pil_utils import BuildImage
from pydantic import Field

Expand Down Expand Up @@ -41,31 +40,37 @@ def guichu(images: List[BuildImage], texts, args: Model):
img = images[0].convert("RGBA")
img_w, img_h = img.size

Mode = namedtuple("Mode", ["method", "size1", "pos1", "size2", "pos2"])
class Mode(NamedTuple):
method: Transpose
size1: Tuple[int, int, int, int]
pos1: Tuple[int, int]
size2: Tuple[int, int, int, int]
pos2: Tuple[int, int]

modes: Dict[str, Mode] = {
"left": Mode(
Image.FLIP_LEFT_RIGHT,
Transpose.FLIP_LEFT_RIGHT,
(0, 0, img_w // 2, img_h),
(0, 0),
(img_w // 2, 0, img_w // 2 * 2, img_h),
(img_w // 2, 0),
),
"right": Mode(
Image.FLIP_LEFT_RIGHT,
Transpose.FLIP_LEFT_RIGHT,
(img_w // 2, 0, img_w // 2 * 2, img_h),
(img_w // 2, 0),
(0, 0, img_w // 2, img_h),
(0, 0),
),
"top": Mode(
Image.FLIP_TOP_BOTTOM,
Transpose.FLIP_TOP_BOTTOM,
(0, 0, img_w, img_h // 2),
(0, 0),
(0, img_h // 2, img_w, img_h // 2 * 2),
(0, img_h // 2),
),
"bottom": Mode(
Image.FLIP_TOP_BOTTOM,
Transpose.FLIP_TOP_BOTTOM,
(0, img_h // 2, img_w, img_h // 2 * 2),
(0, img_h // 2),
(0, 0, img_w, img_h // 2),
Expand Down
4 changes: 2 additions & 2 deletions meme_generator/memes/gun/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
from typing import List, Literal

from PIL import Image
from PIL.Image import Transpose
from pil_utils import BuildImage
from pydantic import Field

Expand Down Expand Up @@ -43,7 +43,7 @@ def gun(images: List[BuildImage], texts, args: Model):
if left:
frame.paste(gun, alpha=True)
if right:
frame.paste(gun.transpose(Image.FLIP_LEFT_RIGHT), alpha=True)
frame.paste(gun.transpose(Transpose.FLIP_LEFT_RIGHT), alpha=True)
return frame.save_jpg()


Expand Down
4 changes: 2 additions & 2 deletions meme_generator/memes/keep_away/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from PIL import Image
from PIL.Image import Transpose
from pil_utils import BuildImage

from meme_generator import add_meme
Expand All @@ -12,7 +12,7 @@ def trans(img: BuildImage, n: int) -> BuildImage:
if n < 4:
return img.rotate(n * 90)
else:
return img.transpose(Image.FLIP_LEFT_RIGHT).rotate((n - 4) * 90)
return img.transpose(Transpose.FLIP_LEFT_RIGHT).rotate((n - 4) * 90)

def paste(img: BuildImage):
nonlocal count
Expand Down
24 changes: 14 additions & 10 deletions meme_generator/memes/symmetric/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from collections import namedtuple
from typing import Dict, List, Literal
from typing import Dict, List, Literal, NamedTuple, Tuple

from PIL import Image
from PIL.Image import Transpose
from pil_utils import BuildImage
from pydantic import Field

Expand Down Expand Up @@ -40,36 +39,41 @@ def symmetric(images: List[BuildImage], texts, args: Model):
img = images[0]
img_w, img_h = img.size

Mode = namedtuple(
"Mode", ["method", "frame_size", "size1", "pos1", "size2", "pos2"]
)
class Mode(NamedTuple):
method: Transpose
frame_size: Tuple[int, int]
size1: Tuple[int, int, int, int]
pos1: Tuple[int, int]
size2: Tuple[int, int, int, int]
pos2: Tuple[int, int]

modes: Dict[str, Mode] = {
"left": Mode(
Image.FLIP_LEFT_RIGHT,
Transpose.FLIP_LEFT_RIGHT,
(img_w // 2 * 2, img_h),
(0, 0, img_w // 2, img_h),
(0, 0),
(img_w // 2, 0, img_w // 2 * 2, img_h),
(img_w // 2, 0),
),
"right": Mode(
Image.FLIP_LEFT_RIGHT,
Transpose.FLIP_LEFT_RIGHT,
(img_w // 2 * 2, img_h),
(img_w // 2, 0, img_w // 2 * 2, img_h),
(img_w // 2, 0),
(0, 0, img_w // 2, img_h),
(0, 0),
),
"top": Mode(
Image.FLIP_TOP_BOTTOM,
Transpose.FLIP_TOP_BOTTOM,
(img_w, img_h // 2 * 2),
(0, 0, img_w, img_h // 2),
(0, 0),
(0, img_h // 2, img_w, img_h // 2 * 2),
(0, img_h // 2),
),
"bottom": Mode(
Image.FLIP_TOP_BOTTOM,
Transpose.FLIP_TOP_BOTTOM,
(img_w, img_h // 2 * 2),
(0, img_h // 2, img_w, img_h // 2 * 2),
(0, img_h // 2),
Expand Down
12 changes: 8 additions & 4 deletions meme_generator/memes/youtube/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
from typing import List

from PIL import Image
from PIL.Image import Transpose
from pil_utils import BuildImage, Text2Image

from meme_generator import add_meme
Expand Down Expand Up @@ -40,9 +40,13 @@ def youtube(images, texts: List[str], args):
x1 = frame.width - corner.width - 50
y1 = frame.height - corner.height - 50
frame.paste(corner, (x0, y0 - 1), alpha=True).paste(
corner.transpose(Image.FLIP_TOP_BOTTOM), (x0, y1 + 1), alpha=True
).paste(corner.transpose(Image.FLIP_LEFT_RIGHT), (x1, y0 - 1), alpha=True).paste(
corner.transpose(Image.FLIP_TOP_BOTTOM).transpose(Image.FLIP_LEFT_RIGHT),
corner.transpose(Transpose.FLIP_TOP_BOTTOM), (x0, y1 + 1), alpha=True
).paste(
corner.transpose(Transpose.FLIP_LEFT_RIGHT), (x1, y0 - 1), alpha=True
).paste(
corner.transpose(Transpose.FLIP_TOP_BOTTOM).transpose(
Transpose.FLIP_LEFT_RIGHT
),
(x1, y1 + 1),
alpha=True,
).paste(
Expand Down

0 comments on commit 128325c

Please sign in to comment.