[英]Detect if all pixels in an image are zero/black (Maya Python)
Newbie.新手。 Using the following code to check if a grayscale image (a mask texture in Maya) has all black pixels in the RGB channels (meaning it is empty).使用以下代码检查灰度图像(Maya 中的蒙版纹理)是否在 RGB 通道中具有所有黑色像素(意味着它是空的)。 This works, but is a bit slow on large images (2048x2048 is around 15 seconds).这可行,但在大图像上有点慢(2048x2048 大约是 15 秒)。 Looking for how I can speed this up / do this more efficiently.寻找我如何可以加快速度/更有效地做到这一点。
EDIT: This is the original code编辑:这是原始代码
def all_black_pixels(image, width, height):
img = PySide2.QtGui.QImage(width, height, PySide2.QtGui.QImage.Format.Format_Grayscale8)
img.load(image)
for y in range(height):
for x in range(width):
color = PySide2.QtGui.QColor()
color.setRgb(img.pixel(x,y))
black = False
# print ( color.getRgb()[0] )
if color.getRgb()[0] is not 0:
black = True
return black
EDIT: changing based on comments, for clarity and fixing:编辑:根据评论进行更改,以明确和修复:
import PySide2
def all_black_pixels(image):
black = True
img = PySide2.QtGui.QImage()
img.load(image)
TexSize = img.width()
for y in range(TexSize):
for x in range(TexSize):
color = PySide2.QtGui.QColor()
color.setRgb(img.pixel(x,y))
print ( color.getRgb()[0] )
if color.getRgb()[0] > 0:
black = False
break
return black
all_black_pixels('/path/to/file/fileName.jpg', 20, 20)
Assuming that image
is an iterable byte object ( bytes
or bytearray
), you can cycle through its values instead of making things more complex than they should: images are "collections of bytes", so, converting those collections to "actual" images and getting their pixel values makes very little sense.假设image
是一个可迭代的字节 object ( bytes
或bytearray
),您可以循环遍历它的值,而不是让事情变得比它们应该的更复杂:图像是“字节集合”,因此,将这些 collections 转换为“实际”图像并获得它们的像素值几乎没有意义。
Since you have to know if any of the pixels has a "non-black" color, you don't need to always iterate the whole image: yes, you have to iterate through the whole image because even the "last" pixel could be "non-black", but, as soon as any previous pixel isn't black, there's obviously no point in checking the next ones.由于您必须知道是否有任何像素具有“非黑色”颜色,因此您不需要总是迭代整个图像:是的,您必须遍历整个图像,因为即使是“最后一个”像素也可能是“非黑色”,但是,一旦任何先前的像素不是黑色的,显然就没有必要检查下一个像素。
The assumption is:假设是:
Also, knowing the size of the image is useless.此外,知道图像的大小是没有用的。
So, just call the function only using the raw data alone:因此,仅使用原始数据调用 function:
def all_black_pixels(imageData):
for pixel in imageData:
if pixel:
return False
return True
Even simpler:更简单:
all_black_pixels = lambda imageData: not any(imageData)
Since the OP has changed the question pointing out they start from an image file , the solution is similar, but it uses constBits()
, which returns an array of the image data .由于 OP 已经改变了指出它们从图像文件开始的问题,因此解决方案类似,但它使用constBits()
,它返回图像数据的数组。
Consider that this is on the assumption that the image format is Format_Grayscale8
, if it's not, it should be converted before with convertTo()
.考虑这是假设图像格式为Format_Grayscale8
,如果不是,则应先使用convertTo()
进行转换。
Also note that if you use PyQt, the returned type of constBits()
is a sip pointer, so it must be converted to an actual array that can be accessed by python.另请注意,如果使用 PyQt,则constBits()
的返回类型是 sip 指针,因此必须将其转换为 python 可以访问的实际数组。
def all_black_pixels(path):
img = QImage(path)
if img.isNull():
return False # or whatever you think appropriate
if not img.format() == img.Format_Grayscale8:
img.convertTo(img.Format_Grayscale8)
# for PySide
return not any(img.constBits())
# for PyQt
return not any(img.constBits().asarray(img.sizeInBytes()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.