From 43b79cd3497b5014a51b8bb5ec783e537737dea3 Mon Sep 17 00:00:00 2001 From: Haoming Date: Tue, 17 Mar 2026 14:40:39 +0800 Subject: [PATCH] lint --- modules/images.py | 195 ++++++++++++++++++++------------------ modules/postprocessing.py | 66 ++++++------- 2 files changed, 137 insertions(+), 124 deletions(-) diff --git a/modules/images.py b/modules/images.py index 68a1e3ae..ec86579d 100644 --- a/modules/images.py +++ b/modules/images.py @@ -55,8 +55,8 @@ def image_grid(imgs, batch_size=1, rows=None): script_callbacks.image_grid_callback(params) w, h = map(max, zip(*(img.size for img in imgs))) - grid_background_color = ImageColor.getcolor(opts.grid_background_color, 'RGBA') - grid = Image.new('RGBA', size=(params.cols * w, params.rows * h), color=grid_background_color) + grid_background_color = ImageColor.getcolor(opts.grid_background_color, "RGBA") + grid = Image.new("RGBA", size=(params.cols * w, params.rows * h), color=grid_background_color) for i, img in enumerate(params.imgs): img_w, img_h = img.size @@ -115,7 +115,7 @@ def combine_grid(grid): def make_mask_image(r): r = r * 255 / grid.overlap r = r.astype(np.uint8) - return Image.fromarray(r, 'L') + return Image.fromarray(r, "L") mask_w = make_mask_image(np.arange(grid.overlap, dtype=np.float32).reshape((1, grid.overlap)).repeat(grid.tile_h, axis=0)) mask_h = make_mask_image(np.arange(grid.overlap, dtype=np.float32).reshape((grid.overlap, 1)).repeat(grid.image_w, axis=1)) @@ -142,7 +142,7 @@ def combine_grid(grid): class GridAnnotation: - def __init__(self, text='', is_active=True): + def __init__(self, text="", is_active=True): self.text = text self.is_active = is_active self.size = None @@ -150,14 +150,14 @@ class GridAnnotation: def draw_grid_annotations(im, width, height, hor_texts, ver_texts, margin=0): - color_active = ImageColor.getcolor(opts.grid_text_active_color, 'RGB') - color_inactive = ImageColor.getcolor(opts.grid_text_inactive_color, 'RGB') - color_background = ImageColor.getcolor(opts.grid_background_color, 'RGB') + color_active = ImageColor.getcolor(opts.grid_text_active_color, "RGB") + color_inactive = ImageColor.getcolor(opts.grid_text_inactive_color, "RGB") + color_background = ImageColor.getcolor(opts.grid_background_color, "RGB") def wrap(drawing, text, font, line_length): - lines = [''] + lines = [""] for word in text.split(): - line = f'{lines[-1]} {word}'.strip() + line = f"{lines[-1]} {word}".strip() if drawing.textlength(line, font=font) <= line_length: lines[-1] = line else: @@ -192,8 +192,8 @@ def draw_grid_annotations(im, width, height, hor_texts, ver_texts, margin=0): cols = im.width // width rows = im.height // height - assert cols == len(hor_texts), f'bad number of horizontal texts: {len(hor_texts)}; must be {cols}' - assert rows == len(ver_texts), f'bad number of vertical texts: {len(ver_texts)}; must be {rows}' + assert cols == len(hor_texts), f"bad number of horizontal texts: {len(hor_texts)}; must be {cols}" + assert rows == len(ver_texts), f"bad number of vertical texts: {len(ver_texts)}; must be {rows}" calc_img = Image.new("RGB", (1, 1), color_background) calc_d = ImageDraw.Draw(calc_img) @@ -216,11 +216,11 @@ def draw_grid_annotations(im, width, height, hor_texts, ver_texts, margin=0): pad_top = 0 if sum(hor_text_heights) == 0 else max(hor_text_heights) + line_spacing * 2 - result = Image.new("RGB", (im.width + pad_left + margin * (cols-1), im.height + pad_top + margin * (rows-1)), color_background) + result = Image.new("RGB", (im.width + pad_left + margin * (cols - 1), im.height + pad_top + margin * (rows - 1)), color_background) for row in range(rows): for col in range(cols): - cell = im.crop((width * col, height * row, width * (col+1), height * (row+1))) + cell = im.crop((width * col, height * row, width * (col + 1), height * (row + 1))) result.paste(cell, (pad_left + (width + margin) * col, pad_top + (height + margin) * row)) d = ImageDraw.Draw(result) @@ -268,13 +268,13 @@ def resize_image(resize_mode, im, width, height, upscaler_name=None, force_RGBA= upscaler_name: The name of the upscaler to use. If not provided, defaults to opts.upscaler_for_img2img. """ - if not force_RGBA and im.mode == 'RGBA': - im = im.convert('RGB') + if not force_RGBA and im.mode == "RGBA": + im = im.convert("RGB") upscaler_name = upscaler_name or opts.upscaler_for_img2img def resize(im, w, h): - if upscaler_name is None or upscaler_name == "None" or im.mode == 'L' or force_RGBA: + if upscaler_name is None or upscaler_name == "None" or im.mode == "L" or force_RGBA: return im.resize((w, h), resample=LANCZOS) scale = max(w / im.width, h / im.height) @@ -336,10 +336,10 @@ def resize_image(resize_mode, im, width, height, upscaler_name=None, force_RGBA= if not shared.cmd_opts.unix_filenames_sanitization: invalid_filename_chars = '#<>:"/\\|?*\n\r\t' else: - invalid_filename_chars = '/' -invalid_filename_prefix = ' ' -invalid_filename_postfix = ' .' -re_nonletters = re.compile(r'[\s' + string.punctuation + ']+') + invalid_filename_chars = "/" +invalid_filename_prefix = " " +invalid_filename_postfix = " ." +re_nonletters = re.compile(r"[\s" + string.punctuation + "]+") re_pattern = re.compile(r"(.*?)(?:\[([^\[\]]+)\]|$)") re_pattern_arg = re.compile(r"(.*)<([^>]*)>$") max_filename_part_length = shared.cmd_opts.filenames_max_length @@ -351,9 +351,9 @@ def sanitize_filename_part(text, replace_spaces=True): return None if replace_spaces: - text = text.replace(' ', '_') + text = text.replace(" ", "_") - text = text.translate({ord(x): '_' for x in invalid_filename_chars}) + text = text.translate({ord(x): "_" for x in invalid_filename_chars}) text = text.lstrip(invalid_filename_prefix)[:max_filename_part_length] text = text.rstrip(invalid_filename_postfix) return text @@ -362,21 +362,21 @@ def sanitize_filename_part(text, replace_spaces=True): @functools.cache def get_scheduler_str(sampler_name, scheduler_name): """Returns {Scheduler} if the scheduler is applicable to the sampler""" - if scheduler_name == 'Automatic': + if scheduler_name == "Automatic": config = sd_samplers.find_sampler_config(sampler_name) - scheduler_name = config.options.get('scheduler', 'Automatic') + scheduler_name = config.options.get("scheduler", "Automatic") return scheduler_name.capitalize() @functools.cache def get_sampler_scheduler_str(sampler_name, scheduler_name): """Returns the '{Sampler} {Scheduler}' if the scheduler is applicable to the sampler""" - return f'{sampler_name} {get_scheduler_str(sampler_name, scheduler_name)}' + return f"{sampler_name} {get_scheduler_str(sampler_name, scheduler_name)}" def get_sampler_scheduler(p, sampler): """Returns '{Sampler} {Scheduler}' / '{Scheduler}' / 'NOTHING_AND_SKIP_PREVIOUS_TEXT'""" - if hasattr(p, 'scheduler') and hasattr(p, 'sampler_name'): + if hasattr(p, "scheduler") and hasattr(p, "sampler_name"): if sampler: sampler_scheduler = get_sampler_scheduler_str(p.sampler_name, p.scheduler) else: @@ -387,42 +387,42 @@ def get_sampler_scheduler(p, sampler): class FilenameGenerator: replacements = { - 'basename': lambda self: self.basename or 'img', - 'seed': lambda self: self.seed if self.seed is not None else '', - 'seed_first': lambda self: self.seed if self.p.batch_size == 1 else self.p.all_seeds[0], - 'seed_last': lambda self: NOTHING_AND_SKIP_PREVIOUS_TEXT if self.p.batch_size == 1 else self.p.all_seeds[-1], - 'steps': lambda self: self.p and self.p.steps, - 'cfg': lambda self: self.p and self.p.cfg_scale, - 'width': lambda self: self.image.width, - 'height': lambda self: self.image.height, - '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), - 'sampler': lambda self: self.p and sanitize_filename_part(self.p.sampler_name, replace_spaces=False), - 'sampler_scheduler': lambda self: self.p and get_sampler_scheduler(self.p, True), - 'scheduler': lambda self: self.p and get_sampler_scheduler(self.p, False), - 'model_hash': lambda self: getattr(self.p, "sd_model_hash", shared.sd_model.sd_model_hash), - 'model_name': lambda self: sanitize_filename_part(shared.sd_model.sd_checkpoint_info.name_for_extra, replace_spaces=False), - 'date': lambda self: datetime.datetime.now().strftime('%Y-%m-%d'), - 'datetime': lambda self, *args: self.datetime(*args), # accepts formats: [datetime], [datetime], [datetime