cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
If you’ve changed your email address, now's the perfect time to update it on your Dropbox account and we’re here to help! Learn more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Python 3 files_download JPEG. Get UnicodeDecodeError

Python 3 files_download JPEG. Get UnicodeDecodeError

katec3
Explorer | Level 3
Go to solution

Using Python 3.8 I can successfully download a JPEG image. However, I want to access the exif information so am trying to use Pillow to open it. Whatever I try I get the following error: 'UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte'. I'd be grateful for any suggestions as I'm stumped.

 

Successful download code:

dbx = dropbox.Dropbox(os.environ['DROPBOX_TOKEN'])
for entry in dbx.files_list_folder('').entries:
md, response = dbx.files_download('/' + entry.name)

file_stream = response.content
print(len(file_stream), 'bytes; md:', md)

 

Code to open file_stream that results in error:

with open(file_stream, 'rb') as data:
# do things with file

 

Same error resulting from Pillow code:

image = Image.open(file_stream)
image.show()

 

Many thanks

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

The Python open method expects a path-like object in order to open a file, but your file_stream is the response.content, which is already the actual file data, not a path-like object. You can use it directly without calling open.

 

As for using Pillow, I can't offer help for that as it's not made by Dropbox, but it looks like there's an example in the requests documentation that may be helpful here.

View solution in original post

2 Replies 2

Greg-DB
Dropbox Staff
Go to solution

The Python open method expects a path-like object in order to open a file, but your file_stream is the response.content, which is already the actual file data, not a path-like object. You can use it directly without calling open.

 

As for using Pillow, I can't offer help for that as it's not made by Dropbox, but it looks like there's an example in the requests documentation that may be helpful here.

katec3
Explorer | Level 3
Go to solution

Thank you very much Greg, the link you sent solved it!

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    katec3 Explorer | Level 3
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?