File size: 3,927 Bytes
c442eda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{
 "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
}