Skip to content

Instantly share code, notes, and snippets.

@DonRichards
Created July 17, 2024 14:21
Show Gist options
  • Save DonRichards/e51d37590fe267e11148061344e8bd32 to your computer and use it in GitHub Desktop.
Save DonRichards/e51d37590fe267e11148061344e8bd32 to your computer and use it in GitHub Desktop.
Super Res Down size image to use for Thumbnails

This make the image super clear and reduces the size to a width of 1024

Modified Real-ESRGAN/inference_realesrgan.py: Made changes to enhance debugging but might not be needed.

Incomplete bash history

pipenv shell
git clone https://github.com/xinntao/Real-ESRGAN.git
cd Real-ESRGAN
wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P weights
pip install torch torchvision pillow basicsr facexlib gfpgan
pip install --upgrade torch torchvision basicsr
pip install -e .
cd ..
python downsized_super_res_image.py /path/graphic_pictorial_collection_image.jpeg
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)
import argparse
import os
import sys
import cv2
import glob
import torch
from basicsr.archs.rrdbnet_arch import RRDBNet
from realesrgan import RealESRGANer
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', type=str, required=True, help='Input image or folder')
parser.add_argument('-o', '--output', type=str, required=True, help='Output folder')
parser.add_argument('-n', '--model_name', type=str, default='RealESRGAN_x4plus', help='Model name')
parser.add_argument('-w', '--model_path', type=str, required=True, help='Model path')
parser.add_argument('-s', '--outscale', type=float, default=4, help='The final scale factor')
parser.add_argument('--face_enhance', action='store_true', help='Use GFPGAN to enhance face')
parser.add_argument('--suffix', type=str, default='out', help='Suffix of the restored image')
parser.add_argument('-t', '--tile', type=int, default=0, help='Tile size, 0 for no tile during testing')
parser.add_argument('--tile_pad', type=int, default=10, help='Tile padding')
parser.add_argument('--pre_pad', type=int, default=0, help='Pre padding size at each border')
parser.add_argument('--half', action='store_true', help='Use half precision during inference')
parser.add_argument('-g', '--gpu_id', type=int, default=None, help='GPU id')
return parser.parse_args()
def main():
args = parse_args()
if not os.path.exists(args.output):
os.makedirs(args.output)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
upsampler = RealESRGANer(
scale=4,
model_path=args.model_path,
model=model,
tile=args.tile,
tile_pad=args.tile_pad,
pre_pad=args.pre_pad,
half=args.half,
device=device)
if args.face_enhance:
from gfpgan import GFPGANer
face_enhancer = GFPGANer(
model_path='https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth',
upscale=args.outscale,
arch='clean',
channel_multiplier=2,
bg_upsampler=upsampler)
img_list = []
if os.path.isfile(args.input):
img_list = [args.input]
else:
img_list = sorted(glob.glob(os.path.join(args.input, '*')))
for idx, path in enumerate(img_list):
imgname, extension = os.path.splitext(os.path.basename(path))
print(f'Testing {idx + 1}/{len(img_list)}: {imgname}{extension}')
img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
if args.face_enhance:
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
else:
output, _ = upsampler.enhance(img, outscale=args.outscale)
if args.suffix == '':
save_path = os.path.join(args.output, f'{imgname}.png')
else:
save_path = os.path.join(args.output, f'{imgname}_{args.suffix}.png')
cv2.imwrite(save_path, output)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment