2011-02-22 47 views
0

我在python 3.1中编程,并使用更新为aswel的PIL。 现在,我的程序正在从网页获取信息,并且我希望将此信息绘制在我已经拥有的图片上,因此我正在使用这个变量,这也是我需要帮助的部分。 我链接是我计划的一部分(因为它是在100行我只是展示我发现realative的问题的部分)是否可以在PIL(python)中使用变量?

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

你'if'语句都不会做太多,因为字符串“死亡骑士”将始终评估为TRUE;。你可能想要:'如果“死亡骑士”在CL:' – Paul 2011-02-22 23:11:07

+1

你确切地想知道你是否可以使用变量? – joshcartme 2011-02-22 23:46:01

+0

猜测不是编程的好方法。你已经对如何使用if语句以及如何使用变量做了一些猜测。如果您从一本详细解释这些事情的Python手册开始,那最好。 - > http://diveintopython3.org/当你学习Python的时候,很清楚如何去做那些打扰你的事情。另外,通过使用正则表达式从HTML中提取信息,您可以“猜测”结构。你应该使用'lxml.html'来代替。 – 2011-02-23 11:51:49

回答

0

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

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

,并为您节省命令:

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