{ "cells": [ { "cell_type": "code", "execution_count": 7, "id": "4aa04654", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 1, "id": "098e115f", "metadata": {}, "outputs": [], "source": [ "import glob\n", "import os\n", "import json\n", "from PIL import Image\n", "from sd_parsers import ParserManager\n", "\n", "# Directory with PNG images\n", "image_directory = 'E:/image/holder/Tagset_Completer/sampleimages/02landscape'\n", "\n", "# Initialize the ParserManager\n", "parser_manager = ParserManager()\n", "\n", "# Dictionary for artist names to corresponding JPG file names\n", "artist_to_file_map = {}\n", "\n", "# Iterate through PNG files in the directory\n", "for png_file in glob.glob(os.path.join(image_directory, '*.png')):\n", " with Image.open(png_file) as img:\n", " # Extract metadata using ParserManager\n", " prompt_info = parser_manager.parse(img)\n", " if prompt_info and prompt_info.prompts:\n", " first_prompt_text = list(prompt_info.prompts)[0].value.split(',')[0].strip()\n", " if first_prompt_text.startswith(\"by \"):\n", " first_prompt_text = first_prompt_text[3:] # Remove \"by \" prefix\n", " artist_to_file_map[first_prompt_text] = os.path.basename(png_file).replace('.png', '.jpg')\n", " else:\n", " artist_to_file_map[\"\"] = os.path.basename(png_file).replace('.png', '.jpg')\n", "\n", "# Save the mapping to a JSON file in the same directory\n", "json_path = os.path.join(image_directory, 'artist_to_file_map.json')\n", "with open(json_path, 'w') as json_file:\n", " json.dump(artist_to_file_map, json_file, indent=4)\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "ac5cba7f", "metadata": {}, "outputs": [], "source": [ "# Iterate through PNG files in the directory\n", "for png_file in glob.glob(os.path.join(image_directory, '*.png')):\n", " # Open the image\n", " with Image.open(png_file) as img:\n", " # Convert the image to RGB mode in case it's RGBA or P mode\n", " img = img.convert('RGB')\n", " # Define the output filename replacing .png with .jpg\n", " jpg_file = png_file.rsplit('.', 1)[0] + '.jpg'\n", " # Save the image in JPG format\n", " img.save(jpg_file, 'JPEG')\n", " # Optionally, remove the original PNG file\n", " os.remove(png_file)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "32bfb9cc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3648a9fc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "09f74cbd", "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "d2e18c17", "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "354fda37", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "ac4e5911", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }