Python图像处理16位灰度png图像
我正在编写一个脚本来检查图像是否标准化。我正在使用 Python PNG 模块来分析图像。为了测试它,我在 Photoshop 中创建了一个由 2 像素线和黑白像素组成的 16 位图像。 我的脚本正确识别了黑色像素 (0),但它给出的白色像素值 (65533) 与我预期的值 (65535) 不同。
我不明白为什么会发生这种情况。我的脚本有问题还是与 Photoshop 保存图像的方式有关?
简约测试 png 图片: https://i.sstatic.net/UgfhF.png
脚本:
#!/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"
I'm writing a script to check if an image is normalized or not. I'm using Python PNG module to analyze the image. To test it I created a 16-bit image consisting of a 2 pixels line with a black and white pixel in Photoshop.
My script correctly identifies the black pixel (0), but it gives a different value (65533) than I was expecting (65535) for the white pixel.
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?
Minimalistic test png image: https://i.sstatic.net/UgfhF.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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来 PNG 文件确实为白色像素存储了 65533 值,而不是 65535。我认为这与 Photoshop 实际上在“16 位模式”下使用 15 位这一事实有关,因此有一个小的保存 16 位灰度图像时不准确。
It seems the PNG file really has the 65533 value stored for the white pixel instead of 65535. I assume this has something to do with the fact that Photoshop in reality works with 15 bits in the "16 bit mode", so there is a small inaccuracy when saving 16 bit greyscale images.