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

Node.js image-size模块返回的图像尺寸单位咨询

image-size模块的尺寸单位及差异说明

单位确认

image-size模块返回的width和height的单位是像素(px),和系统文件夹显示的像素单位完全一致。

尺寸不符的核心原因

你遇到的输出与系统显示差异,几乎都是因为图片包含EXIF方向元数据:

  • 系统文件管理器会自动识别EXIF旋转参数,显示旋转后的视觉尺寸
  • image-size默认返回的是图片原始的像素尺寸(未应用旋转修正)

验证与修正代码

可以通过读取模块返回的orientation字段,计算出和系统一致的显示尺寸:

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

const imgInfo = sizeOf("photo.jpg");
console.log("原始像素尺寸:", `${imgInfo.width}px × ${imgInfo.height}px`);
console.log("EXIF方向标记:", imgInfo.orientation);

// 根据EXIF方向修正显示尺寸
let displayWidth = imgInfo.width;
let displayHeight = imgInfo.height;
// 方向5-8是需要交换宽高的旋转场景
if ([5, 6, 7, 8].includes(imgInfo.orientation)) {
  [displayWidth, displayHeight] = [displayHeight, displayWidth];
}
console.log("实际显示尺寸:", `${displayWidth}px × ${displayHeight}px`);

另外补充你的代码细节:async函数内没必要用同步的sizeOf,异步读取可以用模块提供的async方法:

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

(async () => {
  const dimensions = await sizeOf.async("photo.jpg");
  console.log("width: ", dimensions.width,"height: ", dimensions.height);
})();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 07:15:34