|
import os |
|
import sys |
|
import subprocess |
|
import shutil |
|
from PIL import Image, PngImagePlugin |
|
|
|
# Function to resize the image to a specific width while maintaining aspect ratio |
|
def resize_image(input_path, output_path, width): |
|
with Image.open(input_path) as img: |
|
width_percent = (width / float(img.size[0])) |
|
height_size = int((float(img.size[1]) * float(width_percent))) |
|
img = img.resize((width, height_size), Image.LANCZOS) |
|
img.save(output_path, quality=85, optimize=True) # Save with compression |
|
return output_path |
|
|
|
# Function to apply super-resolution using Real-ESRGAN |
|
def apply_super_resolution(input_path, output_path): |
|
real_esrgan_script = os.path.join(os.path.dirname(__file__), 'Real-ESRGAN', 'inference_realesrgan.py') |
|
weights_path = os.path.join(os.path.dirname(__file__), 'Real-ESRGAN', 'weights', 'RealESRGAN_x4plus.pth') |
|
temp_output_dir = os.path.join(os.path.dirname(output_path), 'temp_output') |
|
|
|
# Check if the Real-ESRGAN script exists |
|
if not os.path.isfile(real_esrgan_script): |
|
print(f"Error: Real-ESRGAN script not found at {real_esrgan_script}") |
|
sys.exit(1) |
|
|
|
# Check if the weights file exists |
|
if not os.path.isfile(weights_path): |
|
print(f"Error: Weights file not found at {weights_path}") |
|
sys.exit(1) |
|
|
|
# Ensure the temporary output directory exists |
|
os.makedirs(temp_output_dir, exist_ok=True) |
|
|
|
command = [ |
|
'python', real_esrgan_script, |
|
'-i', input_path, |
|
'-o', temp_output_dir, |
|
'-n', 'RealESRGAN_x4plus', |
|
'-w', weights_path, |
|
'--tile', '400', # Adjust the tile size as needed |
|
'--tile_pad', '10' # Adjust the tile padding as needed |
|
] |
|
|
|
print("Running command:", " ".join(command)) |
|
|
|
try: |
|
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
|
result.check_returncode() |
|
print("stdout:", result.stdout) |
|
print("stderr:", result.stderr) |
|
except subprocess.CalledProcessError as e: |
|
print("Error running Real-ESRGAN:") |
|
print("Command:", e.cmd) |
|
print("Return code:", e.returncode) |
|
print("stdout:", e.stdout) |
|
print("stderr:", e.stderr) |
|
shutil.rmtree(temp_output_dir) |
|
sys.exit(e.returncode) |
|
|
|
# Move the output file to the desired location |
|
temp_output_file = os.path.join(temp_output_dir, os.listdir(temp_output_dir)[0]) |
|
shutil.move(temp_output_file, output_path) |
|
shutil.rmtree(temp_output_dir) |
|
|
|
# Function to ensure the final image has a maximum width of 1024 pixels |
|
def ensure_max_width(input_path, output_path, max_width): |
|
if os.path.isdir(input_path): |
|
print(f"Error: Output path is a directory: {input_path}") |
|
sys.exit(1) |
|
|
|
if not os.path.isfile(input_path): |
|
print(f"Error: Expected output file not found at {input_path}") |
|
sys.exit(1) |
|
|
|
with Image.open(input_path) as img: |
|
if img.width > max_width: |
|
resize_image(input_path, output_path, max_width) |
|
else: |
|
img.save(output_path, quality=85, optimize=True) # Save with compression |
|
|
|
# Function to resize the final image to 200 pixels wide for use as a thumbnail |
|
def create_thumbnail(input_path, thumbnail_path, width): |
|
resize_image(input_path, thumbnail_path, width) |
|
|
|
# Function to construct the output filename |
|
def get_output_filename(input_path, suffix='_resized', extension='.png'): |
|
base, ext = os.path.splitext(input_path) |
|
return f"{base}{suffix}{extension}" |
|
|
|
if __name__ == "__main__": |
|
if len(sys.argv) != 2: |
|
print("Usage: python script.py <path_to_image>") |
|
sys.exit(1) |
|
|
|
input_image_path = sys.argv[1] |
|
|
|
# Check if the input file exists |
|
if not os.path.isfile(input_image_path): |
|
print(f"Error: Input file not found at {input_image_path}") |
|
sys.exit(1) |
|
|
|
resized_image_path = get_output_filename(input_image_path, '_resized') |
|
super_res_image_path = get_output_filename(input_image_path, '_super_resolved', extension='.png') |
|
final_image_path = get_output_filename(input_image_path, '_final', extension='.png') |
|
thumbnail_image_path = get_output_filename(input_image_path, '_thumbnail', extension='.jpg') |
|
|
|
# Resize the image to a width of 1024 pixels |
|
resize_image(input_image_path, resized_image_path, 1024) |
|
|
|
# Apply super-resolution |
|
apply_super_resolution(resized_image_path, super_res_image_path) |
|
|
|
# Ensure the final image has a maximum width of 1024 pixels |
|
ensure_max_width(super_res_image_path, final_image_path, 1024) |
|
|
|
# Create a 200px wide thumbnail |
|
create_thumbnail(final_image_path, thumbnail_image_path, 200) |
|
|
|
print("Final image saved at", final_image_path) |
|
print("Thumbnail image saved at", thumbnail_image_path) |