python - Python:如何获取、处理、转换带有“.thumb”扩展名的图像 url?
问题描述
我可以使用什么来读取、处理、转换、保存等带有“.thumb”扩展名的图像 url?
我很少看到带有“.thumb”扩展名的图片网址,所以不确定要使用哪个工具。
我尝试使用 Requests 从 URL 获取图像并保存为“.thumb”图像格式。但是当我打开图像时,它是空白的。
import requests
import shutil
image_url = 'https://slickdeals.net/attachment/5/4/8/0/7/1/200x200/9242327.thumb'
resp = requests.get(image_url, stream = True)
local_file = open('local_image.thumb', 'wb')
resp.raw.decode_content = True
shutil.copyfileobj(resp.raw, local_file)
del resp
如果我将其保存为 .jpg 或 .png,则相同。图像是空白的。
解决方案
您无法直接转换它,因为图像的字节*.thumb
不是*.jpg
or *.png
。
并且不需要stream=True
在您的代码中使用。图像不是很大。这段代码可以直接下载:
import requests
image_url = 'https://slickdeals.net/attachment/5/4/8/0/7/1/200x200/9242327.thumb'
resp = requests.get(image_url)
with open('local_image.thumb', 'wb') as f:
f.write(resp.content)
您还可以使用PIL
将其转换为jpg
,例如:
import requests, io
from PIL import Image
image_url = 'https://slickdeals.net/attachment/5/4/8/0/7/1/200x200/9242327.thumb'
resp = requests.get(image_url)
image = Image.open(io.BytesIO(resp.content))
image.save("local_image.jpg")
推荐阅读
- asp.net - 如何在asp.net的ascx中设置表格单元格数据?
- powershell - 在 Docker 容器中创建一对虚拟串行端口
- flutter - 无法在颤动的平面按钮中使用 Provider 将数据添加到列表
- php - 如何将所有行从表传递到刀片视图?
- python - 循环单击 Python 和 Selenium 上具有相同 CSS_SELECTOR 的所有链接
- python - 从 python 文件调用 FloatLayout 类在我的 kivy 文件中不起作用
- java - 容器,片段旋转
- python - 如何在 Python 中收听 Windows 10 通知?
- python - 如何按 2 列一一对多维数组进行排序 - 全部以相同的顺序
- python - Discord Bot 使用 Python 使用语音聊天来静音人们的声音