【问题标题】:How to load .bmp file into BitmapImage class Tkinter python如何将.bmp文件加载到BitmapImage类Tkinter python中
【发布时间】:2010-10-12 09:05:46
【问题描述】:

我无法找到任何方法将 .bmp 文件加载到 Tkinter() 以便我可以在画布小部件中使用它!请帮助我!

from Tkinter import *
from PIL import Image
import ImageTk
import tkFileDialog
import tkMessageBox
root=Tk()
class lapp:
   def __init__(self,master):
      w=Canvas(root,width=300,height=300)
      w.pack()
      p=Image.open("001.bmp")
      tkimage=ImageTk.PhotoImage(p)
      w.creat_image(0,0,image=tkimage)
App=lapp(root)
root.mainloop()

它没有在画布上显示任何图像,它只是空白! 顺便说一句,我正在使用带有 python 2.7 的 win7

【问题讨论】:

  • 你能给我们提供更多细节吗?可能堆栈跟踪或源代码 sn-p 会给我们一些胶水你实际在做什么。

标签: python bitmap tkinter python-imaging-library bmp


【解决方案1】:

这对我有用。

当我使用 Tk PhotoImage 类时,图像不显示。但是在使用 PIL 时它工作正常。

我的图像尺寸是 50*250,所以我放置了以它为中心的坐标 (25, 125)

from Tkinter import *
from PIL import Image, ImageTk

root=Tk()

root.title("My Image")

w = Canvas(root, width=50, height=250)
image = Image.open("blog0.bmp")
w.create_image((25, 125), image=ImageTk.PhotoImage(image))

w.pack()

root.mainloop()

希望对你有帮助

【讨论】:

  • Traceback(最近一次调用最后):文件“C:\Users\Administrator accoun\Desktop\Graphics\imageload”,第 9 行,在 image = Image.open("G:/ pop.bmp") 文件 "C:\Python27\lib\site-packages\PIL\Image.py",第 1980 行,在打开中引发 IOError("cannot identify image file") IOError: cannot identify image file
  • 奇怪的是,如果 PIL 无法打开您的图像。您是否尝试过使用其他 bmp?
  • 何解决了你只需要制作 p 和 Tkimage 全局变量就可以了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-24
  • 2014-02-16
  • 2018-06-17
  • 1970-01-01
  • 2013-01-31
相关资源
最近更新 更多