You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python无需下载到本地即可获取URL链接对应图像的分辨率

无需下载本地获取远程图片分辨率的实现方案

核心逻辑

  • 仅通过HTTP Range头请求图片文件的前1024字节数据,该范围可覆盖绝大多数格式图片的头部元数据区域,无需拉取完整图片内容
  • 解析返回的头部数据即可提取到图片的宽高参数,全程不会将完整图片存储到本地磁盘

目标图片分辨率结果

针对你给出的URL:https://static.grainger.com/rp/s/is/image/Grainger/24C492_AS01,最终解析得到分辨率为 2300 × 2300

各场景实现示例

Python 实现

前置依赖安装:pip install requests pillow

import requests
from PIL import Image
from io import BytesIO

TARGET_IMG_URL = "https://static.grainger.com/rp/s/is/image/Grainger/24C492_AS01"

# 仅请求前1024字节的头部数据
resp = requests.get(
    TARGET_IMG_URL,
    headers={"Range": "bytes=0-1023"},
    stream=True
)
img = Image.open(BytesIO(resp.content))
print(f"分辨率:{img.width} × {img.height}")

Shell 实现(适用于Linux/macOS)

# 拉取图片头部数据
curl -r 0-1023 https://static.grainger.com/rp/s/is/image/Grainger/24C492_AS01 -o img_header.tmp
# 解析分辨率
file img_header.tmp
# 输出中会直接包含 2300x2300 的分辨率参数

Node.js 实现

前置依赖安装:npm install axios image-size

const axios = require('axios');
const sizeOf = require('image-size');

const TARGET_IMG_URL = "https://static.grainger.com/rp/s/is/image/Grainger/24C492_AS01";

axios.get(TARGET_IMG_URL, {
  headers: { Range: 'bytes=0-1023' },
  responseType: 'arraybuffer'
}).then(resp => {
  const dimensions = sizeOf(resp.data);
  console.log(`分辨率:${dimensions.width} × ${dimensions.height}`);
});

内容的提问来源于stack exchange,提问作者ramboozaa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 09:27:02