0

I am creating artificial intelligence to play Chinese checkers with Python, but I can't even get an image of a board to show up!

I am using this code:

from tkinter import *
root = Tk()
board = PhotoImage(file="board.ppm")
root.mainloop()

I get the following error:

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"

I placed the code file in the same file as the image. What happened? How can I fix it?

Blckknght
  • 100,903
  • 11
  • 120
  • 169
F. Zeng
  • 85
  • 1
  • 2
  • 11
  • Well, Python is clearly not finding the file for some reason. To investigate, you may want to try printing out the result you get from calling `os.getcwd()` to make sure your working directory is what you expect. How are you running your script? – Blckknght Oct 30 '18 at 00:40
  • I am using the official pytho IDE to run and code my script on a mac. – F. Zeng Oct 30 '18 at 00:45
  • Do you mean a command line terminal, or IDLE? – Blckknght Oct 30 '18 at 00:45
  • I am using IDLE to make my code. – F. Zeng Oct 30 '18 at 00:48
  • Oh, and i am new to stack overflow, and i dont know how to edit my questions, so i will say it here. The error message says this at the last line: ' _tkinter.TclError: couldn't recognize data in image file "board.ppm"' . – F. Zeng Oct 30 '18 at 00:50
  • There's an "edit" link just below the tags on your question (or click here: [edit]). If the traceback has changed, please update the whole thing! – Blckknght Oct 30 '18 at 00:57
  • So the new exception message suggests that your file doesn't contain image data that the library can read. Are your sure your PPM file is valid? That *should* be a datatype that `tkinter` can read for you, so I'd suspect the file being the problem rather than the code. – Blckknght Oct 30 '18 at 01:14

1 Answers1

0

Try running the following in Terminal:

file board.ppm

If it says anything with the word ASCII in it, that means your image is uncompressed ASCII (NetPBM type=3) and Tkinter will not like it:

board.ppm: Netpbm image data, size = 10 x 10, pixmap, ASCII text

if your file is correct, it will report rawbits (NetPBM type=6):

board.ppm: Netpbm image data, size = 10 x 10, rawbits, pixmap

If you want to convert from ASCII/P3 to binary/rawbits/P6, you could install ImageMagick with homebrew like this:

brew install imagemagick

Then convert like this:

convert board.ppm -compress lossless board.ppm
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • When i ran this in terminal, neither the words ASCII or rawbits appeared:board.ppm: JPEG image data, JFIF standard 1.01, resolution (DPI), density 300x300, segment length 16, Exif Standard: [TIFF image data, big-endian, direntries=4], baseline, precision 8, 600x690, frames 3 – F. Zeng Oct 30 '18 at 01:51
  • Ok, that means your file is a JPEG image. Try changing the name to `board.jpg` and then `board = PhotoImage(file="board.jpg")` – Mark Setchell Oct 30 '18 at 01:52
  • It still returned the same thing, except it couldn't recognize the data in board.jpg – F. Zeng Oct 30 '18 at 01:56
  • Or you could try converting to `GIF` with `sips -s format gif board.jpg --out board.gif` and then load `board.gif` – Mark Setchell Oct 30 '18 at 01:56
  • Wow! It didn't return a error! But the tkinter window is blank. I am not that good a gui coding yet. – F. Zeng Oct 30 '18 at 02:03
  • I guess the image is too big for the window, so i somehow need to expand it. Or maybe im just not displaying the image. – F. Zeng Oct 30 '18 at 02:07
  • OH MY... I just managed to get it right, by using the code in this link:https://stackoverflow.com/questions/10133856/how-to-add-an-image-in-tkinter. I am actually really excited. Thanks! – F. Zeng Oct 30 '18 at 02:14