2
votes

I am making an online image editor, and I am implementing a Hue/Sat/Lum editor. Here is my function to change the image with the given values.

def make_edit(pixels, hue, sat, lum):
    shape = pixels.shape
    new = np.empty(shape)
    print(time.time())
    for row_count, row in enumerate(pixels):
        for pixel_count, p in enumerate(row):
            new_hue = p[0] + hue
            if new_hue < 0:
                new_hue += 255
            elif new_hue > 255:
                new_hue -= 255

            new_sat = p[1] + sat
            if new_sat < 0:
                new_sat += 255
            elif new_sat > 255:
                new_sat -= 255

            new_lum = p[2] + lum
            if new_lum < 0:
                new_lum = 0
            elif new_lum > 255:
                new_lum = 255

            new[row_count, pixel_count] = np.array([new_hue, new_sat, new_lum])
    print(time.time())
    return new

The function takes a numpy array in shape (height, width, 3). I am doing it pixel by pixel, then adding the hue, sat and lum value to each pixel. It takes 13 seconds (on a (648, 1152, 3) shaped array) however, obviously too long. Is there a numpy function that could offset all values by the amount I give it. p.s. the function doesn't work yet, hue seems to, but sat and lum don't give the correct images.

2

2 Answers

1
votes

Since you have not got sat and lum working correctly yet this will probably need to be adjusted to whatever your final code looks like, however it does match the results of your current process and does it orders of magnitude faster:

def getPic():   
    return  np.random.randint(0, 255, 648*1152*3).reshape(648, 1152, 3)

def make_edit(pixels, hue, sat, lum):
    shape = pixels.shape
    new = np.empty(shape)
    #print(time.time())
    for row_count, row in enumerate(pixels):
        for pixel_count, p in enumerate(row):
            new_hue = p[0] + hue
            if new_hue < 0:
                new_hue += 255
            elif new_hue > 255:
                new_hue -= 255

            new_sat = p[1] + sat
            if new_sat < 0:
                new_sat += 255
            elif new_sat > 255:
                new_sat -= 255

            new_lum = p[2] + lum
            if new_lum < 0:
                new_lum = 0
            elif new_lum > 255:
                new_lum = 255

            new[row_count, pixel_count] = np.array([new_hue, new_sat, new_lum])
    #print(time.time())
    return new


def new_make_edit(pixels, hue, sat, lum):
    new = np.empty_like(pixels)
    new[:,:,0] = pixels[:,:,0] + hue
    new[:,:,0][new[:,:,0]<0] += 255
    new[:,:,0][new[:,:,0]>255] -= 255

    new[:,:,1] = pixels[:,:,1] + sat
    new[:,:,1][new[:,:,1]<0] += 255
    new[:,:,1][new[:,:,1]>255] -= 255

    new[:,:,2] = pixels[:,:,2] + lum
    new[:,:,2][new[:,:,2]<0] = 0
    new[:,:,2][new[:,:,2]>255] = 255
    return new

def tEd():
    pic = getPic()
    old = make_edit(pic, 10, -25, 211)
    new = new_make_edit(pic, 10, -25, 211)
    return old, new

def timeOld():
    pic = getPic()
    old = make_edit(pic, 10, -25, 211)
    return old

def timeNew():
    pic = getPic()
    new = new_make_edit(pic, 10, -25, 211)
    return new

Execute old and new on same image and verify output matches:

>>> o,n=tEd()
>>> np.all(o==n)
True

Performance comparison:

>>> timeit.timeit(timeNew, number=10)
0.5608299169980455
>>> timeit.timeit(timeOld, number=10)
58.86368254100671
1
votes

I don't think your algorithm, or the accepted answer is correct - you will see this if you create the pixels array as uint8 (rather than int64 as it currently is) which is presumably what you have if you are limiting outputs to the range 0..255.

You need to treat the Hue differently from Saturation and Lightness. The Hue is circular, which means it "wraps around" a circle of 0..255 "degrees". That means when it gets to 255 and you add 1, you should get back to zero and go around again. Mathematically, that means a modulus %. The Saturation and Lightness are not circular, that means if an image almost fully bright, say 250, if you add 100 to the brightness it should "burn out" at the maximum. Mathematically, this is "clipping". Likewise with Saturation.

So, I believe you want something more like this:

#!/usr/bin/env python3

import numpy as np

def make_edit(im, hue, sat, lum):
    # Make signed and larger to accommodate wrap-around
    im = im.astype(np.int32)

    # Add constant amount of hue to each pixel, wrapping around at 255
    im[:,:,0] = (im[:,:,0] + hue) % 256 

    # Add constant amount of saturation, and lightness to each pixel
    im[:,:,1] += sat
    im[:,:,2] += lum

    # Clip results to range 0..255 and return as uint8
    return np.clip(im,0,255).astype(np.uint8)

# Make our randomness deterministic!
np.random.seed(42)

# Create 4x2 array of HSL pixels - note UINT8
im = np.random.randint(0,255,(2,4,3),dtype=np.uint8) 

# array([[[102, 220, 225],
#        [ 95, 179,  61],
#        [234, 203,  92],
#        [  3,  98, 243]],
#
#       [[ 14, 149, 245],
#        [ 46, 106, 244],
#        [ 99, 187,  71],
#        [212, 153, 199]]], dtype=uint8)

res = make_edit(im, 100, 50, 20)
print(res)

#[[[202 255 245]
#  [195 229  81]
#  [ 78 253 112]
#  [103 148 255]]
#
# [[114 199 255]
#  [146 156 255]
#  [199 237  91]
#  [ 56 203 219]]]

res = make_edit(im, -100, -50, -20)
print(res)

#[[[  2 170 205]
#  [251 129  41]
#  [134 153  72]
#  [159  48 223]]
#
# [[170  99 225]
#  [202  56 224]
#  [255 137  51]
#  [112 103 179]]]

Note that you can use ImageMagick if you want to test your code just by running commands in Terminal. You would use the -modulate operator like this:

magick INPUTIMAGE -modulate BRIGHTNESS,SATURATION,HUE OUTPUTIMAGE

For example, to halve the brightness:

magick input.png -modulate 50 result.jpg

To leave the brightness unchanged at 100% of its previous value, increase the saturation by 20% and rotate the Hue 90 degrees anti-clockwise (because 50% of 180 is 90):

magic input.png -modulate 100,120,50 result.jpg

Keywords: Python, image processing, hue rotation, HSL, HSV, modulate.