簡體   English   中英

檢測圖像中的所有像素是否具有相同的顏色/值(Maya Python)

[英]Detect if all pixels in an image are the same color/value (Maya Python)

上一個問題的后續問題 此代碼使用 Pyside 檢測圖像(8 位灰度 jpg)中的所有像素是否為零/黑色:

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())

想調整它以檢查通道中的所有像素是否都是相同的值。 純紅色或純灰色,平坦恆定的顏色。

例如,在 L8 格式(8 位亮度)的凸塊 map 上,像素在紅色通道上為 50% 灰色,在綠色和藍色上為黑色,RGB[0.5, 0, 0]。 在正常的 map 上,它們將是 RGB[0.5, 0.5, 1](假設 0-1 RGB 比例)。

FWIW 這是來自物質 3D Painter 的高度/凹凸 map 紋理 Output,Pyside2 將在 Maya 內部運行。

這與您之前的問題沒有太大區別,因為我們並不真正關心圖像的表示,而只關心它的像素值:只要任何值與一個不同,我們就知道圖像具有不止一種“顏色”。

def all_pixels_identical(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
    it = iter(img.constBits())
    # for PyQt
    it = iter(img.constBits().asarray(img.sizeInBytes()))

    first = next(it)
    while True:
        try:
            if first != next(it):
                return False
        except StopIteration:
            return True

注意:我沒有機會測試代碼,所以它可能需要一些修復,但這個概念基本上是正確的。 OTOH,我建議您對光柵圖像如何在 memory 中表示為原始數據進行一些研究,以便您將來更好地理解這些問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM