PPM file data cannot be recognized

I'm writing a simple picture editor. It uses PPM files. From what I can tell, I feel like my code should work. However, I get this error

Traceback (most recent call last):
  File "/home/zach/Downloads/piceditor (1).py", line 84, in <module>
  File "/home/zach/Downloads/piceditor (1).py", line 69, in main
    image = Image(Point(100,100), filename)
  File "/home/zach/Downloads/graphics.py", line 770, in __init__
    self.img = tk.PhotoImage(file=pixmap[0], master=_root)
  File "/usr/lib/python3.1/tkinter/__init__.py", line 3272, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/usr/lib/python3.1/tkinter/__init__.py", line 3228, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "pig.ppm"

My code looks like this

def main():
print("Image Editor")
filename = input("name of image file: ")

with open(filename) as f:
    formatind = f.readline()
    width, height = [int(x) for x in f.readline().split()]
    colordepth = f.readline()
    array = []
    for line in f:
        array.append([int(x) for x in line.split()])

win = GraphWin("Image Editor!", width, height)

image = Image(Point(100,100), filename)

Display(image, array, width, height, win)



And my Display function looks like this

def Display(image, array, width, height, win):

for i in range(width):
    for j in range(0, height, 3):
        colors = color_rgb(array[i][j], array[i][j+1], array[i][j+2])
        image.setPixel(i, j, colors)


This is the ppm file i'm using

6 8
249 249 249 255 255 255 250 250 250 255 255 255 250 250 250 250 250 250 254 255 255 251 255 255 
249 251 255 253 249 255 255 248 255 255 234 255 255 242 255 255 245 253 255 246 243 255 253 241 
255 255 237 255 255 237 252 255 241 249 255 246 249 255 253 254 255 255 255 252 255 255 248 241 
255 251 239 254 247 241 252 254 253 252 255 255 251 255 255 242 242 242 255 255 255 241 241 241 
0 0 0 0 0 0 4 4 4 20 20 20 236 236 236 252 252 252 254 255 253 248 255 250 
0 0 0 0 0 0 4 4 4 20 20 20 236 236 236 252 252 252 254 255 253 248 255 250 

I cannot for the life of me figure out why it won't recognize the data in the file.

Any help would be great. Thanks

Asked by: Walter977 | Posted: 06-12-2021

Answer 1

Why don't you use the PIL library? In the documents it claims that it can work with PPM files. However I am not familiar with working with PPM files with PIL.

Example: Opening a a PPM file, creating an object from the file that then can be used to edit the file.

Answered by: Edgar237 | Posted: 07-01-2022

Similar questions

python - Why are my App tests not being recognized by Django tests?

This question already has an answer here:

python - Django views: Object value not recognized

I am trying to learn django from official tutorial. I am stuck with a strange issue, it may be trivial but I am not able to figure it out - I am following this tutorial : Tutorial 3 My problem is that when I try to access http://hello.djangoserver:8080/poll, I am getting following out...

python - named url is not recognized

I'm trying to use a named url in my template, but It's not working. I already tried changing the name of the url called in the template to 'progress', which is another url conf in urls.py. And it works fine. But when I change it to UserSair, it just crashes. I'm getting this error: NoReverseMatch at / Reverse for 'UserSair' with arguments '()' and keyword argum...

python - model not being recognized

I'm trying to use a model from one app into another one's views, but Django is not recognizing it's name and giving the following error: local variable 'user_status' referenced before assignment This is my model: class user_status(models.Model): name = models.CharField(max_length=500) code = models.IntegerField() def __unicode__(self): return u'%s' % se...

python - iCal Feed Served Through Django Not Recognized

I'm having difficulty generating a valid iCal stream using Django. The problem I'm having is that my iCal file and stream is valid. I can visit the stream URL to download the .ics file and validate it only, import it to iCalendar or Google Calendar just fine, etc. In fact, if I host the same .ics file as a static file on my Django server and subscribe to that static URL from Google Calendar, it also works just fine...

python - 'pip' is not recognized

I tried to install PySide but I got error from the power shell as follows: pip : The term 'pip' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + pip install -U PySide + ~~~ + CategoryInfo ...

Python - Input from user is not recognized as "Y" or "N"

I'm trying to write a simple program to get myself back into python after a long break caused by my personal life. The purpose of the program is to do as follows: Ask the user to enter a number equal to 5 (very simple, I know) Check if what the user inputs is valid Check if the user's input is equal to 5 Ask the user if they would like to run the program again Run the progr...

Python Simple Class not Recognized

class Example: values=None s1=Example s1.values.append(1) s1.values.append(2) for x in s1.values: print x Using Python 2.7 Output: line 8, in Example s1=Example NameError: name 'Example' is not defined enter code here New to python and not sure why it's not recognizing the simple class.

python - Input error recognized just first time, second time it starts to run

When I'm trying to select a wrong choice it gave me the error the first time. Second time it starts to run the "Select level" block. For example if I input choice "12", the program returns the error and put me to select again and this time I input "13" it returns the "Select level" block. I need to put a while or what there to verify the choice ever time? import random import sys choice = None while choic...

python - AWS Athena not being recognized in Boto3?

I am trying to use AWS Athena from both the CLI and through boto3 but for some reason it is not being recognized. I have upgraded to the newest version of boto3 boto3.__version__ &gt;&gt;'1.4.4' aws --version &gt;&gt;aws-cli/1.11.56 Python/3.6.0 Darwin/15.6.0 botocore/1.5.19 When I go to do client = boto3.client('athena') I am greeted with: Traceback (most rec...

python - Why would an "command not recognized" error occur only when a window is populated?

My record sheet app has a menu option for creating a new, blank record sheet. When I open a sheet window, I can open new windows without a problem, using subprocess.Popen() to do it. However, under Windows (I haven't tested it on other OSes yet), if I open a new window then use the "open file" dialog to populate the fields with data from a file, I'm no longer able to create new windows. Once it's populated, Windows...

python - Django custom auth backend not recognized on Apache

I'm trying to deploy my Django application to an Apache2 based server with mod_python. I've set the handlers right and made the configuration to make mod_python work with my project. My project implements a custom auth backend to connect my users to twitter, and my backend implementation is on: myproject |- backends/ directory.Everything seems to be working fine, my pages load and I can ma...

python - My method is being recognized within my own program. Newbie mistake probably

Here's my code: sentenceToTranslate = raw_input("Please write in the sentence you want to translate: ") words = sentenceToTranslate.split(" ") for word in words: if isVowel(word[0]): print "TEST" def isVowel(letter): if letter.lower() == "a" or letter.lower() == "e" or letter.lower() == "i" or letter.lower() == "o" or letter.lower() == "u": return True else: return Fal...

Trying to use py2exe, but python is not recognized

I am following the the tutorial at http://www.py2exe.org/index.cgi/Tutorial to figure out how to use py2exe. I get down to step 3 where you are supposed to run the command: python setup.py py2exe I do that and then I get this error: 'python' is not recognized as an internal or external command,...

python - How to have recognized all the libraries Rpy2 R

How to have recognized all the libraries Rpy2 R. Rpy2 not recognizing the libraries, utils, and tools. import rpy2.robjects as robjects R = robjects.r &gt;&gt;&gt; R['library']("utils") RVector - Python:0x7f65fc85cfc8 / R:0x19bb980 &gt;&gt;&gt; R['library']("tools") RVector - Python:0x7f65fc85f5a8 / R:0x2419140 (>>> from rpy2.robjects.packages import importr Traceback (most ...

python - Not recognized non existant file

im getting a weird error using this code (part of a class): from sys import path as workingDIR from os import system, path image = '' # some jpeg image data keep = 0 DIR = workingDIR[0] + '\\image' if path.isfile(DIR + '.jpeg'): # adding numbers to end of file name like how windows prevents multiple files having the same name x = 2 while path.isfile(DIR + ' (' + str(x) + ').jpeg'): ...

python - How to fix shebang flags that are not recognized on some systems

For some reason, the -O (optimized) flag is not recognized in the shebang line on a Red Hat Enterprise Server (release 5.3) that I access. On other systems, the flag is recognized without any issue. Executing the script below on OS X works fine. Recognition of the -O flag can be verified because it enables (when absent) or disables (when given) anything under the if __debug__

python - SCons, MSys, "not recognized as an internal or external command" error

I'm trying to use SCons to set up a project for compilation on Windows 7. In the MSys shell, I cd to the appropriate folder and run: scons target=setup ...and I get the following output: scons: Reading SConscript files ... SCons 2.0.1 OS="'{' is not recognized as an internal or external command, operable program or batch file." Compiler version check failed - need gcc 3.x o...

python - 'twistd' is not a recognized internal or external command

I'm trying to develop a Twisted Web server but can't seem to run the twistd command. I've tried setting the python path and even included the path to the twistd.py script in my Path but nothing seems to work. I'm using Twisted 12.0.0 and Python 2.7 on Windows. Any help would be hugely appreciated.

tkinter - Python-tk package not recognized in Python 2.7.3

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python