Files
sci-gui-agent-benchmark/desktop_env/evaluators/metrics/gimp.py
2024-01-13 01:49:46 +08:00

119 lines
4.1 KiB
Python

import os
from PIL import Image, ImageChops, ImageStat
def get_gimp_export_path():
# Path to GIMP's configuration file. This example assumes GIMP version 2.10.
# You need to adjust the path according to the GIMP version and user's file system.
gimp_config_file = os.path.expanduser("~/.config/GIMP/2.10/gimprc")
try:
# Open and read the configuration file
with open(gimp_config_file, 'r') as file:
for line in file:
# Search for the default export path setting
if "default-export-path" in line:
# Extract the current path from the line (assuming it's enclosed in quotes)
current_path = line.split('"')[1]
# Compare the current path with the expected path
return current_path
except FileNotFoundError:
# Handle the case where the configuration file is not found
print("GIMP configuration file not found")
return False
def check_file_exists(directory, filename):
file_path = os.path.join(directory, filename)
return 1 if os.path.isfile(file_path) else 0
def increase_saturation(image1_path: str, image2_path: str) -> float:
def calculate_saturation(image):
# convert the image to HSV mode
hsv_image = image.convert("HSV")
saturation_channel = hsv_image.split()[1]
# calculate the mean saturation level
stat = ImageStat.Stat(saturation_channel)
mean_saturation = stat.mean[0]
return mean_saturation
image1 = Image.open(image1_path)
image2 = Image.open(image2_path)
# calculate the saturation level of each image
saturation1 = calculate_saturation(image1)
saturation2 = calculate_saturation(image2)
return 1 if saturation1 < saturation2 else 0
def decrease_brightness(image1_path: str, image2_path: str) -> float:
def calculate_brightness(image):
# Convert the image to grayscale mode
grayscale_image = image.convert("L")
# Get the image data
pixels = list(grayscale_image.getdata())
brightness = sum(pixels) / len(pixels)
return brightness
image1 = Image.open(image1_path)
image2 = Image.open(image2_path)
brightness1 = calculate_brightness(image1)
brightness2 = calculate_brightness(image2)
return 1 if brightness1 > brightness2 else 0
import cv2
import numpy as np
def find_yellow_triangle(image):
# Convert the image to RGBA
rgba = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA)
# define range of yellow color in HSV
lower_yellow = np.array([0, 0, 0], dtype=np.uint8)
upper_yellow = np.array([255, 255, 255], dtype=np.uint8)
# expand the dimensions of lower and upper yellow to match the image dimensions
lower_yellow = np.reshape(lower_yellow, (1, 1, 3))
upper_yellow = np.reshape(upper_yellow, (1, 1, 3))
# build a mask for the yellow color
mask = cv2.inRange(rgba, lower_yellow, upper_yellow)
# search for contours in the mask
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# choose the largest contour
max_contour = max(contours, key=cv2.contourArea)
# calculate the center of the contour
M = cv2.moments(max_contour)
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
return cx, cy
def compare_triangle_positions(image1, image2):
image1 = cv2.imread(image1, cv2.IMREAD_COLOR)
image2 = cv2.imread(image2, cv2.IMREAD_COLOR)
# find the center of the yellow triangle in each image
cx1, cy1 = find_yellow_triangle(image1)
cx2, cy2 = find_yellow_triangle(image2)
# calculate the distance between the center of the triangle and the center of the image
center_distance1 = np.sqrt((cx1 - image1.shape[1] // 2)**2 + (cy1 - image1.shape[0] // 2)**2)
center_distance2 = np.sqrt((cx2 - image2.shape[1] // 2)**2 + (cy2 - image2.shape[0] // 2)**2)
return 1 if center_distance1 > center_distance2 else 0
if __name__ == "__main__":
image1_path = "../Downloads/1.png"
image2_path = "../Downloads/edited_darker.png"
decrease_brightness(image1_path, image2_path)
increase_saturation(image1_path, image2_path)