使用python PIL粘贴时出现黑屏

时间:2016-07-14 15:14:59

标签: python tkinter python-imaging-library

我正在尝试端到端粘贴多个图像,然后在画布上显示。我无法弄清楚为什么这张图片显示为黑色。任何想法?

from tkinter import *
from PIL import Image, ImageTk

root = Tk()
canvas = Canvas(root, width=1000, height=800)
canvas.pack()

grass = Image.open(r"C:\pathto\grass.gif")
water = Image.open(r"C:\pathto\ffwater.gif")

r1 = ['g','g','g','w','w','w','g','g']
row = Image.new('RGB', (1000, 800))

xx = 0
for i in r1:
    if i == 'g':
        img = grass
    if i == 'w':
        img = water
    row.paste(img, (xx, 0))
    xx = xx + img.size[0]

worldr1 = ImageTk.PhotoImage(row)
canvas.create_image(0,0, image=worldr1)

root.mainloop()

enter image description here enter image description here

1 个答案:

答案 0 :(得分:3)

图像为黑色,因为它仅在“画布”上部分可见。我换了

canvas.create_image(0,0, image=worldr1)

通过

canvas.create_image(0,0, anchor="nw", image=worldr1)

并且在画布上可以看到完整图像(默认值为anchor="center")。