使用skimage读取图像后如何获取文件大小、色彩模式、位深度等信息
图像多参数获取实现方案
如果尚未安装Pillow库,先执行pip install pillow完成安装。你可以结合os模块和Pillow库补齐剩余参数的获取,完整代码如下:
import os from PIL import Image from skimage.io import imread file_path = 'abc.png' # 获取文件大小(单位:字节) file_size = os.path.getsize(file_path) print(f"文件大小(字节): {file_size}") # 已有实现:获取像素尺寸 im_sk = imread(file_path) print(f"像素尺寸:高度={im_sk.shape[0]},宽度={im_sk.shape[1]},总像素数={im_sk.size}") # 用PIL读取图像获取其余元数据 im_pil = Image.open(file_path) # 获取颜色模式 color_mode = im_pil.mode print(f"颜色模式: {color_mode}") # 获取每像素位数 bits_per_pixel = im_pil.bits print(f"每像素位数: {bits_per_pixel}")
各参数获取逻辑说明:
- 文件大小:通过
os.path.getsize()直接读取文件系统的属性返回字节数,不需要加载图像内容,执行效率更高。 - 颜色模式:Pillow的
Image.mode属性直接返回标准模式标识,常见返回值包括L(灰度图)、RGB、RGBA(带透明通道的RGB)、CMYK等。 - 每像素位数:Pillow内置
Image.bits属性直接返回对应数值,适配所有支持的图像格式,无需手动计算映射。
内容的提问来源于stack exchange,提问作者user15761564
相关产品推荐
相关产品推荐

