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

时间:2011-02-22 22:55:24

标签: python variables text python-imaging-library draw

我正在使用python 3.1进行编程并使用为其更新的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

1 个答案:

答案 0 :(得分:0)

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

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

并保存命令:

im.save(S)
im.show(S)
相关问题