简体   繁体   中英

Faster way to loop through the pixel of a frame in Python?

Trying to find the HSL per pixel value from a frame of input video but it's taking too much time about 0.2s but want to reduce the time to at least within 0.1s.

def colorize(im, h, s, l_adjust):
    result = Image.new('RGBA', im.size)
    pixin = im.load()
    pixout = result.load()
    for y in range(im.size[1]):
        for x in range(im.size[0]):
            currentR, currentG, currentB = pixin[x, y][0]/255 , pixin[x, y][1]/255, pixin[x, y][2]/255
            #luminance
            lum = (currentR * 0.2126) + (currentG * 0.7152) + (currentB * 0.0722)
            if l_adjust > 0:
                lum = lum * (1 - l_adjust)
                lum = lum + (1.0 - (1.0 - l_adjust))
            else:
                lum = lum * (l_adjust + 1)
            l = lum
            r, g, b = colorsys.hls_to_rgb(h, l, s)
            r, g, b = int(r * 255.99), int(g * 255.99), int(b * 255.99)
            pixout[x, y] = (r, g, b, 255)
    return result

Is there any faster way to do this without using these 2 loops? Looking for something like LUT(Look up table) or something with NumPy shortcut to avoid those 2 loops. Thanks

使用内置的cv.cvtColor(im, cv2.COLOR_RGB2HLS) (或CUDA 加速等效项),因为它将用 C++ 编码并为您的处理器进行优化。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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