简体   繁体   English

检测图像中的所有像素是否为零/黑色(Maya Python)

[英]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 ( bytesbytearray ),您可以循环遍历它的值,而不是让事情变得比它们应该的更复杂:图像是“字节集合”,因此,将这些 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:假设是:

  • 8-bit grayscale images always use a single byte for each pixel; 8 位灰度图像始终为每个像素使用一个字节;
  • if the pixel is black, the value of the byte is 0;如果像素为黑色,则该字节的值为 0;

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)

Update更新

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.

相关问题 检测图像中的所有像素是否具有相同的颜色/值(Maya Python) - Detect if all pixels in an image are the same color/value (Maya Python) 将图像中的所有白色像素转换为黑色像素 - Convert all white pixels in the image into black pixels 使用 python 查找图像中黑色/灰色像素的所有坐标 - Find all coordinates of black / grey pixels in image using python 试图找出图像中黑色像素的百分比(Python 2) - Trying to find the percentage of black pixels in an image (Python 2) 使用 OpenCV 在 Python 中计算图像中的黑色像素数 - count number of black pixels in an image in Python with OpenCV Python:从要提取的文本为黑色的图像中去除黑色像素 - Python: removing black pixels from an image where the text to be extracted is black 使用OpenCV Python,如何使所有黑色像素透明,然后将其覆盖在原始图像上 - Using OpenCV Python, How would you make all black pixels transparent, and then overlay it over original image 如何循环所有图像像素并判断它们是黑色还是白色 - How to loop all image pixels and tell whether they are black or white RGBA 图像中所有不透明/黑色像素的 PIL 平均值 - PIL mean of all non-transparent/black pixels in RGBA image 包含图像所有黑色像素的最小角度矩形 - Minimal angled rectangle enclosing all black pixels of an image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM