Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shutil.copyfileobj incorrectly copied data

Tags:

python

In order to work with some picture (download it, detect format and store) I use the following code:

image = urllib.request.urlopen(img_url)
buf = io.BytesIO()
shutil.copyfileobj(image, buf)
ext = imghdr.what(buf)

And ext was empty (meaning format could not be detected). I tried redoing it a bit differently:

image = urllib.request.urlopen(link)
test = image.read()
binbuf = io.BytesIO(test)
imghdr.what(binbuf)

And this actually worked. My conclusion is that copyfileobj messes thing up somehow. Why is it happening?

like image 675
RomaValcer Avatar asked Mar 11 '26 12:03

RomaValcer


1 Answers

After you write to buf with shutil.copyfileobj(image, buf), buf's stream pointer is pointing at the next position after the end of the data you just wrote. When you try to do ext = imghdr.what(buf), it reads nothing (because there's nothing more to read from that position) and returns None. You need to .seek() back to 0 before trying to read what you just wrote.

This works:

image = urllib.request.urlopen(img_url)
buf = io.BytesIO()
shutil.copyfileobj(image, buf)
buf.seek(0)
ext = imghdr.what(buf)
like image 135
Cyphase Avatar answered Mar 13 '26 02:03

Cyphase



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!