Skip to content Skip to sidebar Skip to footer

Convert Greenish To Redish

I have images of the stanrd 52 game cards. Some of them are black and some are red. A neural network has been trained on it to recognize them correctly. Now it turns out that somet

Solution 1:

The easiest way to do this, IMHO, is to split the image into its constituent R, G and B channels and then recombine them in the "wrong" order:

#!/usr/bin/env python3from PIL import Image

# Open image
im = Image.open('cards.jpg')

# Split into component channels
R, G, B = im.split()

# Recombine, but swapping order of red and green
result = Image.merge('RGB',[G,R,B])

# Save result   
result.save('result.jpg')

enter image description here


Alternatively, you can do the same thing via a colour matrix multiplication:

#!/usr/bin/env python3

from PIL import Image

# Open image
im = Image.open('cards.jpg')

# Define color matrix to swap the green and red channels
# This says:
# New red   =0*old red +1*old green +0*old blue +0offset
# New green =1*old red +0*old green +0*old blue +0offset
# New blue  =1*old red +0*old green +1*old blue +0offset
Matrix = ( 0, 1, 0, 0,
           1, 0, 0, 0,
           0, 0, 1, 0)

# Apply matrix
result= im.convert("RGB", Matrix)
result.save('result.jpg')

Alternatively, you could use ImageMagick in Terminal to separate the image into its constituent R,G and B channels, swap the Red and Green channels and recombine like this:

magick cards.jpg -separate -swap 0,1 -combine result.png

Alternatively, you could use ImageMagick to perform a "hue rotation". Basically, you convert the image to HSL colourspace and rotate the hue leaving the saturation and lightness unaffected. That gives you the flexibility to make almost any colour you desire. You can do that in the Terminal with:

magick cards.jpg -modulate 100,100,200 result.jpg

where the 200 above is the interesting parameter - see documentation here. Here is an animation of the various possibilities:

enter image description here

If still using v6 ImageMagick, replace magick with convert in all commands.

Keywords: Python, image processing, hue rotation, channel swap, cards, prime, swap channels, PIL, Pillow, ImageMagick.

Solution 2:

Here's one approach using a tolerance value to decide on the -ish factor to set a mathematical notation to it -

defset_image(a, tol=100): #tol - tolerance to decides on the "-ish" factor# define colors to be worked upon
    colors = np.array([[255,0,0],[0,255,0],[0,0,0],[255,255,255]])

    # Mask of all elements that are closest to one of the colors
    mask0 = np.isclose(a, colors[:,None,None,:], atol=tol).all(-1)

    # Select the valid elements for edit. Sets all nearish colors to exact ones
    out = np.where(mask0.any(0)[...,None], colors[mask0.argmax(0)], a)

    # Finally set all green to red
    out[(out == colors[1]).all(-1)] = colors[0]
    return out.astype(np.uint8)

A more memory-efficient approach would be to loop through those selective colors, like so -

def set_image_v2(a, tol=100): #tol - tolerance to decides on the "-ish" factor

    # define colors to be worked upon
    colors = np.array([[255,0,0],[0,255,0],[0,0,0],[255,255,255]])

    out= a.copy()
    for c in colors:
        out[np.isclose(out, c, atol=tol).all(-1)] = c

    # Finally setall green to red
    out[(out== colors[1]).all(-1)] = colors[0]
    returnout

Sample run -

Input image :

enter image description here

from PIL import Image
img = Image.open('green.png').convert('RGB')
x = np.array(img)
y = set_image(x) 
z = Image.fromarray(y, 'RGB')
z.save("tmp.png")

Output -

enter image description here

Post a Comment for "Convert Greenish To Redish"