简体   繁体   English

用于16位灰度png图像的Python图像处理

[英]Python image processing for 16-bit grayscale png image

I'm writing a script to check if an image is normalized or not. 我正在编写一个脚本来检查图像是否正常化。 I'm using Python PNG module to analyze the image. 我正在使用Python PNG模块来分析图像。 To test it I created a 16-bit image consisting of a 2 pixels line with a black and white pixel in Photoshop. 为了测试它,我在Photoshop中创建了一个由2像素线和黑白像素组成的16位图像。 My script correctly identifies the black pixel (0), but it gives a different value (65533) than I was expecting (65535) for the white pixel. 我的脚本正确识别黑色像素(0),但它给出了与白色像素的预期值(65535)不同的值(65533)。

I can't understand why this happens. 我不明白为什么会这样。 Is there something wrong with my script or it is related with the way Photoshop saves the image? 我的脚本有问题还是与Photoshop保存图像的方式有关?

Minimalistic test png image: http://i.imgur.com/70D0F.png 简约测试png图片: http//i.imgur.com/70D0F.png

Script: 脚本:

#!/usr/bin/python

import sys
import png # https://pypi.python.org/pypi/pypng

if len(sys.argv) != 2:
    print "Invalid number of arguments (",len(sys.argv),").\nUsage: python getMinMaxColor.py png_file"
    sys.exit(-1)
pngFilePath = sys.argv[1]

f = open(pngFilePath, 'rb')
r = png.Reader(file=f)
k = r.read()

bitDepth = 16
if k[3]['bitdepth'] != None:
    bitDepth = k[3]['bitdepth']

absMaxColor = 2**bitDepth-1

maxColor = -1
minColor = absMaxColor+1
print "Content:"
for line in k[2]:
    for color in line:
        print color
        if (color > maxColor):
            maxColor = color
        if (color < minColor):
            minColor = color

f.close()

print "\n"

print "Min Color:", minColor
print "Max Color:", maxColor, "( max:", absMaxColor, ")"
if minColor == 0 and maxColor == absMaxColor:
    print "Image is normalized"
else:
    print "Image is not normalized"

看起来PNG文件确实存储了白色像素而不是65535的65533值。我认为这与实际上Photoshop在“16位模式”中使用15位的事实有关,所以有一个小的保存16位灰度图像时出现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM