adafruit / Python-Thermal-Printer

217 stars 147 forks source link

Error running "main.py" #53

Closed mattpackwood closed 2 years ago

mattpackwood commented 2 years ago

Hi, I am getting the following...

Traceback (most recent call last):
  File "main.py", line 101, in <module>
    printer.printImage(Image.open('gfx/hello.png'), True)
  File "/home/pi/Python-Thermal-Printer/Adafruit_Thermal.py", line 552, in printImage
    image = Image.open(image_file)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2643, in open
    prefix = fp.read(16)
AttributeError: 'PngImageFile' object has no attribute 'read'

Matt

cefoster0 commented 2 years ago

I'm getting the same error

abetusk commented 2 years ago

I was getting the same error. The error comes from the function printer.printImage needing a filename (string) not an image file (see here). Changing line 101 in main.py to:

# Print greeting image
printer.printImage('gfx/hello.png', True)
printer.feed(3)
GPIO.output(ledPin, GPIO.LOW)

should get past this hurdle, though there might be other problems lurking (I haven't tried running the full project or main.py).

Note that the other call to printImage in main.py (line 43) will also need to be changed. Presumably the sudoku-gfx.py file has similar errors in it as well. I'm giving up on chasing all the bugs but I would bet every script in this project that tried to print a PNG file will now stop working.

I would guess that someone introduced breaking changes to the and this is what broke all the scripts in this project. From a cursory look, it seems like the breaking change was here by @galacticfan on May 20, 2020.

CodeDelux commented 2 years ago

Abetusk, I don't think this will work because there are other calls to that function requiring the Image.new. We had to change Adafruit_Thermal.py to get this to work correctly.

Unfortunately, we are new to GitHub and still trying to figure out how to propose our changes :).

CodeDelux commented 2 years ago

Just added a pull request labeled "Add files via upload" with our fixed code.

PaintYourDragon commented 2 years ago

Merged. Thank you muchly!

mattpackwood commented 2 years ago

@CodeDelux

I tried runningpython3 main.py and I get:


Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 265, in open
    self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
FileNotFoundError: [Errno 2] No such file or directory: '/dev/ttyS0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 22, in <module>
    from datetime import date
  File "/home/pi/Python-Thermal-Printer/datetime.py", line 16, in <module>
    printer = Adafruit_Thermal("/dev/ttyS0", 19200, timeout=5)
  File "/home/pi/Python-Thermal-Printer/Adafruit_Thermal.py", line 84, in __init__
    Serial.__init__(self, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 240, in __init__
    self.open()
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 268, in open
    raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 2] could not open port /dev/ttyS0: [Errno 2] No such file or directory: '/dev/ttyS0
```'

Crazy question, is it trying to use Serial (I am using a USB printer)?
abetusk commented 2 years ago

@mattpackwood

This is most likely a separate error from the initial error that was reported in this issue, so might be worth creating it's own issue.

I will say that the /dev/ttyS0 is only referenced in datetime.py in this repo, so I'm confused as to why this is even being called. I'm not up on the intricacies of Python and how it manages its libraries, but one simple thing to try is to rename the file datetime.py in the repo as something else (test_datetime_example.py for example) and then try to see if that solves the problem.

Note that the datetime.py is being called from the from datetime import date line in main.py. I would be shocked if this were the intent as datetime is a common Python library and date is not a method provided in the local datetime.py. I suspect Python is trying to load the local file rather than the standard library. If so, this probably should be a separate bug report to tell the maintainer to rename the local datetime.py to something that won't conflict with the Python library (like test_datetime_example.py, for example).

CodeDelux commented 2 years ago

We only use the built in python datetime and we don’t have a local version. So, I think @abetusk is right and you should rename it.

mattpackwood commented 2 years ago

OK, I think that is "all set", I agree there is something "up" with datetime.py I don't know how to put in a pull request to "delete a file" so I will put in a new issue,