0

I am running a bot that periodically downloads an image from a website and uploads it to Twitter. Sometimes, however, the images get grey bars on the bottom, resulting in uploaded images that look like this or this.

This is the code that downloads the image as 'localImage.jpg' and overwrites the previous image:

image_stream = requests.get('linkToImage', stream=True)
local_image = open('localImage.jpg', 'wb')
image_stream.decode_content = True
shutil.copyfileobj(image_stream.raw, local_image)

The entire repository is here.

My question is what is it that could be causing the image to upload with the grey boxes on the bottom?

  • I don't know exactly what the problem is, seems like not writing the last chunk of image info. Try out this answer, might just solve your problems without having to debug further: https://stackoverflow.com/a/14114741/1609219 – Macattack Jun 24 '20 at 23:28
  • Thanks. It wasn't exactly what I needed but it pointed me in the right direction. – Daniel Lenshin Jun 25 '20 at 00:43

1 Answers1

0

While I couldn't figure out exactly why the grey boxes were appearing over the images, I did find a solution in the answer to this question.

image_stream = requests.get(extracted[2], stream=True)
with  open('localImage.jpg', 'wb') as image:
    for chunk in image_stream:
        image.write(chunk)

Instead of using shutil's copyfileobj method, I used the .write method to write chunks of the stream to the local image. This seems to have solved the problem of the grey boxes.