Spaces:
Running
on
Zero
Running
on
Zero
update workflow, compress result img size
Browse files- app.py +13 -4
- utils/image_utils.py +105 -0
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import subprocess
|
|
| 10 |
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
from huggingface_hub import snapshot_download
|
|
|
|
| 13 |
|
| 14 |
#https://huggingface.co/SG161222/Realistic_Vision_V6.0_B1_noVAE/blob/main/Realistic_Vision_V6.0_NV_B1_fp16.safetensors
|
| 15 |
print("Realistic_Vision_V6.0_NV_B1_fp16.safetensors")
|
|
@@ -699,9 +700,10 @@ def generate_image(model_image, hairstyle_template_image):
|
|
| 699 |
|
| 700 |
# result
|
| 701 |
saveimage_680 = saveimage.save_images(
|
| 702 |
-
filename_prefix="
|
| 703 |
images=get_value_at_index(layerutility_imageblend_v2_686, 0),
|
| 704 |
)
|
|
|
|
| 705 |
|
| 706 |
image_comparer_rgthree_820 = image_comparer_rgthree.compare_images(
|
| 707 |
image_a=get_value_at_index(layerutility_imageblend_v2_399, 0),
|
|
@@ -722,15 +724,21 @@ def generate_image(model_image, hairstyle_template_image):
|
|
| 722 |
|
| 723 |
# result with ultra sharp
|
| 724 |
saveimage_847 = saveimage.save_images(
|
| 725 |
-
filename_prefix="
|
| 726 |
images=get_value_at_index(imageupscalewithmodel_831, 0),
|
| 727 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 728 |
|
| 729 |
# bald
|
| 730 |
saveimage_848 = saveimage.save_images(
|
| 731 |
-
filename_prefix="
|
| 732 |
images=get_value_at_index(layerutility_imageblend_v2_399, 0),
|
| 733 |
)
|
|
|
|
| 734 |
|
| 735 |
saved_path_bald = f"output/{saveimage_848['ui']['images'][0]['filename']}"
|
| 736 |
saved_path_result = f"output/{saveimage_680['ui']['images'][0]['filename']}"
|
|
@@ -887,9 +895,10 @@ def generate_image_bald(model_image):
|
|
| 887 |
|
| 888 |
# bald
|
| 889 |
saveimage_848 = saveimage.save_images(
|
| 890 |
-
filename_prefix="
|
| 891 |
images=get_value_at_index(layerutility_imageblend_v2_399, 0),
|
| 892 |
)
|
|
|
|
| 893 |
|
| 894 |
saved_path_bald = f"output/{saveimage_848['ui']['images'][0]['filename']}"
|
| 895 |
return saved_path_bald
|
|
|
|
| 10 |
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
from huggingface_hub import snapshot_download
|
| 13 |
+
from utils.image_utils import remove_image_metadata, resize_and_optimize_image
|
| 14 |
|
| 15 |
#https://huggingface.co/SG161222/Realistic_Vision_V6.0_B1_noVAE/blob/main/Realistic_Vision_V6.0_NV_B1_fp16.safetensors
|
| 16 |
print("Realistic_Vision_V6.0_NV_B1_fp16.safetensors")
|
|
|
|
| 700 |
|
| 701 |
# result
|
| 702 |
saveimage_680 = saveimage.save_images(
|
| 703 |
+
filename_prefix="hairstyle_filter",
|
| 704 |
images=get_value_at_index(layerutility_imageblend_v2_686, 0),
|
| 705 |
)
|
| 706 |
+
remove_image_metadata(f"output/{saveimage_680['ui']['images'][0]['filename']}")
|
| 707 |
|
| 708 |
image_comparer_rgthree_820 = image_comparer_rgthree.compare_images(
|
| 709 |
image_a=get_value_at_index(layerutility_imageblend_v2_399, 0),
|
|
|
|
| 724 |
|
| 725 |
# result with ultra sharp
|
| 726 |
saveimage_847 = saveimage.save_images(
|
| 727 |
+
filename_prefix="hairstyle_filter_result",
|
| 728 |
images=get_value_at_index(imageupscalewithmodel_831, 0),
|
| 729 |
)
|
| 730 |
+
# 将4x upscale的图片缩小到2x,并优化压缩(长边从3072降到1536)
|
| 731 |
+
resize_and_optimize_image(
|
| 732 |
+
f"output/{saveimage_847['ui']['images'][0]['filename']}",
|
| 733 |
+
target_long_edge=1536
|
| 734 |
+
)
|
| 735 |
|
| 736 |
# bald
|
| 737 |
saveimage_848 = saveimage.save_images(
|
| 738 |
+
filename_prefix="hairstyle_bald_result",
|
| 739 |
images=get_value_at_index(layerutility_imageblend_v2_399, 0),
|
| 740 |
)
|
| 741 |
+
remove_image_metadata(f"output/{saveimage_848['ui']['images'][0]['filename']}")
|
| 742 |
|
| 743 |
saved_path_bald = f"output/{saveimage_848['ui']['images'][0]['filename']}"
|
| 744 |
saved_path_result = f"output/{saveimage_680['ui']['images'][0]['filename']}"
|
|
|
|
| 895 |
|
| 896 |
# bald
|
| 897 |
saveimage_848 = saveimage.save_images(
|
| 898 |
+
filename_prefix="hairstyle_bald_result",
|
| 899 |
images=get_value_at_index(layerutility_imageblend_v2_399, 0),
|
| 900 |
)
|
| 901 |
+
remove_image_metadata(f"output/{saveimage_848['ui']['images'][0]['filename']}")
|
| 902 |
|
| 903 |
saved_path_bald = f"output/{saveimage_848['ui']['images'][0]['filename']}"
|
| 904 |
return saved_path_bald
|
utils/image_utils.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Image processing utility functions
|
| 3 |
+
"""
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def remove_image_metadata(image_path: str) -> None:
|
| 9 |
+
"""
|
| 10 |
+
Remove metadata information from image file
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
image_path: Path to the image file
|
| 14 |
+
"""
|
| 15 |
+
if not os.path.exists(image_path):
|
| 16 |
+
print(f"Warning: Image file does not exist: {image_path}")
|
| 17 |
+
return
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
# Load original image
|
| 21 |
+
img = Image.open(image_path)
|
| 22 |
+
|
| 23 |
+
# Create new image without metadata
|
| 24 |
+
# Preserve image mode and size
|
| 25 |
+
img_without_metadata = Image.new(img.mode, img.size)
|
| 26 |
+
img_without_metadata.putdata(list(img.getdata()))
|
| 27 |
+
|
| 28 |
+
# Save without any metadata
|
| 29 |
+
img_without_metadata.save(image_path, format='PNG')
|
| 30 |
+
|
| 31 |
+
print(f"Removed image metadata: {image_path}")
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Failed to remove metadata ({image_path}): {str(e)}")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def remove_multiple_images_metadata(image_paths: list) -> None:
|
| 37 |
+
"""
|
| 38 |
+
Batch remove metadata from multiple image files
|
| 39 |
+
|
| 40 |
+
Args:
|
| 41 |
+
image_paths: List of image file paths
|
| 42 |
+
"""
|
| 43 |
+
for path in image_paths:
|
| 44 |
+
remove_image_metadata(path)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def resize_and_optimize_image(image_path: str, target_long_edge: int = 1536) -> None:
|
| 48 |
+
"""
|
| 49 |
+
Resize image (long edge) and optimize PNG compression, while removing metadata
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
image_path: Path to the image file
|
| 53 |
+
target_long_edge: Target long edge size, default 1536 (768×2)
|
| 54 |
+
"""
|
| 55 |
+
if not os.path.exists(image_path):
|
| 56 |
+
print(f"Warning: Image file does not exist: {image_path}")
|
| 57 |
+
return
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
# Load original image
|
| 61 |
+
img = Image.open(image_path)
|
| 62 |
+
original_size = img.size
|
| 63 |
+
|
| 64 |
+
# Calculate scaling ratio
|
| 65 |
+
width, height = img.size
|
| 66 |
+
long_edge = max(width, height)
|
| 67 |
+
|
| 68 |
+
# If long edge is already smaller than target, only optimize compression
|
| 69 |
+
if long_edge <= target_long_edge:
|
| 70 |
+
print(f"Image size {original_size} is already smaller than target, only optimizing compression: {image_path}")
|
| 71 |
+
img.save(
|
| 72 |
+
image_path,
|
| 73 |
+
format='PNG',
|
| 74 |
+
compress_level=9, # Maximum compression level (0-9)
|
| 75 |
+
optimize=True # Enable optimization
|
| 76 |
+
)
|
| 77 |
+
return
|
| 78 |
+
|
| 79 |
+
# Calculate new dimensions
|
| 80 |
+
scale = target_long_edge / long_edge
|
| 81 |
+
new_width = int(width * scale)
|
| 82 |
+
new_height = int(height * scale)
|
| 83 |
+
|
| 84 |
+
# Resize using high-quality Lanczos resampling algorithm
|
| 85 |
+
img_resized = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 86 |
+
|
| 87 |
+
# Save optimized image (without metadata)
|
| 88 |
+
img_resized.save(
|
| 89 |
+
image_path,
|
| 90 |
+
format='PNG',
|
| 91 |
+
compress_level=9, # Maximum compression level
|
| 92 |
+
optimize=True # Enable optimization
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
print(f"Image resized and optimized: {image_path}")
|
| 96 |
+
print(f" Original size: {original_size} -> New size: ({new_width}, {new_height})")
|
| 97 |
+
|
| 98 |
+
# Display file size
|
| 99 |
+
if os.path.exists(image_path):
|
| 100 |
+
file_size_mb = os.path.getsize(image_path) / (1024 * 1024)
|
| 101 |
+
print(f" File size: {file_size_mb:.2f} MB")
|
| 102 |
+
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(f"Failed to resize and optimize image ({image_path}): {str(e)}")
|
| 105 |
+
|