Discord.py 头像 tobytes 引发错误 图像

huangapple go评论109阅读模式
英文:

Discord.py avatar tobytes raise error image

问题

以下是您提供的代码的翻译部分:

我的代码

  1. resp = requests.get('https://www.generatormix.com/random-female-celebrities', stream=True)
  2. soup = BeautifulSoup(resp.content, "html.parser")
  3. elements = soup.find_all("img", class_="thumbnail")
  4. rand_photo = elements[:6][random.randint(0, 5)]['data-src']
  5. rand_photo = requests.get(rand_photo)
  6. pilImage = Image.open(BytesIO(rand_photo.content))
  7. pilImage = pilImage.resize((250, 250), Image.ANTIALIAS)
  8. await client.user.edit(password=str(data.get()['Settings']['password']), avatar=pilImage.tobytes())

错误输出

  1. 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.
  2. pilImage = pilImage.resize((250, 250), Image.ANTIALIAS).convert("RGBA")
  3. Ignoring exception in on_ready
  4. Traceback (most recent call last):
  5. File "C:\Users\fatih\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 343, in _run_event
  6. await coro(*args, **kwargs)
  7. File "C:\Users\fatih\OneDrive\Masaüstü\Python\discord reklam-project\main.py", line 1205, in on_ready
  8. await client.user.edit(password=str(data.get()['Settings']['password']), avatar=pilImage.tobytes())
  9. File "C:\Users\fatih\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\user.py", line 478, in edit
  10. avatar = _bytes_to_base64_data(avatar_bytes)
  11. File "C:\Users\fatih\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\utils.py", line 323, in _bytes_to_base64_data
  12. mime = _get_mime_type_for_image(data)
  13. File "C:\Users\fatih\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\utils.py", line 319, in _get_mime_type_for_image
  14. raise InvalidArgument('Unsupported image type given')
  15. discord.errors.InvalidArgument: Unsupported image type given

请注意,代码中的警告指出,ANTIALIAS已被弃用,建议使用LANCZOS或Resampling.LANCZOS。并且,出现了不支持的图像类型错误。

英文:

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:

  1. resp = requests.get('https://www.generatormix.com/random-female-celebrities', stream=True)
  2. soup = BeautifulSoup(resp.content, "html.parser")
  3. elements = soup.find_all("img", class_="thumbnail")
  4. rand_photo = elements[:6][random.randint(0, 5)]['data-src']
  5. rand_photo = requests.get(rand_photo)
  6. pilImage = Image.open(BytesIO(rand_photo.content))
  7. pilImage = pilImage.resize((250, 250), Image.ANTIALIAS)
  8. await client.user.edit(password=str(data.get()['Settings']['password']), avatar=pilImage.tobytes())

Error output:

  1. 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.
  2. pilImage = pilImage.resize((250, 250), Image.ANTIALIAS).convert("RGBA")
  3. Ignoring exception in on_ready
  4. Traceback (most recent call last):
  5. File "C:\Users\fatih\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 343, in _run_event
  6. await coro(*args, **kwargs)
  7. File "C:\Users\fatih\OneDrive\Masaüstü\Python\discord reklam-project\main.py", line 1205, in on_ready
  8. await client.user.edit(password=str(data.get()['Settings']['password']), avatar=pilImage.tobytes())
  9. File "C:\Users\fatih\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\user.py", line 478, in edit
  10. avatar = _bytes_to_base64_data(avatar_bytes)
  11. File "C:\Users\fatih\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\utils.py", line 323, in _bytes_to_base64_data
  12. mime = _get_mime_type_for_image(data)
  13. File "C:\Users\fatih\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\utils.py", line 319, in _get_mime_type_for_image
  14. raise InvalidArgument('Unsupported image type given')
  15. discord.errors.InvalidArgument: Unsupported image type given

答案1

得分: 0

这应该解决了你的问题:

  1. # ...
  2. pil_image = pil_image.resize((250, 250), Image.ANTIALIAS)
  3. bytes_image = BytesIO()
  4. pil_image.save(bytes_image, format='JPEG')
  5. await client.user.edit(
  6. password=str(data.get()['Settings']['password']),
  7. avatar=bytes_image.getvalue()
  8. )
英文:

This should solve your problem:

  1. # ...
  2. pil_image = pil_image.resize((250, 250), Image.ANTIALIAS)
  3. bytes_image = BytesIO()
  4. pil_image.save(bytes_image, format='JPEG')
  5. await client.user.edit(
  6. password=str(data.get()['Settings']['password']),
  7. avatar=bytes_image.getvalue()
  8. )

huangapple
  • 本文由 发表于 2023年6月1日 04:19:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定