2011-02-22 46 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) 
相關問題