简体   繁体   English

通过使用python pil,我想使图像居中,并包含来自url的文本并下载。 怎么做?

[英]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.

相关问题 如何在 python 中使用 PIL 将我的文本集中在图像上,并在 for 或 while 循环中以不同的名称保存图像? - How do I center my text on an image using PIL in python and save the image with a different name in a for or while loop? 使用python和PIL如何获取图像中的文本块? - Using python and PIL how can I grab a block of text in an image? 如何使用python的PIL以一定角度绘制文本? - How do I draw text at an angle using python's PIL? 如何使用Python PIL下载图像并提取Exif数据? - How do you download an image and extract Exif data using Python PIL? Python:URL 图像下载。 URL 包含重音字母。 我收到一个错误 - Python: URL images download. The URL contains an accented letter. I'm getting an error Python 使用 BeautifulSoup 和 PIL 从 url 获取图像路径和大小 - Python get image paths and sizes from a url using BeautifulSoup & PIL Python-从URL在Tkinter中打开图像-不使用PIL - Python - Open image within Tkinter from URL - not using PIL 从哪里下载 python PIL 模块? - Where do download python PIL module from? 如何使图像亮度均匀(使用 Python/PIL) - How to Make an Image Uniform Brightness (using Python/PIL) 如何使用 PIL 获得正确的 output 用于日本文本到图像? - How do i get correct output for japan text to image using PIL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM