所以我想在画布上放一个.jpg,我在互联网上发现的只是使用 PIL,但我使用的是 Python 3.2,所以 PIL 不起作用。如何使用 Python 3.2 在画布中插入 .jpg?
问问题
18471 次
3 回答
7
只是为了避免其他人现在正在寻找这个点点滴滴(就像我刚刚做的那样)
正如 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()
于 2014-10-12T08:42:39.647 回答
0
PIL确实适用于 Python 3.2;安装Pillow,友好的 PIL 叉子。
Pillow 2.0.0 增加了对 Python 3 的支持,并包含来自 Internet 的许多错误修复。
于 2013-05-14T09:35:58.210 回答
-1
您只需要在画布中创建图像。确保您的图像与代码位于同一文件夹中。
image = PhotoImage (file="image.jpg")
yourcanvas.canvas.create_image (0, 0, anchor=NW, image=image, tags="bg_img")
那应该这样做。这也会将画布扩展到图像的大小,只是为了让您知道。祝你的项目好运!
于 2013-06-08T20:09:52.617 回答