'Cannot open image using Pillow

I'm making a flask application for converting image formats, everything went well until it came to using Pillow, I can't even open the image even though it exists in the given path

Traceback (most recent call last):
  File "/data/data/com.termux/files/home/flask_projects/converter_website/env/lib/python3.10/site-packages/flask/app.py", line 2095, in __call__                                                              return self.wsgi_app(environ, start_response)
  File "/data/data/com.termux/files/home/flask_projects/converter_website/env/lib/python3.10/site-packages/flask/app.py", line 2080, in wsgi_app                                                              response = self.handle_exception(e)
  File "/data/data/com.termux/files/home/flask_projects/converter_website/env/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app                                                              response = self.full_dispatch_request()
  File "/data/data/com.termux/files/home/flask_projects/converter_website/env/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request                                                 rv = self.handle_user_exception(e)
  File "/data/data/com.termux/files/home/flask_projects/converter_website/env/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request                                                 rv = self.dispatch_request()
  File "/data/data/com.termux/files/home/flask_projects/converter_website/env/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)                       File "/data/data/com.termux/files/home/flask_projects/converter_website/app.py", line 24, in index
    foo(filename, filename_without_extension, path_to_converted_file)
  File "/data/data/com.termux/files/home/flask_projects/converter_website/app.py", line 13, in foo       file = Image.open(".user_images/" + filename)
  File "/data/data/com.termux/files/home/flask_projects/converter_website/env/lib/python3.10/site-packages/PIL/Image.py", line 3123, in open                                                                  raise UnidentifiedImageError(
PIL.UnidentifiedImageError: cannot identify image file '.user_images/aFrFkNJV-JE9ExzDh6nnlOBXi16cYOKSf14D02qLQQpOwEgf_XtzKX-Jo2a8X6BmRKmKgHytpbWrbkzG4vtQFPr.jpg'

Here is the code

@app.route("/", methods=["POST", "GET"])
def index():
    form = ConversionForm()
    if form.validate_on_submit():
        file = form.file_for_conversion.data
        filename = secure_filename(file.filename)
        filename_without_extension = re.sub(r"\.[a-z]{3,}$", "", f"{filename}") # this regex remove file extension from end of filename
        path_to_converted_file = ".user_images/" + filename_without_extension + "." +form.filetype.data
        print(filename, filename_without_extension, path_to_converted_file, sep="\n")
        file = Image.open(file)

    return render_template("index.html", form=form)

help me please

I found a solution - the image was corrupted


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source