Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -22,10 +22,20 @@ from .slides import check_presenter_console_disable, check_image_stretch_and_cen
|
||||
# from .table import check_sheet_list, check_xlsx_freeze, check_xlsx_zoom, check_data_validations
|
||||
from .table import compare_table, compare_csv
|
||||
from .thunderbird import check_thunderbird_prefs, check_thunderbird_filter
|
||||
|
||||
from .vlc import is_vlc_playing, is_vlc_recordings_folder, is_vlc_fullscreen, compare_images, compare_audios, \
|
||||
compare_videos
|
||||
from .thunderbird import check_thunderbird_prefs, check_thunderbird_filter
|
||||
from .gimp import (
|
||||
check_brightness_decrease_and_structure_sim,
|
||||
check_contrast_increase_and_structure_sim,
|
||||
check_saturation_increase_and_structure_sim,
|
||||
check_image_size_and_structure_sim,
|
||||
check_image_mirror,
|
||||
check_palette_and_structure_sim,
|
||||
check_textbox_on_leftside,
|
||||
check_green_background,
|
||||
check_file_exists_and_structure_sim,
|
||||
check_triangle_position,
|
||||
check_structure_sim,
|
||||
check_config_status,
|
||||
)
|
||||
from .vlc import is_vlc_playing, is_vlc_recordings_folder, is_vlc_fullscreen, compare_images, compare_audios, \
|
||||
compare_videos, check_qt_bgcone, check_one_instance_when_started_from_file, check_qt_minimal_view, \
|
||||
check_qt_max_volume, \
|
||||
@@ -33,4 +43,3 @@ from .vlc import is_vlc_playing, is_vlc_recordings_folder, is_vlc_fullscreen, co
|
||||
from .vscode import compare_text_file, compare_config, compare_answer, is_extension_installed, check_json_settings, \
|
||||
check_json_keybindings
|
||||
from .basic_os import check_gnome_favorite_apps, is_utc_0, check_text_enlarged, check_moved_jpgs
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import os
|
||||
from typing import List, Union
|
||||
from skimage.metrics import structural_similarity as ssim
|
||||
from PIL import Image, ImageChops, ImageStat
|
||||
|
||||
def compare_images(pred_img_path_list: Union[str, List[str]], gold_img_path_list: Union[str, List[str]]) -> float:
|
||||
|
||||
def compare_images(pred_img_path_list: Union[str, List[str]],
|
||||
gold_img_path_list: Union[str, List[str]]) -> float:
|
||||
""" Compare two image lists, only if all images are the same, return 1.0, otherwise return 0.0
|
||||
"""
|
||||
if type(pred_img_path_list) != list:
|
||||
@@ -36,11 +39,13 @@ def get_gimp_export_path():
|
||||
# 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
|
||||
@@ -63,6 +68,7 @@ def increase_saturation(image1_path: str, image2_path: str) -> float:
|
||||
|
||||
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
|
||||
@@ -81,9 +87,12 @@ def decrease_brightness(image1_path: str, image2_path: str) -> float:
|
||||
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)
|
||||
@@ -111,6 +120,7 @@ def find_yellow_triangle(image):
|
||||
|
||||
return cx, cy
|
||||
|
||||
|
||||
def compare_triangle_positions(image1, image2):
|
||||
image1 = cv2.imread(image1, cv2.IMREAD_COLOR)
|
||||
image2 = cv2.imread(image2, cv2.IMREAD_COLOR)
|
||||
@@ -119,16 +129,355 @@ def compare_triangle_positions(image1, image2):
|
||||
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)
|
||||
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
|
||||
|
||||
|
||||
# Functions for the GIMP evaluator
|
||||
def calculate_brightness(image):
|
||||
"""Calculate the average brightness of an image"""
|
||||
grayscale = image.convert('L')
|
||||
stat = ImageStat.Stat(grayscale)
|
||||
return stat.mean[0]
|
||||
|
||||
|
||||
def normalize_brightness(image, target_brightness):
|
||||
"""Normalize the brightness of an image to a target brightness in [0, 1]"""
|
||||
current_brightness = calculate_brightness(image)
|
||||
factor = target_brightness / current_brightness
|
||||
|
||||
# Apply a point transform to each pixel
|
||||
def point_transform(x):
|
||||
return min(255, max(0, int(x * factor)))
|
||||
|
||||
return image.point(point_transform)
|
||||
|
||||
|
||||
def measure_saturation(hsv_image):
|
||||
"""Measure the average saturation of an image"""
|
||||
# Split into H, S, V channels
|
||||
_, s, _ = hsv_image.split()
|
||||
# Convert the saturation channel to a numpy array
|
||||
s_array = np.array(s)
|
||||
# Calculate the average saturation
|
||||
avg_saturation = np.mean(s_array)
|
||||
return avg_saturation
|
||||
|
||||
|
||||
def calculate_contrast(image):
|
||||
"""Calculate the contrast of an image as the standard deviation of the pixel
|
||||
values."""
|
||||
pixels = np.asarray(image, dtype=np.float32)
|
||||
return np.std(pixels)
|
||||
|
||||
|
||||
def structure_check_by_mse(img1, img2, threshold=0.03):
|
||||
"""Check if two images are approximately the same by MSE"""
|
||||
mse = np.mean(
|
||||
(np.array(img1, dtype=np.float32) / 255
|
||||
- np.array(img2, dtype=np.float32) / 255) ** 2)
|
||||
structure_same = True if mse < threshold else False
|
||||
print("MSE: ", mse)
|
||||
return structure_same
|
||||
|
||||
|
||||
def structure_check_by_ssim(img1, img2, threshold=0.9):
|
||||
"""Check if two images are approximately the same by SSIM"""
|
||||
similarity = ssim(np.array(img1), np.array(img2), multichannel=True)
|
||||
print("SSIM: ", similarity)
|
||||
return similarity >= threshold
|
||||
|
||||
|
||||
def check_brightness_decrease_and_structure_sim(src_path, tgt_path):
|
||||
"""
|
||||
Check the brightness of src is lower than tgt and the structures are similar
|
||||
gimp:7a4deb26-d57d-4ea9-9a73-630f66a7b568
|
||||
"""
|
||||
img_src = Image.open(src_path)
|
||||
img_tgt = Image.open(tgt_path)
|
||||
|
||||
# Brightness comparison
|
||||
brightness_src = calculate_brightness(img_src)
|
||||
brightness_tgt = calculate_brightness(img_tgt)
|
||||
brightness_reduced = brightness_tgt > brightness_src
|
||||
|
||||
# Normalize and compare images
|
||||
target_brightness = 128
|
||||
img_src_normalized = normalize_brightness(img_src, target_brightness)
|
||||
img_tgt_normalized = normalize_brightness(img_tgt, target_brightness)
|
||||
|
||||
structure_same = structure_check_by_mse(img_src_normalized, img_tgt_normalized)
|
||||
return brightness_reduced and structure_same
|
||||
|
||||
|
||||
def check_saturation_increase_and_structure_sim(src_path, tgt_path):
|
||||
"""
|
||||
Check the saturation of src is higher than tgt and the structures are similar
|
||||
gimp:554785e9-4523-4e7a-b8e1-8016f565f56a
|
||||
"""
|
||||
img_src = Image.open(src_path)
|
||||
hsv_img_src = img_src.convert('HSV')
|
||||
img_tgt = Image.open(tgt_path)
|
||||
hsv_img_tgt = img_tgt.convert('HSV')
|
||||
|
||||
# Saturation comparison
|
||||
src_saturation = measure_saturation(hsv_img_src)
|
||||
tgt_saturation = measure_saturation(hsv_img_tgt)
|
||||
|
||||
saturation_increased = tgt_saturation < src_saturation
|
||||
|
||||
# Structure comparison
|
||||
h1, s1, v1 = hsv_img_src.split()
|
||||
h2, s2, v2 = hsv_img_tgt.split()
|
||||
h_same = structure_check_by_ssim(h1, h2)
|
||||
v_same = structure_check_by_ssim(v1, v2)
|
||||
if h_same and v_same:
|
||||
structure_same = True
|
||||
else:
|
||||
structure_same = False
|
||||
|
||||
return saturation_increased and structure_same
|
||||
|
||||
|
||||
def check_file_exists_and_structure_sim(src_path, tgt_path):
|
||||
"""
|
||||
Check if the image has been exported to the desktop
|
||||
gimp:77b8ab4d-994f-43ac-8930-8ca087d7c4b4
|
||||
"""
|
||||
# Check if the file exists
|
||||
export_file_exists = os.path.isfile(src_path)
|
||||
if not export_file_exists:
|
||||
return False
|
||||
|
||||
# Check whether the target image is the same as the source image
|
||||
img_src = Image.open(src_path)
|
||||
img_tgt = Image.open(tgt_path)
|
||||
structure_same = structure_check_by_ssim(img_src, img_tgt)
|
||||
|
||||
return structure_same
|
||||
|
||||
|
||||
def check_triangle_position(tgt_path):
|
||||
"""
|
||||
Check if the triangle is in the middle of the image.
|
||||
gimp:f4aec372-4fb0-4df5-a52b-79e0e2a5d6ce
|
||||
"""
|
||||
# Load the image
|
||||
img = Image.open(tgt_path)
|
||||
img_array = np.array(img)
|
||||
|
||||
# We will determine if the triangle is in the middle of the picture by checking the centroid
|
||||
# We assume the triangle is a different color from the background
|
||||
# Find the unique colors
|
||||
unique_colors = np.unique(img_array.reshape(-1, img_array.shape[2]), axis=0)
|
||||
|
||||
# Assuming the background is the most common color and the triangle is a different color,
|
||||
# we identify the triangle's color as the least common one
|
||||
triangle_color = unique_colors[-1]
|
||||
|
||||
# Create a mask where the triangle pixels are True
|
||||
triangle_mask = np.all(img_array == triangle_color, axis=2)
|
||||
|
||||
# Get the coordinates of the triangle pixels
|
||||
triangle_coords = np.argwhere(triangle_mask)
|
||||
|
||||
# Calculate the centroid of the triangle
|
||||
centroid = triangle_coords.mean(axis=0)
|
||||
|
||||
# Check if the centroid is approximately in the middle of the image
|
||||
image_center = np.array(img_array.shape[:2]) / 2
|
||||
|
||||
# We will consider the triangle to be in the middle if the centroid is within 5% of the image's center
|
||||
tolerance = 0.05 * np.array(img_array.shape[:2])
|
||||
middle = np.all(np.abs(centroid - image_center) < tolerance)
|
||||
|
||||
return middle
|
||||
|
||||
|
||||
def check_structure_sim(src_path, tgt_path):
|
||||
"""
|
||||
Check if the structure of the two images are similar
|
||||
gimp:2a729ded-3296-423d-aec4-7dd55ed5fbb3
|
||||
"""
|
||||
img_src = Image.open(src_path)
|
||||
img_tgt = Image.open(tgt_path)
|
||||
structure_same = structure_check_by_ssim(img_src, img_tgt)
|
||||
return structure_same
|
||||
|
||||
|
||||
def check_contrast_increase_and_structure_sim(src_path, tgt_path):
|
||||
"""
|
||||
Check if the src image has higher contrast than the tgt image and the structures are similar
|
||||
gimp:f723c744-e62c-4ae6-98d1-750d3cd7d79d
|
||||
"""
|
||||
# Load images
|
||||
source_image = Image.open(src_path)
|
||||
target_image = Image.open(tgt_path)
|
||||
|
||||
# Calculate contrast
|
||||
source_contrast = calculate_contrast(source_image)
|
||||
target_contrast = calculate_contrast(target_image)
|
||||
higher_contrast = target_contrast < source_contrast
|
||||
|
||||
# Check structure
|
||||
structure_same = structure_check_by_ssim(source_image, target_image, threshold=0.65)
|
||||
|
||||
return higher_contrast and structure_same
|
||||
|
||||
|
||||
def check_config_status(actual_config_path, rule):
|
||||
"""
|
||||
Check if the GIMP status is as expected
|
||||
"""
|
||||
with open(actual_config_path, 'r') as f:
|
||||
content = f.readlines()
|
||||
|
||||
for line in content:
|
||||
if line.startswith('#') or line == '\n':
|
||||
continue
|
||||
items = line.strip().lstrip('(').rstrip(')\n').split()
|
||||
if isinstance(rule["key"], str):
|
||||
if items[0] == rule["key"] and items[-1] == rule["value"]:
|
||||
return True
|
||||
elif isinstance(rule["key"], list) and len(rule["key"]) == 2:
|
||||
if items[0] == rule["key"][0] \
|
||||
and items[1] == rule["key"][1] \
|
||||
and items[-1] == rule["value"]:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def check_image_size_and_structure_sim(src_path, tgt_path, height=512, width=None):
|
||||
"""
|
||||
Check if the size of the src image is correct and the structure of the two images are similar.
|
||||
gimp:d16c99dc-2a1e-46f2-b350-d97c86c85c15
|
||||
"""
|
||||
# Load images
|
||||
source_image = Image.open(src_path)
|
||||
target_image = Image.open(tgt_path)
|
||||
|
||||
# Check size
|
||||
if width is not None:
|
||||
width_same = source_image.size[0] == width
|
||||
else:
|
||||
width_same = True
|
||||
if height is not None:
|
||||
height_same = source_image.size[1] == height
|
||||
else:
|
||||
height_same = True
|
||||
|
||||
# Check structure
|
||||
resized_target_image = target_image.resize(source_image.size)
|
||||
structure_same = structure_check_by_ssim(source_image, resized_target_image)
|
||||
|
||||
return width_same and height_same and structure_same
|
||||
|
||||
|
||||
def check_palette_and_structure_sim(src_path, tgt_path):
|
||||
"""
|
||||
Check if the src image is palette-based and the structure of the two images are similar
|
||||
gimp:06ca5602-62ca-47f6-ad4f-da151cde54cc
|
||||
"""
|
||||
# Check if the source image is palette-based
|
||||
source_image = Image.open(src_path)
|
||||
palette_based = source_image.mode == 'P'
|
||||
|
||||
# Check structure
|
||||
target_image = Image.open(tgt_path)
|
||||
source_image = source_image.convert('RGB')
|
||||
structure_same = structure_check_by_ssim(source_image, target_image)
|
||||
return palette_based and structure_same
|
||||
|
||||
|
||||
def check_textbox_on_leftside(src_path):
|
||||
"""
|
||||
Check if the textbox is on the left side of the image.
|
||||
gimp:e2dd0213-26db-4349-abe5-d5667bfd725c
|
||||
"""
|
||||
source_image = Image.open(src_path)
|
||||
gray_image = source_image.convert("L")
|
||||
width, height = source_image.size
|
||||
|
||||
# Find the bounds of the black text
|
||||
left_most_dark_pixel = width # Start with the farthest possible left position
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
# If the pixel is dark, consider it as part of the text
|
||||
if gray_image.getpixel((x, y)) < 128: # Arbitrary threshold for "dark"
|
||||
left_most_dark_pixel = min(left_most_dark_pixel, x)
|
||||
break # Stop after finding the first dark pixel in this row
|
||||
|
||||
# Here we define "almost" on the left side as being within the left 5% of the image
|
||||
return left_most_dark_pixel < width * 0.05
|
||||
|
||||
|
||||
def check_image_mirror(src_path, tgt_path):
|
||||
"""
|
||||
Check if the image is mirrored
|
||||
gimp:72f83cdc-bf76-4531-9a1b-eb893a13f8aa
|
||||
"""
|
||||
# Load images
|
||||
source_image = Image.open(src_path)
|
||||
target_image = Image.open(tgt_path)
|
||||
|
||||
# Check if the image is mirrored
|
||||
transposed_image = source_image.transpose(Image.FLIP_LEFT_RIGHT)
|
||||
# Use 0.99 because the image may not be exactly mirrored by gimp
|
||||
mirrored = structure_check_by_ssim(transposed_image, target_image, 0.99)
|
||||
return mirrored
|
||||
|
||||
|
||||
def check_green_background(src_path, tgt_path):
|
||||
"""
|
||||
Check if the background of the source image is green.
|
||||
gimp:734d6579-c07d-47a8-9ae2-13339795476b
|
||||
"""
|
||||
# Load images
|
||||
source_image = Image.open(src_path)
|
||||
target_image = Image.open(tgt_path)
|
||||
|
||||
source_pixels = np.array(source_image)
|
||||
target_pixels = np.array(target_image)
|
||||
|
||||
for x in range(target_image.width):
|
||||
for y in range(target_image.height):
|
||||
# Identify background pixel in target image (not black)
|
||||
if tuple(target_pixels[x, y][:3]) != (0, 0, 0):
|
||||
# Check if corresponding pixel in source image is green
|
||||
# Here, "green" means more green than red or blue
|
||||
r, g, b = source_pixels[x, y][:3]
|
||||
if not (g > r and g > b):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
image1_path = "../Downloads/1.png"
|
||||
image2_path = "../Downloads/edited_darker.png"
|
||||
actual_config_path = "../../../cache/sessionrc_test"
|
||||
rule = {
|
||||
"key": "hide-docks",
|
||||
"value": "no"
|
||||
}
|
||||
print(check_config_status(actual_config_path, rule))
|
||||
|
||||
decrease_brightness(image1_path, image2_path)
|
||||
actual_config_path = "../../../cache/action-history_test"
|
||||
rule = {
|
||||
"key": ["history-item", "\"filters-vignette\""],
|
||||
"value": "1"
|
||||
}
|
||||
print(check_config_status(actual_config_path, rule))
|
||||
|
||||
increase_saturation(image1_path, image2_path)
|
||||
actual_config_path = "../../../cache/gimprc_test"
|
||||
rule = {
|
||||
"key": "undo-levels",
|
||||
"value": "100"
|
||||
}
|
||||
print(check_config_status(actual_config_path, rule))
|
||||
|
||||
src_path = "../../../cache/734d6579-c07d-47a8-9ae2-13339795476b/green_background_with_object.png"
|
||||
tgt_path = "../../../cache/734d6579-c07d-47a8-9ae2-13339795476b/white_background_with_object.png"
|
||||
print(check_green_background(src_path, tgt_path))
|
||||
|
||||
Reference in New Issue
Block a user