Spaces:
Running
Running
Update routers/cover.py
Browse files- routers/cover.py +35 -2
routers/cover.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from fastapi import APIRouter, Query, HTTPException
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
-
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
import requests
|
| 6 |
|
|
@@ -52,12 +52,42 @@ def resize_and_crop_to_fill(img: Image.Image, target_width: int, target_height:
|
|
| 52 |
|
| 53 |
return img_resized.crop((left, top, right, bottom))
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def create_cover_image(image_url: str) -> BytesIO:
|
| 56 |
"""
|
| 57 |
Cria uma capa para reels do Instagram.
|
| 58 |
- Fundo preto
|
| 59 |
- Largura: 1080, Altura: 1920
|
| 60 |
- Imagem renderizada em Largura: 1080, Altura: 1440, X: 0, Y: 240
|
|
|
|
| 61 |
"""
|
| 62 |
# Dimensões do canvas
|
| 63 |
canvas_width = 1080
|
|
@@ -75,8 +105,11 @@ def create_cover_image(image_url: str) -> BytesIO:
|
|
| 75 |
img_height = 1440
|
| 76 |
filled_img = resize_and_crop_to_fill(img, img_width, img_height)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
| 78 |
# Colar imagem na posição X: 0, Y: 240
|
| 79 |
-
canvas.paste(
|
| 80 |
|
| 81 |
# Converter para bytes
|
| 82 |
buffer = BytesIO()
|
|
|
|
| 1 |
from fastapi import APIRouter, Query, HTTPException
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
from io import BytesIO
|
| 5 |
import requests
|
| 6 |
|
|
|
|
| 52 |
|
| 53 |
return img_resized.crop((left, top, right, bottom))
|
| 54 |
|
| 55 |
+
def add_rounded_corners(img: Image.Image, radius: int) -> Image.Image:
|
| 56 |
+
"""
|
| 57 |
+
Adiciona cantos arredondados à imagem.
|
| 58 |
+
|
| 59 |
+
Args:
|
| 60 |
+
img: Imagem PIL
|
| 61 |
+
radius: Raio do arredondamento em pixels
|
| 62 |
+
|
| 63 |
+
Returns:
|
| 64 |
+
Imagem com cantos arredondados
|
| 65 |
+
"""
|
| 66 |
+
# Criar máscara para os cantos arredondados
|
| 67 |
+
mask = Image.new("L", img.size, 0)
|
| 68 |
+
draw = ImageDraw.Draw(mask)
|
| 69 |
+
|
| 70 |
+
# Desenhar retângulo com cantos arredondados
|
| 71 |
+
draw.rounded_rectangle(
|
| 72 |
+
[(0, 0), (img.width, img.height)],
|
| 73 |
+
radius=radius,
|
| 74 |
+
fill=255
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Aplicar máscara à imagem
|
| 78 |
+
output = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
| 79 |
+
output.paste(img, (0, 0))
|
| 80 |
+
output.putalpha(mask)
|
| 81 |
+
|
| 82 |
+
return output
|
| 83 |
+
|
| 84 |
def create_cover_image(image_url: str) -> BytesIO:
|
| 85 |
"""
|
| 86 |
Cria uma capa para reels do Instagram.
|
| 87 |
- Fundo preto
|
| 88 |
- Largura: 1080, Altura: 1920
|
| 89 |
- Imagem renderizada em Largura: 1080, Altura: 1440, X: 0, Y: 240
|
| 90 |
+
- Cantos arredondados com raio de 145px
|
| 91 |
"""
|
| 92 |
# Dimensões do canvas
|
| 93 |
canvas_width = 1080
|
|
|
|
| 105 |
img_height = 1440
|
| 106 |
filled_img = resize_and_crop_to_fill(img, img_width, img_height)
|
| 107 |
|
| 108 |
+
# Aplicar arredondamento de 145px nos cantos
|
| 109 |
+
rounded_img = add_rounded_corners(filled_img, 145)
|
| 110 |
+
|
| 111 |
# Colar imagem na posição X: 0, Y: 240
|
| 112 |
+
canvas.paste(rounded_img, (0, 240), rounded_img)
|
| 113 |
|
| 114 |
# Converter para bytes
|
| 115 |
buffer = BytesIO()
|