首页 > 解决方案 > 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,则相同。图像是空白的。

标签: pythonpython-3.ximagepython-requestspython-imaging-library

解决方案


您无法直接转换它,因为图像的字节*.thumb不是*.jpgor *.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")

推荐阅读