【发布时间】:2013-05-14 09:23:32
【问题描述】:
所以我想将 .jpg 放在画布中,我在互联网上找到的只是使用 PIL,但我使用的是 Python 3.2,所以 PIL 不起作用。 如何使用 Python 3.2 在画布中插入 .jpg?
【问题讨论】:
标签: python python-3.x tkinter jpeg
所以我想将 .jpg 放在画布中,我在互联网上找到的只是使用 PIL,但我使用的是 Python 3.2,所以 PIL 不起作用。 如何使用 Python 3.2 在画布中插入 .jpg?
【问题讨论】:
标签: python python-3.x tkinter jpeg
只是为了让现在看到这个的其他人免于四处寻找零碎的东西(就像我刚刚做的那样)
正如 Martijn Pieters 所说,使用 Pillow 而不是 PIL,但代码看起来相同
from tkinter import Tk, Canvas
from PIL import ImageTk, Image
root = Tk()
#Create a canvas
canvas = Canvas(root, width=400, height=300)
canvas.pack()
# Load the image file
im = Image.open('test_image.jpg')
# Put the image into a canvas compatible class, and stick in an
# arbitrary variable to the garbage collector doesn't destroy it
canvas.image = ImageTk.PhotoImage(im)
# Add the image to the canvas, and set the anchor to the top left / north west corner
canvas.create_image(0, 0, image=canvas.image, anchor='nw')
root.mainloop()
【讨论】:
PIL 确实在 Python 3.2 上工作;安装 Pillow,友好的 PIL 分支。
Pillow 2.0.0 增加了对 Python 3 的支持,并包含许多来自 Internet 的错误修复。
【讨论】:
您只需要在画布中创建图像。确保您的图像与您的代码位于同一文件夹中。
image = PhotoImage (file="image.jpg")
yourcanvas.canvas.create_image (0, 0, anchor=NW, image=image, tags="bg_img")
应该这样做。这也会将画布扩展到图像的大小,只是为了让您知道。祝你的项目好运!
【讨论】: