是否可以在 PIL (python) 中使用变量?

发布于 2024-10-18 13:39:57 字数 2334 浏览 4 评论 0原文

我正在使用 python 3.1 进行编程并使用 PIL,该 PIL 也已更新。 现在,我的程序正在从网页获取信息,并且我希望在我已经获得的图片上绘制该信息,因此我为此使用了一个变量这也是我需要帮助的部分。 我链接的是我的程序的一部分(因为它超过 100 行,我只是显示我发现与问题相关的部分)

import webbrowser
import random
import urllib.request, urllib.parse, urllib.error
import re
import PIL
from PIL import Image, ImageFont, ImageDraw

arial16 = ImageFont.truetype ("arial.ttf", 16)

Z = (input ("What is the character name? "))

#The link which i get from my input info..
url = "http://"+X+".battle.net/wow/en/character/"+Y+"/"+Z+"/simple"

# Read the webpage
html = urllib.request.urlopen(url).read()

# Encoding
encoding = "utf-8"
html =html.decode (encoding)

# Find right class & choose background from it
cl = re.findall ('class="class">(.*)</a><span class="comma">,</span>', html) 

if "Death Knight" : im = Image.open ("DeathKnightBackground.png")
elif "Druid" : im = Image.open ("DruidBackground.png")
elif "Hunter" : im = Image.open ("HunterBackground.png")
elif "Mage" : im = Image.open ("MageBackground.png")
elif "Paladin" : im = Image.open ("PaladinBackground.png")
elif "Priest" : im = Image.open ("PriestBackground.png")
elif "Rogue" : im = Image.open ("RogueBackground.png")
elif "Shaman" : im = Image.open ("ShamanBackground.png")
elif "Warlock" : im = Image.open ("WarlockBackground.png")
elif "Warrior" : im = Image.open ("WarriorBackground.png")

# Find the Title
title = re.findall('<div class="title">(.*)</div>', html)

# If i want to print this i just do
print (("Your Charactername with it's title:")+Z, title)
print (("You are:"),cl)

# Then i want to use a variable to save my picture
S = input("Please enter a name to save your forumsignature: ")

# When i want to draw the text to the picture i tried to use (+Z, title) as in the print
# function - but since this didn't work i will just go with ((+Z, title)) which don't give me
# syntax error

draw = ImageDraw.Draw ( im )
draw.text ( (20,20), ((+ Z, title)), font=arial16, fill="white")

# As i said before, i'm using variable S for saving and tried this way.. but in all the ways i
# have tried, i always keep getting syntax errors 

im.save ((+S))

im.show ((+S))

那么有谁知道 PIL 是否能够以这种方式处理变量?或者您是否知道使用 python 3.1 从图像变量中绘制文本的其他方法?

非常高兴得到回复,我将继续尝试测试我所知道的各种不同方法,以使其发挥作用并获得成功。如果我有事可做,请在这里发帖...

// Zabs

i'm programing in python 3.1 and using a PIL which are updated for it aswel.
Now it's so that my program is getting info from a webpage and i want this info to be drawed at a picture which i already got, so i'm using a variable for this & it's also the part i need help with.
What I link is a part of my program (since it's over 100 lines i'm just showing the parts which i found realative to the question)

import webbrowser
import random
import urllib.request, urllib.parse, urllib.error
import re
import PIL
from PIL import Image, ImageFont, ImageDraw

arial16 = ImageFont.truetype ("arial.ttf", 16)

Z = (input ("What is the character name? "))

#The link which i get from my input info..
url = "http://"+X+".battle.net/wow/en/character/"+Y+"/"+Z+"/simple"

# Read the webpage
html = urllib.request.urlopen(url).read()

# Encoding
encoding = "utf-8"
html =html.decode (encoding)

# Find right class & choose background from it
cl = re.findall ('class="class">(.*)</a><span class="comma">,</span>', html) 

if "Death Knight" : im = Image.open ("DeathKnightBackground.png")
elif "Druid" : im = Image.open ("DruidBackground.png")
elif "Hunter" : im = Image.open ("HunterBackground.png")
elif "Mage" : im = Image.open ("MageBackground.png")
elif "Paladin" : im = Image.open ("PaladinBackground.png")
elif "Priest" : im = Image.open ("PriestBackground.png")
elif "Rogue" : im = Image.open ("RogueBackground.png")
elif "Shaman" : im = Image.open ("ShamanBackground.png")
elif "Warlock" : im = Image.open ("WarlockBackground.png")
elif "Warrior" : im = Image.open ("WarriorBackground.png")

# Find the Title
title = re.findall('<div class="title">(.*)</div>', html)

# If i want to print this i just do
print (("Your Charactername with it's title:")+Z, title)
print (("You are:"),cl)

# Then i want to use a variable to save my picture
S = input("Please enter a name to save your forumsignature: ")

# When i want to draw the text to the picture i tried to use (+Z, title) as in the print
# function - but since this didn't work i will just go with ((+Z, title)) which don't give me
# syntax error

draw = ImageDraw.Draw ( im )
draw.text ( (20,20), ((+ Z, title)), font=arial16, fill="white")

# As i said before, i'm using variable S for saving and tried this way.. but in all the ways i
# have tried, i always keep getting syntax errors 

im.save ((+S))

im.show ((+S))

So does anyone know if PIL are able to work with variables in this kind of way? or does you know some other way to draw text from variables at an image with python 3.1?

Very cheerful for replies and i will keep trying to test all kind of different ways i know to get this to work & post up here if i get something to work...

// Zabs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀中猫帐中妖 2024-10-25 13:39:57

这是一个语法错误。添加更多括号只会产生一个新的数据结构。尝试:

text = Z + " " + title
draw.text ( (20,20), text, font=arial16, fill="white")

对于您的保存命令:

im.save(S)
im.show(S)

It is a syntax error. Adding more parenthesis just makes a new datastructure. Try:

text = Z + " " + title
draw.text ( (20,20), text, font=arial16, fill="white")

and for your save command:

im.save(S)
im.show(S)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文