diff --git a/modules/images.py b/modules/images.py index cca84b0ea..8dc32d2be 100644 --- a/modules/images.py +++ b/modules/images.py @@ -11,6 +11,7 @@ import random import hashlib import datetime import threading +import seam_carving from pathlib import Path from collections import namedtuple import numpy as np @@ -300,6 +301,29 @@ def resize_image(resize_mode, im, width, height, upscaler_name=None, output_type from modules import masking res = fill(im, color=0) res, _mask = masking.outpaint(res) + elif resize_mode == 5: # seam-add + ratio = min(width / im.width, height / im.height) + im = resize(im, int(im.width * ratio), int(im.height * ratio)) + res = Image.fromarray(seam_carving.resize( + im, # source image (rgb or gray) + size=(width, height), # target size + energy_mode="backward", # choose from {backward, forward} + order="width-first", # choose from {width-first, height-first} + keep_mask=None, # object mask to protect from removal + )) + elif resize_mode == 6: # seam-delete + ratio = width / height + src_ratio = im.width / im.height + src_w = width if ratio > src_ratio else im.width * height // im.height + src_h = height if ratio <= src_ratio else im.height * width // im.width + resized = resize(im, src_w, src_h) + res = Image.fromarray(seam_carving.resize( + resized, # source image (rgb or gray) + size=(width, height), # target size + energy_mode="backward", # choose from {backward, forward} + order="width-first", # choose from {width-first, height-first} + keep_mask=None, # object mask to protect from removal + )) else: res = im.copy() shared.log.error(f'Invalid resize mode: {resize_mode}') diff --git a/modules/shared.py b/modules/shared.py index b8c9e57e0..dc96d8f83 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -76,7 +76,7 @@ restricted_opts = { "outdir_save", "outdir_init_images" } -resize_modes = ["None", "Fixed", "Crop", "Fill", "Outpaint"] +resize_modes = ["None", "Fixed", "Crop", "Fill", "Outpaint", "Seam-Carving Add", "Seam-Carving Subtract"] compatibility_opts = ['clip_skip', 'uni_pc_lower_order_final', 'uni_pc_order'] console = Console(log_time=True, log_time_format='%H:%M:%S-%f') dir_timestamps = {} diff --git a/requirements.txt b/requirements.txt index c52ab6199..e08970468 100644 --- a/requirements.txt +++ b/requirements.txt @@ -62,3 +62,4 @@ typing-extensions==4.11.0 torchdiffeq dctorch scikit-image +seam-carving