images.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. import datetime
  2. import sys
  3. import traceback
  4. import pytz
  5. import io
  6. import math
  7. import os
  8. from collections import namedtuple
  9. import re
  10. import numpy as np
  11. import piexif
  12. import piexif.helper
  13. from PIL import Image, ImageFont, ImageDraw, PngImagePlugin
  14. from fonts.ttf import Roboto
  15. import string
  16. import json
  17. import hashlib
  18. from modules import sd_samplers, shared, script_callbacks, errors
  19. from modules.shared import opts, cmd_opts
  20. LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS)
  21. def image_grid(imgs, batch_size=1, rows=None):
  22. if rows is None:
  23. if opts.n_rows > 0:
  24. rows = opts.n_rows
  25. elif opts.n_rows == 0:
  26. rows = batch_size
  27. elif opts.grid_prevent_empty_spots:
  28. rows = math.floor(math.sqrt(len(imgs)))
  29. while len(imgs) % rows != 0:
  30. rows -= 1
  31. else:
  32. rows = math.sqrt(len(imgs))
  33. rows = round(rows)
  34. if rows > len(imgs):
  35. rows = len(imgs)
  36. cols = math.ceil(len(imgs) / rows)
  37. params = script_callbacks.ImageGridLoopParams(imgs, cols, rows)
  38. script_callbacks.image_grid_callback(params)
  39. w, h = imgs[0].size
  40. grid = Image.new('RGB', size=(params.cols * w, params.rows * h), color='black')
  41. for i, img in enumerate(params.imgs):
  42. grid.paste(img, box=(i % params.cols * w, i // params.cols * h))
  43. return grid
  44. Grid = namedtuple("Grid", ["tiles", "tile_w", "tile_h", "image_w", "image_h", "overlap"])
  45. def split_grid(image, tile_w=512, tile_h=512, overlap=64):
  46. w = image.width
  47. h = image.height
  48. non_overlap_width = tile_w - overlap
  49. non_overlap_height = tile_h - overlap
  50. cols = math.ceil((w - overlap) / non_overlap_width)
  51. rows = math.ceil((h - overlap) / non_overlap_height)
  52. dx = (w - tile_w) / (cols - 1) if cols > 1 else 0
  53. dy = (h - tile_h) / (rows - 1) if rows > 1 else 0
  54. grid = Grid([], tile_w, tile_h, w, h, overlap)
  55. for row in range(rows):
  56. row_images = []
  57. y = int(row * dy)
  58. if y + tile_h >= h:
  59. y = h - tile_h
  60. for col in range(cols):
  61. x = int(col * dx)
  62. if x + tile_w >= w:
  63. x = w - tile_w
  64. tile = image.crop((x, y, x + tile_w, y + tile_h))
  65. row_images.append([x, tile_w, tile])
  66. grid.tiles.append([y, tile_h, row_images])
  67. return grid
  68. def combine_grid(grid):
  69. def make_mask_image(r):
  70. r = r * 255 / grid.overlap
  71. r = r.astype(np.uint8)
  72. return Image.fromarray(r, 'L')
  73. mask_w = make_mask_image(np.arange(grid.overlap, dtype=np.float32).reshape((1, grid.overlap)).repeat(grid.tile_h, axis=0))
  74. mask_h = make_mask_image(np.arange(grid.overlap, dtype=np.float32).reshape((grid.overlap, 1)).repeat(grid.image_w, axis=1))
  75. combined_image = Image.new("RGB", (grid.image_w, grid.image_h))
  76. for y, h, row in grid.tiles:
  77. combined_row = Image.new("RGB", (grid.image_w, h))
  78. for x, w, tile in row:
  79. if x == 0:
  80. combined_row.paste(tile, (0, 0))
  81. continue
  82. combined_row.paste(tile.crop((0, 0, grid.overlap, h)), (x, 0), mask=mask_w)
  83. combined_row.paste(tile.crop((grid.overlap, 0, w, h)), (x + grid.overlap, 0))
  84. if y == 0:
  85. combined_image.paste(combined_row, (0, 0))
  86. continue
  87. combined_image.paste(combined_row.crop((0, 0, combined_row.width, grid.overlap)), (0, y), mask=mask_h)
  88. combined_image.paste(combined_row.crop((0, grid.overlap, combined_row.width, h)), (0, y + grid.overlap))
  89. return combined_image
  90. class GridAnnotation:
  91. def __init__(self, text='', is_active=True):
  92. self.text = text
  93. self.is_active = is_active
  94. self.size = None
  95. def draw_grid_annotations(im, width, height, hor_texts, ver_texts, margin=0):
  96. def wrap(drawing, text, font, line_length):
  97. lines = ['']
  98. for word in text.split():
  99. line = f'{lines[-1]} {word}'.strip()
  100. if drawing.textlength(line, font=font) <= line_length:
  101. lines[-1] = line
  102. else:
  103. lines.append(word)
  104. return lines
  105. def get_font(fontsize):
  106. try:
  107. return ImageFont.truetype(opts.font or Roboto, fontsize)
  108. except Exception:
  109. return ImageFont.truetype(Roboto, fontsize)
  110. def draw_texts(drawing, draw_x, draw_y, lines, initial_fnt, initial_fontsize):
  111. for i, line in enumerate(lines):
  112. fnt = initial_fnt
  113. fontsize = initial_fontsize
  114. while drawing.multiline_textsize(line.text, font=fnt)[0] > line.allowed_width and fontsize > 0:
  115. fontsize -= 1
  116. fnt = get_font(fontsize)
  117. drawing.multiline_text((draw_x, draw_y + line.size[1] / 2), line.text, font=fnt, fill=color_active if line.is_active else color_inactive, anchor="mm", align="center")
  118. if not line.is_active:
  119. drawing.line((draw_x - line.size[0] // 2, draw_y + line.size[1] // 2, draw_x + line.size[0] // 2, draw_y + line.size[1] // 2), fill=color_inactive, width=4)
  120. draw_y += line.size[1] + line_spacing
  121. fontsize = (width + height) // 25
  122. line_spacing = fontsize // 2
  123. fnt = get_font(fontsize)
  124. color_active = (0, 0, 0)
  125. color_inactive = (153, 153, 153)
  126. pad_left = 0 if sum([sum([len(line.text) for line in lines]) for lines in ver_texts]) == 0 else width * 3 // 4
  127. cols = im.width // width
  128. rows = im.height // height
  129. assert cols == len(hor_texts), f'bad number of horizontal texts: {len(hor_texts)}; must be {cols}'
  130. assert rows == len(ver_texts), f'bad number of vertical texts: {len(ver_texts)}; must be {rows}'
  131. calc_img = Image.new("RGB", (1, 1), "white")
  132. calc_d = ImageDraw.Draw(calc_img)
  133. for texts, allowed_width in zip(hor_texts + ver_texts, [width] * len(hor_texts) + [pad_left] * len(ver_texts)):
  134. items = [] + texts
  135. texts.clear()
  136. for line in items:
  137. wrapped = wrap(calc_d, line.text, fnt, allowed_width)
  138. texts += [GridAnnotation(x, line.is_active) for x in wrapped]
  139. for line in texts:
  140. bbox = calc_d.multiline_textbbox((0, 0), line.text, font=fnt)
  141. line.size = (bbox[2] - bbox[0], bbox[3] - bbox[1])
  142. line.allowed_width = allowed_width
  143. hor_text_heights = [sum([line.size[1] + line_spacing for line in lines]) - line_spacing for lines in hor_texts]
  144. ver_text_heights = [sum([line.size[1] + line_spacing for line in lines]) - line_spacing * len(lines) for lines in ver_texts]
  145. pad_top = 0 if sum(hor_text_heights) == 0 else max(hor_text_heights) + line_spacing * 2
  146. result = Image.new("RGB", (im.width + pad_left + margin * (cols-1), im.height + pad_top + margin * (rows-1)), "white")
  147. for row in range(rows):
  148. for col in range(cols):
  149. cell = im.crop((width * col, height * row, width * (col+1), height * (row+1)))
  150. result.paste(cell, (pad_left + (width + margin) * col, pad_top + (height + margin) * row))
  151. d = ImageDraw.Draw(result)
  152. for col in range(cols):
  153. x = pad_left + (width + margin) * col + width / 2
  154. y = pad_top / 2 - hor_text_heights[col] / 2
  155. draw_texts(d, x, y, hor_texts[col], fnt, fontsize)
  156. for row in range(rows):
  157. x = pad_left / 2
  158. y = pad_top + (height + margin) * row + height / 2 - ver_text_heights[row] / 2
  159. draw_texts(d, x, y, ver_texts[row], fnt, fontsize)
  160. return result
  161. def draw_prompt_matrix(im, width, height, all_prompts, margin=0):
  162. prompts = all_prompts[1:]
  163. boundary = math.ceil(len(prompts) / 2)
  164. prompts_horiz = prompts[:boundary]
  165. prompts_vert = prompts[boundary:]
  166. hor_texts = [[GridAnnotation(x, is_active=pos & (1 << i) != 0) for i, x in enumerate(prompts_horiz)] for pos in range(1 << len(prompts_horiz))]
  167. ver_texts = [[GridAnnotation(x, is_active=pos & (1 << i) != 0) for i, x in enumerate(prompts_vert)] for pos in range(1 << len(prompts_vert))]
  168. return draw_grid_annotations(im, width, height, hor_texts, ver_texts, margin)
  169. def resize_image(resize_mode, im, width, height, upscaler_name=None):
  170. """
  171. Resizes an image with the specified resize_mode, width, and height.
  172. Args:
  173. resize_mode: The mode to use when resizing the image.
  174. 0: Resize the image to the specified width and height.
  175. 1: Resize the image to fill the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, cropping the excess.
  176. 2: Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, filling empty with data from image.
  177. im: The image to resize.
  178. width: The width to resize the image to.
  179. height: The height to resize the image to.
  180. upscaler_name: The name of the upscaler to use. If not provided, defaults to opts.upscaler_for_img2img.
  181. """
  182. upscaler_name = upscaler_name or opts.upscaler_for_img2img
  183. def resize(im, w, h):
  184. if upscaler_name is None or upscaler_name == "None" or im.mode == 'L':
  185. return im.resize((w, h), resample=LANCZOS)
  186. scale = max(w / im.width, h / im.height)
  187. if scale > 1.0:
  188. upscalers = [x for x in shared.sd_upscalers if x.name == upscaler_name]
  189. if len(upscalers) == 0:
  190. upscaler = shared.sd_upscalers[0]
  191. print(f"could not find upscaler named {upscaler_name or '<empty string>'}, using {upscaler.name} as a fallback")
  192. else:
  193. upscaler = upscalers[0]
  194. im = upscaler.scaler.upscale(im, scale, upscaler.data_path)
  195. if im.width != w or im.height != h:
  196. im = im.resize((w, h), resample=LANCZOS)
  197. return im
  198. if resize_mode == 0:
  199. res = resize(im, width, height)
  200. elif resize_mode == 1:
  201. ratio = width / height
  202. src_ratio = im.width / im.height
  203. src_w = width if ratio > src_ratio else im.width * height // im.height
  204. src_h = height if ratio <= src_ratio else im.height * width // im.width
  205. resized = resize(im, src_w, src_h)
  206. res = Image.new("RGB", (width, height))
  207. res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2))
  208. else:
  209. ratio = width / height
  210. src_ratio = im.width / im.height
  211. src_w = width if ratio < src_ratio else im.width * height // im.height
  212. src_h = height if ratio >= src_ratio else im.height * width // im.width
  213. resized = resize(im, src_w, src_h)
  214. res = Image.new("RGB", (width, height))
  215. res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2))
  216. if ratio < src_ratio:
  217. fill_height = height // 2 - src_h // 2
  218. res.paste(resized.resize((width, fill_height), box=(0, 0, width, 0)), box=(0, 0))
  219. res.paste(resized.resize((width, fill_height), box=(0, resized.height, width, resized.height)), box=(0, fill_height + src_h))
  220. elif ratio > src_ratio:
  221. fill_width = width // 2 - src_w // 2
  222. res.paste(resized.resize((fill_width, height), box=(0, 0, 0, height)), box=(0, 0))
  223. res.paste(resized.resize((fill_width, height), box=(resized.width, 0, resized.width, height)), box=(fill_width + src_w, 0))
  224. return res
  225. invalid_filename_chars = '<>:"/\\|?*\n'
  226. invalid_filename_prefix = ' '
  227. invalid_filename_postfix = ' .'
  228. re_nonletters = re.compile(r'[\s' + string.punctuation + ']+')
  229. re_pattern = re.compile(r"(.*?)(?:\[([^\[\]]+)\]|$)")
  230. re_pattern_arg = re.compile(r"(.*)<([^>]*)>$")
  231. max_filename_part_length = 128
  232. def sanitize_filename_part(text, replace_spaces=True):
  233. if text is None:
  234. return None
  235. if replace_spaces:
  236. text = text.replace(' ', '_')
  237. text = text.translate({ord(x): '_' for x in invalid_filename_chars})
  238. text = text.lstrip(invalid_filename_prefix)[:max_filename_part_length]
  239. text = text.rstrip(invalid_filename_postfix)
  240. return text
  241. class FilenameGenerator:
  242. replacements = {
  243. 'seed': lambda self: self.seed if self.seed is not None else '',
  244. 'steps': lambda self: self.p and self.p.steps,
  245. 'cfg': lambda self: self.p and self.p.cfg_scale,
  246. 'width': lambda self: self.image.width,
  247. 'height': lambda self: self.image.height,
  248. 'styles': lambda self: self.p and sanitize_filename_part(", ".join([style for style in self.p.styles if not style == "None"]) or "None", replace_spaces=False),
  249. 'sampler': lambda self: self.p and sanitize_filename_part(self.p.sampler_name, replace_spaces=False),
  250. 'model_hash': lambda self: getattr(self.p, "sd_model_hash", shared.sd_model.sd_model_hash),
  251. 'model_name': lambda self: sanitize_filename_part(shared.sd_model.sd_checkpoint_info.model_name, replace_spaces=False),
  252. 'date': lambda self: datetime.datetime.now().strftime('%Y-%m-%d'),
  253. 'datetime': lambda self, *args: self.datetime(*args), # accepts formats: [datetime], [datetime<Format>], [datetime<Format><Time Zone>]
  254. 'job_timestamp': lambda self: getattr(self.p, "job_timestamp", shared.state.job_timestamp),
  255. 'prompt_hash': lambda self: hashlib.sha256(self.prompt.encode()).hexdigest()[0:8],
  256. 'prompt': lambda self: sanitize_filename_part(self.prompt),
  257. 'prompt_no_styles': lambda self: self.prompt_no_style(),
  258. 'prompt_spaces': lambda self: sanitize_filename_part(self.prompt, replace_spaces=False),
  259. 'prompt_words': lambda self: self.prompt_words(),
  260. }
  261. default_time_format = '%Y%m%d%H%M%S'
  262. def __init__(self, p, seed, prompt, image):
  263. self.p = p
  264. self.seed = seed
  265. self.prompt = prompt
  266. self.image = image
  267. def prompt_no_style(self):
  268. if self.p is None or self.prompt is None:
  269. return None
  270. prompt_no_style = self.prompt
  271. for style in shared.prompt_styles.get_style_prompts(self.p.styles):
  272. if len(style) > 0:
  273. for part in style.split("{prompt}"):
  274. prompt_no_style = prompt_no_style.replace(part, "").replace(", ,", ",").strip().strip(',')
  275. prompt_no_style = prompt_no_style.replace(style, "").strip().strip(',').strip()
  276. return sanitize_filename_part(prompt_no_style, replace_spaces=False)
  277. def prompt_words(self):
  278. words = [x for x in re_nonletters.split(self.prompt or "") if len(x) > 0]
  279. if len(words) == 0:
  280. words = ["empty"]
  281. return sanitize_filename_part(" ".join(words[0:opts.directories_max_prompt_words]), replace_spaces=False)
  282. def datetime(self, *args):
  283. time_datetime = datetime.datetime.now()
  284. time_format = args[0] if len(args) > 0 and args[0] != "" else self.default_time_format
  285. try:
  286. time_zone = pytz.timezone(args[1]) if len(args) > 1 else None
  287. except pytz.exceptions.UnknownTimeZoneError as _:
  288. time_zone = None
  289. time_zone_time = time_datetime.astimezone(time_zone)
  290. try:
  291. formatted_time = time_zone_time.strftime(time_format)
  292. except (ValueError, TypeError) as _:
  293. formatted_time = time_zone_time.strftime(self.default_time_format)
  294. return sanitize_filename_part(formatted_time, replace_spaces=False)
  295. def apply(self, x):
  296. res = ''
  297. for m in re_pattern.finditer(x):
  298. text, pattern = m.groups()
  299. res += text
  300. if pattern is None:
  301. continue
  302. pattern_args = []
  303. while True:
  304. m = re_pattern_arg.match(pattern)
  305. if m is None:
  306. break
  307. pattern, arg = m.groups()
  308. pattern_args.insert(0, arg)
  309. fun = self.replacements.get(pattern.lower())
  310. if fun is not None:
  311. try:
  312. replacement = fun(self, *pattern_args)
  313. except Exception:
  314. replacement = None
  315. print(f"Error adding [{pattern}] to filename", file=sys.stderr)
  316. print(traceback.format_exc(), file=sys.stderr)
  317. if replacement is not None:
  318. res += str(replacement)
  319. continue
  320. res += f'[{pattern}]'
  321. return res
  322. def get_next_sequence_number(path, basename):
  323. """
  324. Determines and returns the next sequence number to use when saving an image in the specified directory.
  325. The sequence starts at 0.
  326. """
  327. result = -1
  328. if basename != '':
  329. basename = basename + "-"
  330. prefix_length = len(basename)
  331. for p in os.listdir(path):
  332. if p.startswith(basename):
  333. l = os.path.splitext(p[prefix_length:])[0].split('-') # splits the filename (removing the basename first if one is defined, so the sequence number is always the first element)
  334. try:
  335. result = max(int(l[0]), result)
  336. except ValueError:
  337. pass
  338. return result + 1
  339. def save_image(image, path, basename, seed=None, prompt=None, extension='png', info=None, short_filename=False, no_prompt=False, grid=False, pnginfo_section_name='parameters', p=None, existing_info=None, forced_filename=None, suffix="", save_to_dirs=None):
  340. """Save an image.
  341. Args:
  342. image (`PIL.Image`):
  343. The image to be saved.
  344. path (`str`):
  345. The directory to save the image. Note, the option `save_to_dirs` will make the image to be saved into a sub directory.
  346. basename (`str`):
  347. The base filename which will be applied to `filename pattern`.
  348. seed, prompt, short_filename,
  349. extension (`str`):
  350. Image file extension, default is `png`.
  351. pngsectionname (`str`):
  352. Specify the name of the section which `info` will be saved in.
  353. info (`str` or `PngImagePlugin.iTXt`):
  354. PNG info chunks.
  355. existing_info (`dict`):
  356. Additional PNG info. `existing_info == {pngsectionname: info, ...}`
  357. no_prompt:
  358. TODO I don't know its meaning.
  359. p (`StableDiffusionProcessing`)
  360. forced_filename (`str`):
  361. If specified, `basename` and filename pattern will be ignored.
  362. save_to_dirs (bool):
  363. If true, the image will be saved into a subdirectory of `path`.
  364. Returns: (fullfn, txt_fullfn)
  365. fullfn (`str`):
  366. The full path of the saved imaged.
  367. txt_fullfn (`str` or None):
  368. If a text file is saved for this image, this will be its full path. Otherwise None.
  369. """
  370. namegen = FilenameGenerator(p, seed, prompt, image)
  371. if save_to_dirs is None:
  372. save_to_dirs = (grid and opts.grid_save_to_dirs) or (not grid and opts.save_to_dirs and not no_prompt)
  373. if save_to_dirs:
  374. dirname = namegen.apply(opts.directories_filename_pattern or "[prompt_words]").lstrip(' ').rstrip('\\ /')
  375. path = os.path.join(path, dirname)
  376. os.makedirs(path, exist_ok=True)
  377. if forced_filename is None:
  378. if short_filename or seed is None:
  379. file_decoration = ""
  380. elif opts.save_to_dirs:
  381. file_decoration = opts.samples_filename_pattern or "[seed]"
  382. else:
  383. file_decoration = opts.samples_filename_pattern or "[seed]-[prompt_spaces]"
  384. add_number = opts.save_images_add_number or file_decoration == ''
  385. if file_decoration != "" and add_number:
  386. file_decoration = "-" + file_decoration
  387. file_decoration = namegen.apply(file_decoration) + suffix
  388. if add_number:
  389. basecount = get_next_sequence_number(path, basename)
  390. fullfn = None
  391. for i in range(500):
  392. fn = f"{basecount + i:05}" if basename == '' else f"{basename}-{basecount + i:04}"
  393. fullfn = os.path.join(path, f"{fn}{file_decoration}.{extension}")
  394. if not os.path.exists(fullfn):
  395. break
  396. else:
  397. fullfn = os.path.join(path, f"{file_decoration}.{extension}")
  398. else:
  399. fullfn = os.path.join(path, f"{forced_filename}.{extension}")
  400. pnginfo = existing_info or {}
  401. if info is not None:
  402. pnginfo[pnginfo_section_name] = info
  403. params = script_callbacks.ImageSaveParams(image, p, fullfn, pnginfo)
  404. script_callbacks.before_image_saved_callback(params)
  405. image = params.image
  406. fullfn = params.filename
  407. info = params.pnginfo.get(pnginfo_section_name, None)
  408. def _atomically_save_image(image_to_save, filename_without_extension, extension):
  409. # save image with .tmp extension to avoid race condition when another process detects new image in the directory
  410. temp_file_path = filename_without_extension + ".tmp"
  411. image_format = Image.registered_extensions()[extension]
  412. if extension.lower() == '.png':
  413. pnginfo_data = PngImagePlugin.PngInfo()
  414. if opts.enable_pnginfo:
  415. for k, v in params.pnginfo.items():
  416. pnginfo_data.add_text(k, str(v))
  417. image_to_save.save(temp_file_path, format=image_format, quality=opts.jpeg_quality, pnginfo=pnginfo_data)
  418. elif extension.lower() in (".jpg", ".jpeg", ".webp"):
  419. if image_to_save.mode == 'RGBA':
  420. image_to_save = image_to_save.convert("RGB")
  421. elif image_to_save.mode == 'I;16':
  422. image_to_save = image_to_save.point(lambda p: p * 0.0038910505836576).convert("RGB" if extension.lower() == ".webp" else "L")
  423. image_to_save.save(temp_file_path, format=image_format, quality=opts.jpeg_quality, lossless=opts.webp_lossless)
  424. if opts.enable_pnginfo and info is not None:
  425. exif_bytes = piexif.dump({
  426. "Exif": {
  427. piexif.ExifIFD.UserComment: piexif.helper.UserComment.dump(info or "", encoding="unicode")
  428. },
  429. })
  430. piexif.insert(exif_bytes, temp_file_path)
  431. else:
  432. image_to_save.save(temp_file_path, format=image_format, quality=opts.jpeg_quality)
  433. # atomically rename the file with correct extension
  434. os.replace(temp_file_path, filename_without_extension + extension)
  435. fullfn_without_extension, extension = os.path.splitext(params.filename)
  436. if hasattr(os, 'statvfs'):
  437. max_name_len = os.statvfs(path).f_namemax
  438. fullfn_without_extension = fullfn_without_extension[:max_name_len - max(4, len(extension))]
  439. params.filename = fullfn_without_extension + extension
  440. fullfn = params.filename
  441. _atomically_save_image(image, fullfn_without_extension, extension)
  442. image.already_saved_as = fullfn
  443. oversize = image.width > opts.target_side_length or image.height > opts.target_side_length
  444. if opts.export_for_4chan and (oversize or os.stat(fullfn).st_size > opts.img_downscale_threshold * 1024 * 1024):
  445. ratio = image.width / image.height
  446. if oversize and ratio > 1:
  447. image = image.resize((round(opts.target_side_length), round(image.height * opts.target_side_length / image.width)), LANCZOS)
  448. elif oversize:
  449. image = image.resize((round(image.width * opts.target_side_length / image.height), round(opts.target_side_length)), LANCZOS)
  450. try:
  451. _atomically_save_image(image, fullfn_without_extension, ".jpg")
  452. except Exception as e:
  453. errors.display(e, "saving image as downscaled JPG")
  454. if opts.save_txt and info is not None:
  455. txt_fullfn = f"{fullfn_without_extension}.txt"
  456. with open(txt_fullfn, "w", encoding="utf8") as file:
  457. file.write(info + "\n")
  458. else:
  459. txt_fullfn = None
  460. script_callbacks.image_saved_callback(params)
  461. return fullfn, txt_fullfn
  462. def read_info_from_image(image):
  463. items = image.info or {}
  464. geninfo = items.pop('parameters', None)
  465. if "exif" in items:
  466. exif = piexif.load(items["exif"])
  467. exif_comment = (exif or {}).get("Exif", {}).get(piexif.ExifIFD.UserComment, b'')
  468. try:
  469. exif_comment = piexif.helper.UserComment.load(exif_comment)
  470. except ValueError:
  471. exif_comment = exif_comment.decode('utf8', errors="ignore")
  472. if exif_comment:
  473. items['exif comment'] = exif_comment
  474. geninfo = exif_comment
  475. for field in ['jfif', 'jfif_version', 'jfif_unit', 'jfif_density', 'dpi', 'exif',
  476. 'loop', 'background', 'timestamp', 'duration']:
  477. items.pop(field, None)
  478. if items.get("Software", None) == "NovelAI":
  479. try:
  480. json_info = json.loads(items["Comment"])
  481. sampler = sd_samplers.samplers_map.get(json_info["sampler"], "Euler a")
  482. geninfo = f"""{items["Description"]}
  483. Negative prompt: {json_info["uc"]}
  484. Steps: {json_info["steps"]}, Sampler: {sampler}, CFG scale: {json_info["scale"]}, Seed: {json_info["seed"]}, Size: {image.width}x{image.height}, Clip skip: 2, ENSD: 31337"""
  485. except Exception:
  486. print("Error parsing NovelAI image generation parameters:", file=sys.stderr)
  487. print(traceback.format_exc(), file=sys.stderr)
  488. return geninfo, items
  489. def image_data(data):
  490. import gradio as gr
  491. try:
  492. image = Image.open(io.BytesIO(data))
  493. textinfo, _ = read_info_from_image(image)
  494. return textinfo, None
  495. except Exception:
  496. pass
  497. try:
  498. text = data.decode('utf8')
  499. assert len(text) < 10000
  500. return text, None
  501. except Exception:
  502. pass
  503. return gr.update(), None
  504. def flatten(img, bgcolor):
  505. """replaces transparency with bgcolor (example: "#ffffff"), returning an RGB mode image with no transparency"""
  506. if img.mode == "RGBA":
  507. background = Image.new('RGBA', img.size, bgcolor)
  508. background.paste(img, mask=img)
  509. img = background
  510. return img.convert('RGB')