Spaces:
Runtime error
Runtime error
Automatically crop card png output
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ from io import BytesIO
|
|
7 |
import gradio as gr
|
8 |
import numpy as np
|
9 |
import torch
|
10 |
-
from PIL import Image
|
11 |
from fastai.callback.core import Callback
|
12 |
from fastai.learner import *
|
13 |
from fastai.torch_core import TitledStr
|
@@ -238,6 +238,8 @@ def format_monster_card(monster_text, image_data):
|
|
238 |
else:
|
239 |
card = card.replace('{actions}', f'<div class="monster-action"><p>{actions}</p></div>')
|
240 |
|
|
|
|
|
241 |
card = card.replace('Melee Weapon Attack:', '<i>Melee Weapon Attack:</i>')
|
242 |
card = card.replace('Ranged Weapon Attack:', '<i>Ranged Weapon Attack:</i>')
|
243 |
card = card.replace('Melee or Ranged Weapon Attack:', '<i>Melee or Ranged Weapon Attack:</i>')
|
@@ -259,12 +261,29 @@ def pil_to_base64(image):
|
|
259 |
hti = Html2Image(output_path='rendered_cards')
|
260 |
|
261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
def html_to_png(html):
|
263 |
print('CONVERTING HTML CARD TO PNG IMAGE')
|
264 |
-
paths = hti.screenshot(html_str=html, css_file="monstermaker.css", save_as="test.png", size=(400,
|
265 |
path = paths[0]
|
266 |
print('OPENING IMAGE FROM FILE')
|
267 |
img = Image.open(path).convert("RGB")
|
|
|
|
|
268 |
print('CONVERTING HTML CARD TO PNG IMAGE COMPLETE')
|
269 |
return img
|
270 |
|
|
|
7 |
import gradio as gr
|
8 |
import numpy as np
|
9 |
import torch
|
10 |
+
from PIL import Image, ImageChops, ImageDraw
|
11 |
from fastai.callback.core import Callback
|
12 |
from fastai.learner import *
|
13 |
from fastai.torch_core import TitledStr
|
|
|
238 |
else:
|
239 |
card = card.replace('{actions}', f'<div class="monster-action"><p>{actions}</p></div>')
|
240 |
|
241 |
+
# TODO: Legendary actions, reactions
|
242 |
+
|
243 |
card = card.replace('Melee Weapon Attack:', '<i>Melee Weapon Attack:</i>')
|
244 |
card = card.replace('Ranged Weapon Attack:', '<i>Ranged Weapon Attack:</i>')
|
245 |
card = card.replace('Melee or Ranged Weapon Attack:', '<i>Melee or Ranged Weapon Attack:</i>')
|
|
|
261 |
hti = Html2Image(output_path='rendered_cards')
|
262 |
|
263 |
|
264 |
+
def trim(im, border):
|
265 |
+
bg = Image.new(im.mode, im.size, border)
|
266 |
+
diff = ImageChops.difference(im, bg)
|
267 |
+
bbox = diff.getbbox()
|
268 |
+
if bbox:
|
269 |
+
return im.crop(bbox)
|
270 |
+
|
271 |
+
|
272 |
+
def crop_background(image):
|
273 |
+
white = (255, 255, 255)
|
274 |
+
ImageDraw.floodfill(image, (image.size[0] - 1, 0), white, thresh=50)
|
275 |
+
image = trim(image, white)
|
276 |
+
return image
|
277 |
+
|
278 |
+
|
279 |
def html_to_png(html):
|
280 |
print('CONVERTING HTML CARD TO PNG IMAGE')
|
281 |
+
paths = hti.screenshot(html_str=html, css_file="monstermaker.css", save_as="test.png", size=(400, 1440))
|
282 |
path = paths[0]
|
283 |
print('OPENING IMAGE FROM FILE')
|
284 |
img = Image.open(path).convert("RGB")
|
285 |
+
print('CROPPING BACKGROUND')
|
286 |
+
img = crop_background(img)
|
287 |
print('CONVERTING HTML CARD TO PNG IMAGE COMPLETE')
|
288 |
return img
|
289 |
|