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

Python实现Photoshop中获取图像Bits per channel的方法及所需模块

Python中获取图像每通道位深度的实现方式

在Python中,常用Pillow(PIL的分支)或OpenCV模块读取图像并获取每通道位深度,以下是具体实现:

方法一:使用Pillow模块

  1. 安装模块:
pip install pillow
  1. 实现代码:
from PIL import Image

with Image.open("你的图像路径.jpg") as img:
    # 获取每通道位深度,返回值为8、16等(注:24是RGB图像总位深度,非单通道)
    bits = img.bits

方法二:使用OpenCV模块

  1. 安装模块:
pip install opencv-python
  1. 实现代码:
import cv2

img = cv2.imread("你的图像路径.jpg", cv2.IMREAD_UNCHANGED)
# 根据图像数据类型映射每通道位深度
depth_mapping = {
    'uint8': 8,
    'uint16': 16,
    'float32': 32  # 适配浮点型图像场景
}
bits = depth_mapping[str(img.dtype)]

补充说明

若你提到的24是指RGB图像的总位深度(3×8),可通过以下方式计算:

  • Pillow:total_bits = img.bits * len(img.getbands())
  • OpenCV(针对3通道图像):total_bits = bits * img.shape[2]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 20:55:18