Skip to content

Instantly share code, notes, and snippets.

@shacharmirkin
Last active October 6, 2024 16:38
Show Gist options
  • Select an option

  • Save shacharmirkin/f5abdc58867a2950111cf5a811f33b7e to your computer and use it in GitHub Desktop.

Select an option

Save shacharmirkin/f5abdc58867a2950111cf5a811f33b7e to your computer and use it in GitHub Desktop.
"""
Some memes, made up of multiple boxes with a predefined reading order,
can be confusing or feel unnatural when used with right-to-left (RTL) languages.
This script takes a vertically symmetric meme and adjusts it for RTL reading.
Optionally, you can also mirror the images if it makes more sense after swapping the two vertical halves.
"""
from PIL import Image
def rtl_meme(input_path, output_path, mirror=False):
img = Image.open(input_path)
width, height = img.size
mid = width // 2
left_half = img.crop((0, 0, mid, height))
right_half = img.crop((mid, 0, width, height))
if mirror:
left_half = left_half.transpose(Image.FLIP_LEFT_RIGHT)
right_half = right_half.transpose(Image.FLIP_LEFT_RIGHT)
new_img = Image.new("RGB", (width, height))
new_img.paste(right_half, (0, 0))
new_img.paste(left_half, (mid, 0))
new_img.save(output_path)
# Run example
if __name__ == "__main__":
input_image = "input/sponge_bob.jpg"
output_image = "output/RTL_sponge_bob.jpg"
rtl_meme(input_image, output_image, mirror=False)
@shacharmirkin
Copy link
Author

Example (showing input & output side by side)
example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment