From 96e82b8d58dfb43860756c82e25eeb09955746ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TED=20A=2E=20=E2=AD=95?= Date: Sun, 12 Nov 2023 21:49:32 +0000 Subject: [PATCH] Upload files to "image/script" --- image/script/generate_rows.py | 73 +++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 image/script/generate_rows.py diff --git a/image/script/generate_rows.py b/image/script/generate_rows.py new file mode 100644 index 0000000..6b8c962 --- /dev/null +++ b/image/script/generate_rows.py @@ -0,0 +1,73 @@ +# gpt + +import os +from PIL import Image, ImageOps, ImageEnhance + +def combine_images(images): + """Combine multiple Image objects horizontally.""" + widths, heights = zip(*(i.size for i in images)) + + total_width = sum(widths) + max_height = max(heights) + + new_im = Image.new('RGB', (total_width, max_height)) + + x_offset = 0 + for im in images: + new_im.paste(im, (x_offset, 0)) + x_offset += im.size[0] + + return new_im + +def find_image_path(base_path, file_name): + """Find the image path, checking for both PNG and JPEG extensions.""" + for ext in ['.png', '.jpg', '.jpeg']: + path = os.path.join(base_path, file_name + ext) + if os.path.exists(path): + return path + return None + +def process_and_colorize_image(image_path, red, green, blue): + """Process and colorize the image.""" + image = Image.open(image_path).convert("L") # Convert to grayscale + + # Colorize the image + colormap = ImageOps.colorize(image, black=(0, 0, 0), white=(red, green, blue)) + + # Enhance brightness + enhancer = ImageEnhance.Brightness(colormap) + colormap = enhancer.enhance(1.5) + + return colormap + +def process_file(file_path, images_directory): + images_by_row = {} + + with open(file_path, 'r') as file: + for line in file: + parts = line.split() + if len(parts) < 6: + continue + + file_name, x, y, red, green, blue = parts[0], int(parts[1]), int(parts[2]), int(parts[3]), int(parts[4]), int(parts[5]) + image_path = find_image_path(images_directory, file_name) + + if image_path is None: + print(f"Image not found: {file_name}") + continue + + processed_image = process_and_colorize_image(image_path, red, green, blue) + + if y not in images_by_row: + images_by_row[y] = [] + + images_by_row[y].append(processed_image) + + for y, images in images_by_row.items(): + combined_image = combine_images(images) + output_filename = f"image_row_{y}.png" + combined_image.save("../rows/" + output_filename) + print(f"Saved {output_filename}") + +# Replace 'your_file.txt' with the path to your text file +process_file('../image.txt', '../original')