Created
May 1, 2024 16:15
-
-
Save blopa/cd83c38cf2dc61476ee8d635c27c6a75 to your computer and use it in GitHub Desktop.
Code for post "Transforming manga pages for lazy western readers"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_image_with_inverted_text_placement(base_image, data): | |
width, height = base_image.size | |
new_image = Image.new('RGBA', (width, height), (255, 255, 255, 0)) | |
data = json.loads(data) | |
for index, item in enumerate(data): | |
x, y = item['location'] | |
box_width, box_height = item['width'], item['height'] | |
if box_width <= 0 or box_height <= 0: | |
continue | |
new_x = width - x - box_width | |
cropped_image = base_image.crop((x, y, x + box_width, y + box_height)) | |
cropped_image = cropped_image.convert('RGBA') | |
new_image.paste(cropped_image, (new_x, y), cropped_image) | |
return new_image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment