Python PIL UnidentifiedImageError

55 Views Asked by At

**I'm trying to upload an image as base64 to a database on pythonanywhere, and be able to retrieve those images and display them.

I have this code, which works completely: **

root = tk.Tk()
root.withdraw()
filepath = filedialog.askopenfilename()
with open(filepath, "rb") as image_file:
    encoded = base64.b64encode(image_file.read())
    print(encoded)
decoded = base64.b64decode(encoded)
img = Image.open(io.BytesIO(decoded))
img_path = "temp_image.png"
img.save(img_path)
subprocess.Popen(["open" if os.name == "posix" else "start", img_path], shell=True)
root.mainloop()

**I split it up into various different functions in my class. I know that add_post() works minus calling decode_and_display(). The plan is, instead of passing in the encoded string, to get it from the database, but I can do that later.


    def get_image(self, filename):  

        with open(filename, "rb") as img:
            encoded = base64.b64encode(img.read())
        return encoded

    def decode_and_display(self, encoded):
        decoded = base64.b64decode(encoded)
        to_display = Image.open(io.BytesIO(decoded))
        img_path = "post.png"
        to_display.save(img_path)
        subprocess.Popen(["open" if os.name == "posix" else "start", img_path], shell=True)

    def add_post(self):
        accountid = 1
        window = tk.Tk()
        tk.Label(window, text='New post!').grid(row=1)
        tk.Label(window, text="Caption: ").grid(row=2)
        caption_entry = tk.Entry(window)
        caption_entry.grid(row=2, column=1)
        tk.Label(window, text="Area: ").grid(row=3)
        area_entry = tk.Entry(window)
        area_entry.grid(row=3, column=1)
        file = str(self.get_filename())
        img = str(self.get_image(file))
        def post(accountid, image):
            area = int(area_entry.get())
            caption = caption_entry.get()
            return print(json.loads(requests.post('##my web address##', json={'accountid':accountid, 'caption':caption, 'image':image, 'area':area, 'operation':'add_post'}).text))
        enter = tk.Button(window, text='Enter', command=lambda: post(accountid, img))
        enter.grid(row=4)
        self.decode_and_display(img)
        window.mainloop()

**However, it it gave me this error: **

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Me\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\Me\Documents\Bianca Computing NEA\NEA Client Code (2).py", line 130, in add_post
    self.decode_and_display(img)
  File "C:\Users\Me\Documents\Bianca Computing NEA\NEA Client Code (2).py", line 106, in decode_and_display
    to_display = Image.open(io.BytesIO(decoded))
  File "C:\Users\Me\lib\site-packages\PIL\Image.py", line 3298, in open
    raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x0000006E8E0DBB88>

I have no idea what it means. I'm probably just missing something really obvious!

0

There are 0 best solutions below