英文:
Discord.py avatar tobytes raise error image
问题
以下是您提供的代码的翻译部分:
我的代码:
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())
错误输出:
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
请注意,代码中的警告指出,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:
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
答案1
得分: 0
这应该解决了你的问题:
# ...
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()
)
英文:
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()
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论