I'm trying to get a random avatar, if I don't resize it, I can use it without any problem, but when I resize it gives me an unsupported type error.
My Code:
resp = requests.get('https://www.generatormix.com/random-female-celebrities', stream=True)
soup = BeautifulSoup(resp.content, "html.parser")
elements = soup.find_all("img", class_="thumbnail")
rand_photo = elements[:6][random.randint(0, 5)]['data-src']
rand_photo = requests.get(rand_photo)
pilImage = Image.open(BytesIO(rand_photo.content))
pilImage = pilImage.resize((250, 250), Image.ANTIALIAS)
await client.user.edit(password=str(data.get()['Settings']['password']), avatar=pilImage.tobytes())
Error output:
C:\Users\fatih\OneDrive\Masaüstü\Python\discord-project\main.py:1204: DeprecationWarning: ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use LANCZOS or Resampling.LANCZOS instead.
pilImage = pilImage.resize((250, 250), Image.ANTIALIAS).convert("RGBA")
Ignoring exception in on_ready
Traceback (most recent call last):
File "C:\Users\fatih\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Users\fatih\OneDrive\Masaüstü\Python\discord reklam-project\main.py", line 1205, in on_ready
await client.user.edit(password=str(data.get()['Settings']['password']), avatar=pilImage.tobytes())
File "C:\Users\fatih\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\user.py", line 478, in edit
avatar = _bytes_to_base64_data(avatar_bytes)
File "C:\Users\fatih\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\utils.py", line 323, in _bytes_to_base64_data
mime = _get_mime_type_for_image(data)
File "C:\Users\fatih\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\utils.py", line 319, in _get_mime_type_for_image
raise InvalidArgument('Unsupported image type given')
discord.errors.InvalidArgument: Unsupported image type given
This should solve your problem:
# ...
pil_image = pil_image.resize((250, 250), Image.ANTIALIAS)
bytes_image = BytesIO()
pil_image.save(bytes_image, format='JPEG')
await client.user.edit(
password=str(data.get()['Settings']['password']),
avatar=bytes_image.getvalue()
)