今天我正在嘗試學習Python中的PIL/Pillow。
我用下面的代碼:
import PIL
import PIL.Image as Image
import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont
font = ImageFont.truetype("C:\Windows\Fonts\Verdanab.ttf", 80)
img = Image.open("C:/Users/imagem/fundo_preto.png")
draw = ImageDraw.Draw(img)
filename = "info.txt"
for line in open(filename):
print line
x = 0
y = 0
draw.text((x, y),line,(255,255,255),font=font)
img.save("a_test.png")
x += 10
y += 10
我不`噸知道「draw.text()」函數的作品,但我試圖軟件寫一個黑色的背景圖片,我有以下。
Line 1
Line 2
Line 3
Line 4
Line 5
我得到的是這些線一個比其他在同一行。
這個函數是如何工作的,我如何獲得不同位置的線的位置,而不是一個位於另一個位置。
x'和'y'應當在循環之前被移動的'初始化,否則'X + = 10; y + = 10'不會有任何效果。我也會在循環後保存圖像。字體路徑可以作爲原始字符串'r「C:\ Windows \ Fonts \ Verdana.ttf」'給出,以防止字符串中反斜槓的特殊含義。 –
因爲我們正在談論字體。我是否得到模仿終端字體的字體?我試圖模擬一個命令提示終端的屏幕,但我使用的字體並不是那麼好。 –
也許,你想使用一個單間隔字體。順便說一句,對於Windows的字體目錄來說,這是一種更便捷的方式:'os.path.join(os.environ ['WINDIR']''Fonts')'。 –