[英]By Using python pil, I want to make image centered with text from url and download. How to do it?
I input a text at url, and want to get downloaded image that the text is centered. 我在url中输入了文本,并希望获得文本居中的下载图像。
for example, when I input, http://127.0.0.1:8000/app/hello , then an image is downloaded. 例如,当我输入http://127.0.0.1:8000/app/hello时 ,便会下载图像。 That image centers the text 'hello' 该图像使文本“ hello”居中
def homework(request, name):
text = name
img = Image.new('RGB',(256,256),(0,0,0))
draw = ImageDraw.Draw(img)
font = ImageFont.load_default().font
draw.text((128,128), text, font = font, fill="black")
with open(img, 'rb') as f:
response = HttpResponse(f, content_type='image/jpeg')
response['Content-Disposition'] = 'attachment; filename="textimage"'
return response
By using python pil. 通过使用python pil。
How to do it? 怎么做?
#install PIL (Pillow) via Pip
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
#position of Text
x = 50
y = 50
#font color as tuple
color = (255, 255, 255)
#download the image to the local system and open it
img = Image.open("1.jpg")
#draw image and prepare font
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('arial.ttf', size=30)
#draw text on image
draw.text((x, y),"Sample Text", color,font=font)
#save the new image, that contains the text
img.save('sample-out.jpg')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.