Merge pull request 'New Image size limit patch' (#1) from hitmare-patch-image-limit into main

Reviewed-on: #1
Looks good, Merge not tested
This commit is contained in:
Hitmare 2023-11-14 20:09:46 +00:00
commit 4178bd4788
1 changed files with 134 additions and 32 deletions

162
api.py
View File

@ -50,7 +50,7 @@ Eris TRTpatch:
Please refer the Readme on https://git.foxo.me/Hitmare/Eris_api_tensor_patch for the full TRTpatch instructions
Eris Imagelimit:
Will resize every request down to 1024px max on height and width, keeping the aspect ratio. Also reduces Steps down to 35 if the set Steps exceeds 35
Will resize every request down to 1024px max on height and width, keeping the aspect ratio. Also reduces Steps down to the set limit if the set Steps exceeds the limit
Compatible with the TRTpatch. Please refer the Readme on https://git.foxo.me/Hitmare/Eris_api_tensor_patch for the full TRTpatch instructions
@ -65,7 +65,18 @@ eris_imagelog = False
eris_consolelog = False
eris_TRTpatch = False
eris_imagelimit = False
# Limits for text2image. needs eris_imagelimit = True
txt_max_width_height = 1024
txt_min_width_height = 320
txt_max_pixel_count = 589824
txt_max_steps = 35
txt_max_characters = 2000
# Limits for image2image. needs eris_imagelimit = True
img_max_width_height = 1024
img_min_width_height = 320
img_max_pixel_count = 589824
img_max_steps = 35
img_max_characters = 2000
def script_name_to_index(name, scripts):
try:
@ -379,22 +390,70 @@ class Api:
# Eris imagelimit 1024x1024 35 Steps
if eris_imagelimit:
# Add a check to only resize if the dimensions are larger than 1024
if txt2imgreq.width > 1024 or txt2imgreq.height > 1024:
# Calculate the aspect ratio
# Configuration variables
#max_width_height = 1024
#min_width_height = 320
#max_pixel_count = 589824
#max_steps = 35
#max_characters = 2000
max_width_height = txt_max_width_height
min_width_height = txt_min_width_height
max_pixel_count = txt_max_pixel_count
max_steps = txt_max_steps
max_characters = txt_max_characters
# Log the initial values
# initial_pixels = txt2imgreq.width * txt2imgreq.height
# print(f'Before processing: Resolution={txt2imgreq.width}x{txt2imgreq.height}, '
# f'Steps={txt2imgreq.steps}, Total Pixels={initial_pixels}')
# Log the length of the prompt before processing
# original_length = len(txt2imgreq.prompt)
# print(f'Original prompt length: {original_length} characters')
# Truncate the prompt if it exceeds the maximum number of characters
if len(txt2imgreq.prompt) > max_characters:
txt2imgreq.prompt = txt2imgreq.prompt[:max_characters]
# print(f'Truncated prompt length: {len(txt2imgreq.prompt)} characters')
# Calculate the initial aspect ratio
aspect_ratio = txt2imgreq.width / txt2imgreq.height
# Scale down to the maximum allowed dimensions while retaining the aspect ratio
if aspect_ratio > 1: # If the image is wider than it is tall
txt2imgreq.width = 1024
txt2imgreq.height = round(1024 / aspect_ratio)
else: # If the image is as wide as it is tall or taller
txt2imgreq.height = 1024
txt2imgreq.width = round(1024 * aspect_ratio)
# Ensure both dimensions are a multiple of 64 (if this is a requirement for your use case)
# Enforce maximum dimensions
if txt2imgreq.width > max_width_height or txt2imgreq.height > max_width_height:
if aspect_ratio > 1: # Image is wider than it is tall
txt2imgreq.width = max_width_height
txt2imgreq.height = int(max_width_height / aspect_ratio)
else: # Image is taller than it is wide
txt2imgreq.height = max_width_height
txt2imgreq.width = int(max_width_height * aspect_ratio)
# Enforce minimum dimensions
if txt2imgreq.width < min_width_height or txt2imgreq.height < min_width_height:
if aspect_ratio > 1: # Image is wider than it is tall
txt2imgreq.width = min_width_height
txt2imgreq.height = int(min_width_height / aspect_ratio)
else: # Image is taller than it is wide
txt2imgreq.height = min_width_height
txt2imgreq.width = int(min_width_height * aspect_ratio)
# Adjust based on maximum pixel count
if txt2imgreq.width * txt2imgreq.height > max_pixel_count:
scale_factor = (max_pixel_count / (txt2imgreq.width * txt2imgreq.height)) ** 0.5
txt2imgreq.width = int(txt2imgreq.width * scale_factor)
txt2imgreq.height = int(txt2imgreq.height * scale_factor)
# Clamp steps to the maximum allowed
txt2imgreq.steps = min(txt2imgreq.steps, max_steps)
# Ensure both dimensions are a multiple of 64
txt2imgreq.width = round(txt2imgreq.width // 64) * 64
txt2imgreq.height = round(txt2imgreq.height // 64) * 64
# Reduce maximum steps
txt2imgreq.steps = min(txt2imgreq.steps, 35)
# Calculate the adjusted pixel amount after processing
# adjusted_pixels = txt2imgreq.width * txt2imgreq.height
# print(f'After processing: Resolution={txt2imgreq.width}x{txt2imgreq.height}, '
# f'Steps={txt2imgreq.steps}, Total Pixels={adjusted_pixels}')
# Eris ______
@ -474,22 +533,65 @@ class Api:
# Eris imagelimit 1024x1024 35 Steps
if eris_imagelimit:
# Add a check to only resize if the dimensions are larger than 1024
if img2imgreq.width > 1024 or img2imgreq.height > 1024:
# Calculate the aspect ratio of the input image
# Configuration variables
#max_width_height = 1024
#min_width_height = 320
#max_pixel_count = 589824
#max_steps = 35
#max_characters = 2000
max_width_height = img_max_width_height
min_width_height = img_min_width_height
max_pixel_count = img_max_pixel_count
max_steps = img_max_steps
max_characters = img_max_characters
# Log the initial values
# initial_pixels = img2imgreq.width * img2imgreq.height
# print(f'Before processing: Resolution={img2imgreq.width}x{img2imgreq.height}, '
# f'Steps={img2imgreq.steps}, Total Pixels={initial_pixels}')
# Truncate the prompt if it exceeds the maximum number of characters
if len(img2imgreq.prompt) > max_characters:
img2imgreq.prompt = img2imgreq.prompt[:max_characters]
# Calculate the initial aspect ratio
aspect_ratio = img2imgreq.width / img2imgreq.height
# Scale down to the maximum allowed dimensions while retaining the aspect ratio
if aspect_ratio > 1: # If the image is wider than it is tall
img2imgreq.width = 1024
img2imgreq.height = round(1024 / aspect_ratio)
else: # If the image is as wide as it is tall or taller
img2imgreq.height = 1024
img2imgreq.width = round(1024 * aspect_ratio)
# Ensure both dimensions are a multiple of 64 (if this is a requirement for your use case)
img2imgreq.width = round(img2imgreq.width / 64) * 64
img2imgreq.height = round(img2imgreq.height / 64) * 64
# Reduce maximum steps
img2imgreq.steps = min(img2imgreq.steps, 35)
# Enforce maximum dimensions
if img2imgreq.width > max_width_height or img2imgreq.height > max_width_height:
if aspect_ratio > 1: # Image is wider than it is tall
img2imgreq.width = max_width_height
img2imgreq.height = int(max_width_height / aspect_ratio)
else: # Image is taller than it is wide
img2imgreq.height = max_width_height
img2imgreq.width = int(max_width_height * aspect_ratio)
# Enforce minimum dimensions
if img2imgreq.width < min_width_height or img2imgreq.height < min_width_height:
if aspect_ratio > 1: # Image is wider than it is tall
img2imgreq.width = min_width_height
img2imgreq.height = int(min_width_height / aspect_ratio)
else: # Image is taller than it is wide
img2imgreq.height = min_width_height
img2imgreq.width = int(min_width_height * aspect_ratio)
# Adjust based on maximum pixel count
if img2imgreq.width * img2imgreq.height > max_pixel_count:
scale_factor = (max_pixel_count / (img2imgreq.width * img2imgreq.height)) ** 0.5
img2imgreq.width = int(img2imgreq.width * scale_factor)
img2imgreq.height = int(img2imgreq.height * scale_factor)
# Clamp steps to the maximum allowed
img2imgreq.steps = min(img2imgreq.steps, max_steps)
# Ensure both dimensions are a multiple of 64
img2imgreq.width = round(img2imgreq.width // 64) * 64
img2imgreq.height = round(img2imgreq.height // 64) * 64
# Calculate the adjusted pixel amount after processing
# adjusted_pixels = img2imgreq.width * img2imgreq.height
# print(f'After processing: Resolution={img2imgreq.width}x{img2imgreq.height}, '
# f'Steps={img2imgreq.steps}, Total Pixels={adjusted_pixels}')
# Eris ______
# Eris console prompt log -> writes promts into the console/terminal window