How to attribute an image in a variable in unix inside a python script

51 views Asked by At

I have this code:

os.system('convert xc:red xc:black xc:white +append swatch.png')
os.system('convert red_1.jpg +dither -remap swatch.png start.png')

In first line I create a saved colored Image like this:

But I would like just to attribute this image a variable, without open this image again, do this directly. Something like this, in a simple way:

os.system('convert xc:red xc:black xc:white +append my_image')

In second line, I need to read the images, I would like to atribute the variables directly too, like this:

os.system('convert input_image_1 +dither -remap input_image_2 output_image')

I am doing several processes of an image, and I would not like to need to save an image and then open it again, I would like to do the process directly, is it possible?

1

There are 1 answers

0
Greg Rov On

I used a python code as suggested:

import numpy as np
import cv2

# Load image
img = cv2.imread('marmoreio_red_1.jpg')


# Define standard colors
colors = [(0, 0, 0), (255, 255, 255), (0, 0, 255)]  # black, white, red

# Define color mapping function as a lambda function
map_colors = lambda pixel: colors[np.argmin([np.linalg.norm(np.array(pixel) - np.array(color)) for color in colors])]

# Map the colors in the image using the color mapping function
for i in range(img.shape[0]):
    for j in range(img.shape[1]):
        img[i, j] = map_colors(img[i, j])

# Display and save the resulting image
cv2.imshow("Standardized Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.imwrite("standardized_image.png", img)