猿问

Python 3.6 的成像问题

我正在创建人工智能来用 Python 玩中国跳棋,但我什至无法显示棋盘的图像!


我正在使用此代码:


from tkinter import *

root = Tk()

board = PhotoImage(file="board.ppm")

root.mainloop()

我收到以下错误:


Traceback (most recent call last):

  File "/Users/GAMEKNIGHT7/Desktop/genius hour/chineseCheckersAI(genius hour).py", line 3, in <module>

    board = PhotoImage(file="board.ppm")

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 3539, in __init__

    Image.__init__(self, 'photo', name, cnf, master, **kw)

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 3495, in __init__

    self.tk.call(('image', 'create', imgtype, name,) + options)

_tkinter.TclError: couldn't recognize data in image file "board.ppm"

我将代码文件放在与图像相同的文件中。发生了什么?我该如何解决?


慕标琳琳
浏览 174回答 1
1回答

收到一只叮咚

尝试在终端中运行以下命令:file&nbsp;board.ppm如果它里面有这个词ASCII,那意味着你的图像是未压缩的 ASCII(NetPBM类型=3),Tkinter不会喜欢它:board.ppm:&nbsp;Netpbm&nbsp;image&nbsp;data,&nbsp;size&nbsp;=&nbsp;10&nbsp;x&nbsp;10,&nbsp;pixmap,&nbsp;ASCII&nbsp;text如果您的文件正确,它将报告rawbits(NetPBM&nbsp;type=6):board.ppm:&nbsp;Netpbm&nbsp;image&nbsp;data,&nbsp;size&nbsp;=&nbsp;10&nbsp;x&nbsp;10,&nbsp;rawbits,&nbsp;pixmap如果你想从 ASCII/P3 转换为 binary/rawbits/P6,你可以像这样用homebrew安装ImageMagick:brew&nbsp;install&nbsp;imagemagick然后像这样转换:convert&nbsp;board.ppm&nbsp;-compress&nbsp;lossless&nbsp;board.ppm
随时随地看视频慕课网APP

相关分类

Python
我要回答