用Python在PIL中读取PNG

dul*_*lla 3 python png jpeg python-imaging-library

我正在用Python阅读PNG文件.我想要图像中每个像素的RGB值:

  img = Image.open(path)
  pixels = img.load()
Run Code Online (Sandbox Code Playgroud)

对于JPEG文件,像素是元组,但对于PNG,我得到一个整数.我应该如何使用Python读取PNG图像以获取像素值?

Kev*_*vin 7

听起来像是以灰度模式打开图像.尝试在访问像素值之前转换为RGB.

img = Image.open(path).convert("RGB")
pixels = img.load()
Run Code Online (Sandbox Code Playgroud)