Compare commits
1
Commits
main
...
feat/img-fit
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0fb1c77907 |
@@ -1 +1,3 @@
|
||||
drawsvg==2.4.0
|
||||
pillow==10.4.0
|
||||
--extra-index-url=https://packagecloud.io/github/git-lfs/pypi/simple
|
||||
@@ -8,6 +8,7 @@ from draw_functions import create_gold_icon, create_effects, draw_champion_shiel
|
||||
from card_types import Card, SkinData, SkinFile, Translation
|
||||
|
||||
from drawsvg.drawing import Drawing
|
||||
from PIL import Image
|
||||
|
||||
from edit_card import finish_card
|
||||
|
||||
@@ -212,16 +213,45 @@ for card_data in cards:
|
||||
)
|
||||
|
||||
# Draw image
|
||||
d.append(dw.Rectangle(x=1, y=65, width=x_width - 2, height=200))
|
||||
d.append(
|
||||
dw.Image(
|
||||
x=border_width_x,
|
||||
y=65,
|
||||
width=x_width - (border_width_x * 2),
|
||||
height=200,
|
||||
path=card_skin_data["image_path"],
|
||||
)
|
||||
image_area_width = x_width - (2 * border_width_x)
|
||||
image_area_height = 200
|
||||
d.append(dw.Rectangle(x=1, y=65, width=x_width - 2, height=image_area_height))
|
||||
image = Image.open(card_skin_data['image_path'].replace('../../', ''))
|
||||
image_natural_width, image_natural_height = image.size
|
||||
image_ratio = image_natural_width / image_natural_height
|
||||
area_ratio = image_area_width / image_area_height
|
||||
print(image_natural_width)
|
||||
print(image_area_width)
|
||||
print(image_ratio)
|
||||
print(area_ratio)
|
||||
if image_ratio > area_ratio:
|
||||
# Wider image
|
||||
scale = image_area_height / image_natural_height
|
||||
new_width = image_natural_width * scale
|
||||
translate_x = (image_area_width - new_width) / 2
|
||||
transform = f"translate({translate_x}, 0) scale ({scale})"
|
||||
print(transform)
|
||||
else:
|
||||
# Taller image
|
||||
scale = image_area_width / image_natural_width
|
||||
new_height = image_natural_height * scale
|
||||
translate_y = (image_area_height - new_height) / 2
|
||||
transform = f"translate(0, {translate_y}) scale({scale})"
|
||||
|
||||
card_image = dw.Image(
|
||||
x=border_width_x,
|
||||
y=65,
|
||||
width=x_width - (border_width_x * 2),
|
||||
height=200,
|
||||
path=card_skin_data["image_path"],
|
||||
**{
|
||||
"object-fit": "cover",
|
||||
}
|
||||
)
|
||||
|
||||
card_image.args['transform'] = transform
|
||||
|
||||
d.append(card_image)
|
||||
d.append(dw.Rectangle(x=2, y=65 - 1, width=border_width_x - 2, height=200 + 2))
|
||||
# d.append(dw.Rectangle(x=0, y=65+1, width=1, height=200+2, fill='white'))
|
||||
d.append(
|
||||
|
||||
Reference in New Issue
Block a user