red to blue:
from PIL import Image
import colorsys
def rgb_to_hsv(r, g, b):
# Convert RGB values to HSV
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
return h, s, v
def hsv_to_rgb(h, s, v):
# Convert HSV values to RGB
r, g, b = colorsys.hsv_to_rgb(h, s, v)
return int(r * 255), int(g * 255), int(b * 255)
def change_red_to_blue(image_path, output_path):
# Open the image
img = Image.open(image_path)
# Convert the image to RGB mode (if it's not already)
img = img.convert("RGB")
# Get the width and height of the image
width, height = img.size
# Iterate through each pixel in the image
for x in range(width):
for y in range(height):
# Get the RGB values of the pixel
r, g, b = img.getpixel((x, y))
# Convert RGB to HSV
h, s, v = rgb_to_hsv(r, g, b)
# Check if the pixel is red (adjust the threshold as needed)
if (h < 0.05 or h > 0.95) and s > 0.5 and v > 0.5:
# Change the pixel color to blue
img.putpixel((x, y), (0, 0, 255))
# Save the modified image
img.save(output_path)
# Close the image file
img.close()
if __name__ == "__main__":
# Specify the input and output file paths
input_image_path = "prima_up_blue_down_red.png"
output_image_path = "blue_version.png"
try:
# Change red to blue
change_red_to_blue(input_image_path, output_image_path)
print("Output image saved successfully.")
except Exception as e:
print("An error occurred while saving the output image:", e)
green to red:
from PIL import Image
import colorsys
def rgb_to_hsv(r, g, b):
# Convert RGB values to HSV
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
return h, s, v
def hsv_to_rgb(h, s, v):
# Convert HSV values to RGB
r, g, b = colorsys.hsv_to_rgb(h, s, v)
return int(r * 255), int(g * 255), int(b * 255)
def change_red_to_blue(image_path, output_path):
# Open the image
img = Image.open(image_path)
# Convert the image to RGB mode (if it's not already)
img = img.convert("RGB")
# Get the width and height of the image
width, height = img.size
# Iterate through each pixel in the image
for x in range(width):
for y in range(height):
# Get the RGB values of the pixel
r, g, b = img.getpixel((x, y))
# Convert RGB to HSV
h, s, v = rgb_to_hsv(r, g, b)
# Check if the pixel is green (adjust the threshold as needed)
if (h > 0.2 and h < 0.4) and (s > 0.3 and s < 0.7) and (v > 0.3 and v < 0.7):
# Change the pixel color to red
img.putpixel((x, y), (255, 0, 0))
# Save the modified image
img.save(output_path)
# Close the image file
img.close()
if __name__ == "__main__":
# Specify the input and output file paths
input_image_path = "red_to_blue_version.png"
output_image_path = "green_to_red_version.png"
try:
# Change red to blue
change_red_to_blue(input_image_path, output_image_path)
print("Output image saved successfully.")
except Exception as e:
print("An error occurred while saving the output image:", e)