I'm making a discord bot that sends a message whenever someones changes their avatar. It downloads their previous avatar and the new avatar, and joins them together, but I'm getting this error:
Ignoring exception in on_user_update
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "bot.py", line 40, in on_user_update
images = [Image.open(x) for x in ['originalAvatar.png', 'updatedAvatar.png']]
File "bot.py", line 40, in <listcomp>
images = [Image.open(x) for x in ['originalAvatar.png', 'updatedAvatar.png']]
File "/app/.heroku/python/lib/python3.6/site-packages/PIL/Image.py", line 2959, in open
"cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file 'originalAvatar.png'
This is my code:
@bot.event
async def on_user_update(before, after):
if before.avatar != after.avatar:
    mencao = after.mention
    channel = bot.get_channel(zzzzzzzzzzzzz)
    authorId = after.id
    tempo = datetime.date.today().strftime("%d/%m/%Y")
    originalFile = before.avatar_url_as(format='png', static_format='webp', size=128)
    updatedFile = after.avatar_url_as(format='png', static_format='webp', size=128)
    r = requests.get(originalFile, allow_redirects=True)
    open('originalAvatar.png', 'wb').write(r.content)
    s = requests.get(updatedFile, allow_redirects=True)
    open('updatedAvatar.png', 'wb').write(s.content)
    images = [Image.open(x) for x in ['originalAvatar.png', 'updatedAvatar.png']]
    widths, heights = zip(*(i.size for i in images))
    total_width = sum(widths)
    max_height = max(heights)
    new_im = Image.new('RGB', (total_width, max_height))
    x_offset = 0
    for im in images:
        new_im.paste(im, (x_offset, 0))
        x_offset += im.size[0]
    new_im.save('test.png')
    file = discord.File("test.png", filename="image.png")
    embed = discord.Embed(name="title", description=f"\U0001F5BC {mencao} **alterou o avatar**", color=0xe12674)
    embed.set_image(url="attachment://image.png")
    embed.set_footer(text=f"ID do usuário: {authorId} • {tempo}")
    await channel.send(file=file, embed=embed)
    if os.path.exists(f"originalAvatar.png"):
        os.remove(f"originalAvatar.png")
    if os.path.exists(f"updatedAvatar.png"):
        os.remove(f"updatedAvatar.png")
    if os.path.exists("test.png"):
        os.remove("test.png")
I'm running the python file on heroku btw, how do I fix this?
UPDATE:
I've replaced
open('originalAvatar.png', 'wb').write(r.content) with
 with open('originalAvatar.png', 'wb').write(r.content) as f:
            r.raw.decode_content = True
            shutil.copyfileobj(r.raw, f)
but now it gives me this error:
Ignoring exception in on_user_update
Traceback (most recent call last):
File "/home/lucas/.local/lib/python3.8/site-packages/discord/client.py", line 
343, in _run_event
await coro(*args, **kwargs)
File "/home/lucas/bot/bot.py", line 36, in on_user_update
with open('originalAvatar.png', 'wb').write(r.content) as f:
AttributeError: __enter__
and here is the on_message part of the code:
 @bot.event
 async def on_message(message):
 mensagem = message.content
 if mensagem.startswith("$avatar") and hasNumbers(mensagem) == False:
     nome = message.author.name
     avatar = message.author.avatar_url_as(size=2048)
     r = requests.get(avatar, allow_redirects=True)
     with open('Avatar.gif', 'wb').write(r.content) as f:
         r.raw.decode_content = True
         shutil.copyfileobj(r.raw, f)
