{"library":"realesrgan","title":"Real-ESRGAN","description":"Real-ESRGAN provides state-of-the-art practical algorithms for general image restoration, particularly focusing on super-resolution. It is currently at version 0.3.0 and sees active development with several minor and major releases annually, bringing new models and features.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install realesrgan"],"cli":{"name":"realesrgan","version":""}},"imports":["from realesrgan import RealESRGANer"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nfrom PIL import Image\nimport numpy as np\n\n# Assuming realesrgan is installed via pip\ntry:\n    from realesrgan import RealESRGANer\nexcept ImportError:\n    print(\"RealESRGAN is not installed. Please run 'pip install realesrgan'\")\n    exit(1)\n\n# Create a dummy image for demonstration\ninput_image_path = \"temp_input_64x64.png\"\noutput_image_path = \"temp_output_upscaled.png\"\ndummy_image = Image.new('RGB', (64, 64), color = 'red')\ndummy_image.save(input_image_path)\n\ntry:\n    # Initialize RealESRGANer with a common model (auto-downloads if not present)\n    # Using 'cpu' for device ensures it runs without a CUDA setup.\n    # For GPU, change 'cpu' to 'cuda' and ensure PyTorch with CUDA is installed.\n    upscaler = RealESRGANer(\n        model_name='RealESRGAN_x4plus', # A widely used general-purpose model\n        netscale=4, # The upsampling factor the model was trained for\n        outscale=4, # Desired output upsampling factor (should match netscale for best results)\n        tile=0, # Set to 0 to process the entire image at once. For large images/limited VRAM, use e.g. 600.\n        tile_pad=10,\n        pre_pad=0,\n        device='cpu' # Use 'cuda' if a compatible GPU and PyTorch+CUDA are available\n    )\n\n    # Load and convert image\n    img = Image.open(input_image_path).convert('RGB')\n    img_np = np.array(img) # Convert PIL Image to NumPy array for processing\n\n    # Perform enhancement\n    upscaled_image_np, _ = upscaler.enhance(img_np, outscale=4)\n\n    # Convert back to PIL Image and save\n    upscaled_image_pil = Image.fromarray(upscaled_image_np)\n    upscaled_image_pil.save(output_image_path)\n\n    print(f\"Image upscaled successfully and saved to {output_image_path}\")\n\nexcept Exception as e:\n    print(f\"An error occurred during upscaling: {e}\")\n    print(\"Common issues: missing models (check network), CUDA not available/misconfigured, out of memory.\")\n    if \"cuda\" in str(e).lower() and \"memory\" in str(e).lower():\n        print(\"Consider reducing `tile` size in RealESRGANer or processing smaller images.\")\n    if \"model_name\" in str(e) or \"model_path\" in str(e):\n        print(\"Ensure the specified `model_name` is valid or `model_path` points to an existing model file.\")\nfinally:\n    # Clean up dummy files\n    if os.path.exists(input_image_path):\n        os.remove(input_image_path)\n    if os.path.exists(output_image_path):\n        os.remove(output_image_path)","lang":"python","description":"This quickstart demonstrates how to use `RealESRGANer` to upscale a dummy image programmatically. It uses the `RealESRGAN_x4plus` model, which will be automatically downloaded on first use. By default, it runs on CPU, but can be switched to 'cuda' for GPU acceleration if PyTorch with CUDA is installed.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}