color: #999999;
요구사항 충족하기출처: https://stackoverflow.com/questions/7510313/transparent-png-in-pil-turns-out-not-to-be-transparent
from PIL import Image, ImageFont, ImageDraw
import requests
from io import BytesIO
def apply_watermark(main_img_url):
"""
파라미터명: main_img_url
파라미터 값: AWS S3 객체 URL로, "https://~~.jpg" 형태
"""
try:
padding_right = 12
padding_bottom = 16
main_img_url = main_img_url
response = requests.get(main_img_url)
ioFile = BytesIO(response.content)
main_image = Image.open(ioFile).convert('RGBA')
main_width, main_height = main_image.size
# 워터파크 반영 문구
text = "쿠돈에서 직접 촬영한 사진입니다"
font = ImageFont.truetype("./Spoqa Han Sans Regular.ttf", 24)
text_width, text_height = font.getsize(text)
# 투명 배경 생성
watermark = Image.new('RGBA', (text_width, text_height), 0)
draw = ImageDraw.Draw(watermark)
# 투명 배경에 워터마크 반영
draw.text((0, 0), text=text, fill=(153, 153, 153, 85), font=font)
# 메인 배경 우측 하단 워터마크 paste
main_image.paste(
watermark,
(main_width - text_width - padding_right,
main_height - text_height - padding_bottom),
mask=watermark)
return main_image
except Exception as e:
print(e)
return False