用Python在PIL中读取PNG

时间:2015-11-09 21:13:33

标签: python png jpeg python-imaging-library

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

  img = Image.open(path)
  pixels = img.load()

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

1 个答案:

答案 0 :(得分:6)

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

def fileType(): 
f = pickAFile 
print f f[f.rfind('.'):] 
  if f[f.rfind('.'):] == '.wav': 
    print "A sound file was selected." 
  if f[f.rfind('.'):] == '.jpg' : 
    print "An image file was selected 
  else: 
    print " This was an incorrect file type." 
    print " Please chose a .wav or .jpg file."