Upscale Images with Python: A Practical Guide

Updated on Sep 17,2025

In today's digital world, high-resolution images are essential. This article will delve into using Python to upscale images, transforming low-resolution visuals into high-quality assets. We'll focus on a practical approach, utilizing the Real-ESRGAN model to enhance image resolution effectively. Let's dive into the process of image upscaling with Python, making it accessible and straightforward for all skill levels.

Key Points

Understand the concept of image upscaling and its importance.

Learn to install necessary Python packages like basicsr, Real-ESRGAN, Pillow, NumPy, and PyTorch.

Configure the Real-ESRGAN model for image enhancement.

Write Python code to load and apply the upscaling model.

Convert images for compatibility with the model.

Increase the resolution of an image by a factor of four.

Save the upscaled image with improved quality.

Bypass warnings of the package versions.

Introduction to Image Upscaling with Python

What is Image Upscaling?

Image upscaling is the process of increasing the resolution of an image, effectively making it larger. This is particularly useful when you have a low-resolution image that you need to display on a larger screen or use in a high-quality print. Without upscaling, simply enlarging a low-resolution image results in pixelation and a loss of detail. Several techniques exist for image upscaling, ranging from basic interpolation methods to more advanced algorithms that use machine learning to infer missing details.

Why is Image Upscaling Important?

  • Improved Visual Quality: Upscaling enhances the clarity and detail of images, making them more visually appealing.
  • Compatibility with Modern Displays: Modern displays have higher resolutions, requiring images to be upscaled to avoid pixelation.
  • Enhanced Printing Quality: Upscaling improves the quality of printed images, ensuring crisp details.
  • Restoration of Old Images: Upscaling can be used to restore old or damaged images, bringing them back to life.

Traditional methods of image upscaling often rely on interpolation, where new pixels are created based on the colors of surrounding pixels. While these methods are fast, they often result in blurry or artificial-looking images. More advanced techniques, such as those based on deep learning, can produce significantly better results by learning to generate realistic details that are not present in the original image.

In this guide, we'll explore using Python and a powerful model called Real-ESRGAN to achieve high-quality image upscaling.

Why Use Python for Image Upscaling?

Python is a popular choice for image processing tasks due to its ease of use and extensive libraries. It provides a rich ecosystem of tools that make it easy to manipulate images, apply complex algorithms, and work with machine learning models.

Key Advantages of Using Python for Image Upscaling:

  • Extensive Libraries: Python offers libraries like Pillow, NumPy, and PyTorch, which provide powerful image processing and numerical computation capabilities.
  • Machine Learning Support: Python is the go-to language for machine learning, making it easy to integrate advanced upscaling models like Real-ESRGAN.
  • Cross-Platform Compatibility: Python code can run on various operating systems, making it a versatile choice for image upscaling.
  • Large Community Support: A vast community of developers supports Python, providing ample resources, tutorials, and solutions to common problems.

The combination of these factors makes Python an ideal environment for experimenting with and implementing image upscaling algorithms. Whether you're a beginner or an experienced programmer, Python offers the tools and support you need to achieve impressive results.

Setting Up Your Python Environment for Image Upscaling

Installing Required Packages

Before you can start upscaling images, you need to install several Python packages. These packages provide the necessary tools and libraries for image processing, numerical computation, and machine learning. Here's how to install them using pip, the Python package installer.

Open your terminal or command Prompt and run the following command:

pip3 install basicsr Real-ESRGAN pillow numpy torch

Let's break down what each of these packages does:

  • basicsr: Provides basic super-resolution functionalities.
  • Real-ESRGAN: Implements the Real-ESRGAN model for high-quality image upscaling.
  • Pillow: A powerful image processing library that supports a wide variety of image formats.
  • NumPy: A fundamental package for numerical computation in Python.
  • PyTorch: An open-source machine learning framework, crucial for working with the Real-ESRGAN model.

Once you've installed these packages, you're ready to move on to setting up the Real-ESRGAN model.

Downloading the Real-ESRGAN Model Weights

Real-ESRGAN is a deep learning model, requiring pre-trained model weights to function. These weights are stored in a ".pth" file and need to be downloaded separately.

You can download the Real-ESRGAN model weights from the official GitHub repository.

  1. Visit the Real-ESRGAN Releases page.
  2. Look for the RealESRGAN_x4plus.pth file. This file contains the model weights for upscaling images by a factor of 4.
  3. Download the file and place it in your project directory.

By having these model weights, your Python script can load and use the pre-trained model to enhance image resolution effectively.

Step-by-Step Guide to Upscaling Images with Python

Writing the Python Script

Now that you have all the necessary packages and the model weights, you can start writing the Python script to upscale images. Here's a step-by-step guide to help you get started.

1. Import the Required Libraries

Start by importing the required libraries:

import torch
import numpy as np
from PIL import Image
from basicsr.archs.rrdnet_arch import RRDBNet
from realesrgan import RealESRGANer
import warnings
warnings.filterwarnings('ignore')
  • torch: Used for loading and using the PyTorch model.
  • numpy: Used for numerical operations, especially for image data.
  • PIL (Pillow): Used for opening, manipulating, and saving images.
  • basicsr.archs.rrdnet_arch: Imports the RRDBNet architecture, which is used by Real-ESRGAN.
  • realesrgan: Imports the RealESRGANer class, a framework that simplifies using the model.
  • warnings: Used to filter out warning messages for cleaner output.

2. Load the Model Weights

Specify the path to the model weights file and load the model:

model_path = 'RealESRGAN_x4plus.pth'
state_dict = torch.load(model_path, map_location=torch.device('cpu'))['params_ema']
  • model_path: Specifies the path to the RealESRGAN model weights file.
  • state_dict: Loads the model weights into a state dictionary, mapping it to the CPU to avoid GPU dependency. You can modify it to ‘cuda’ if you want to use your GPU

3. Configure the Upscaler

Create an instance of the RealESRGAN upscaler.

model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
model.load_state_dict(state_dict)
model.eval()
upsampler = RealESRGANer(scale=4,model_path=model_path,model=model,tile=0,tile_pad=10,pre_pad=0,half=False)
  • RRDBNet: Creates the model object
  • model.load_state_dict: load the previously loaded parameters.
  • model.eval(): Puts the model into evaluation mode, which turns off gradients
  • RealESRGANer: Sets up the upscaler with the specified parameters.

4. Load and Process the Image

Load the image you want to upscale and convert it to the correct format:

img = Image.open('image.jpg').convert('RGB')
img = np.array(img)
  • Image.open(): Opens the image file.
  • .convert('RGB'): Converts the image to the RGB color space.
  • np.array(): Converts the image to a NumPy array.

5. Upscale the Image

Use the RealESRGAN model to upscale the image:

output, _ = upsampler.enhance(img, outscale=4)
  • upsampler.enhance(): Applies the RealESRGAN model to the image, upscaling it by a factor of 4.

6. Save the Upscaled Image

Save the upscaled image to a file:

output_img = Image.fromarray(output)
output_img.save('new_image.jpg')
  • Image.fromarray(): Converts the NumPy array back to an image.
  • .save(): Saves the upscaled image to a file.

Complete Python Script:

import torch
import numpy as np
from PIL import Image
from basicsr.archs.rrdnet_arch import RRDBNet
from realesrgan import RealESRGANer
import warnings
warnings.filterwarnings('ignore')

# Load the model weights
model_path = 'RealESRGAN_x4plus.pth'
state_dict = torch.load(model_path, map_location=torch.device('cpu'))['params_ema']

# Configure the upscaler
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
model.load_state_dict(state_dict)
model.eval()
upsampler = RealESRGANer(scale=4,model_path=model_path,model=model,tile=0,tile_pad=10,pre_pad=0,half=False)

# Load and process the image
img = Image.open('image.jpg').convert('RGB')
img = np.array(img)

# Upscale the image
output, _ = upsampler.enhance(img, outscale=4)

# Save the upscaled image
output_img = Image.fromarray(output)
output_img.save('new_image.jpg')

Explanation

  • The script imports required libraries and classes.
  • The model_path variable saves a string for where the .pth is located
  • The script has some configurations for running correctly, and applies upsampler.enhance to output to run the upscaling.
  • This script combines the necessary steps to upscale an image using Real-ESRGAN, providing a clear and effective solution for image enhancement.

Pricing of Real-ESRGAN

Real-ESRGAN Model and Framework

The Real-ESRGAN model and the basic framework for using it are typically available for free. Real-ESRGAN is an open-source project, and the model weights and source code can be downloaded without any cost.

However, costs may arise in the following scenarios:

  • Compute Resources: If you don't have a dedicated GPU, you might need to use cloud-based services like Google Colab, AWS, or Azure to run the model, which can incur usage-based costs.
  • Commercial Licenses: For commercial use, you might need to check licensing terms to ensure compliance.
  • Custom Implementations: If you hire developers to create custom implementations or integrate Real-ESRGAN into a commercial application, development costs would apply.

Here's a simplified pricing overview:

Item Cost Details
Real-ESRGAN Model Weights Free Downloadable from the official GitHub repository.
Basic Framework Free Open-source code available for personal and research use.
Cloud-based GPU Services Usage-based Costs depend on the service provider and usage duration.
Commercial License Varies Check terms and conditions for commercial use; might require a license depending on the specific application.
Custom Development Varies Depends on the scope and complexity of the implementation.

By understanding these factors, you can effectively plan and manage the costs associated with using Real-ESRGAN for image upscaling.

Advantages and Disadvantages of Using Real-ESRGAN

👍 Pros

Excellent Image Quality: Generates realistic details and reduces common upscaling artifacts.

Versatile Application: Suitable for upscaling various types of images.

Open-Source: Free to use, making it accessible to a wide range of users.

Cross-Platform: Works on different operating systems due to Python's compatibility.

👎 Cons

Resource-Intensive: Requires significant computational resources, especially GPUs.

Setup Complexity: Requires installation of multiple Python packages and downloading model weights.

Longer Processing Time: Can take longer to upscale images compared to simpler methods.

Package Incompatibility: May encounter some warnings due to package versions

Core Features of Real-ESRGAN

Key Capabilities of Real-ESRGAN

Real-ESRGAN is designed to enhance image quality and produce high-resolution images from low-resolution inputs. Here are its core features:

  • High-Quality Upscaling: It can significantly improve the visual quality of images, making details clearer and more defined.
  • Realistic Detail Generation: The model is trained to infer and generate realistic details that are not present in the original image, avoiding artificial-looking results.
  • Artifact Reduction: Real-ESRGAN reduces common upscaling artifacts such as blurring and pixelation, providing a more natural appearance.
  • Versatile Application: Suitable for upscaling various types of images, including photos, illustrations, and more.
  • Scalability: The model can upscale images by different factors, typically ranging from 2x to 4x or higher, depending on the specific implementation and configuration.

Here’s a summary table:

Feature Description
High-Quality Upscaling Enhances image clarity and detail.
Detail Generation Infers and generates realistic image details.
Artifact Reduction Reduces common upscaling artifacts like blurring and pixelation.
Versatile Application Suitable for various types of images.
Scalability Can upscale images by different factors (2x, 4x, etc.).
Enhanced Facial Features Optimizes for enhancing facial features, making it suitable for portraits and images with people.
Noise Handling Designed to handle and reduce noise in the input images, improving overall visual quality.
Fine-Tuning Supports fine-tuning on custom datasets, allowing adaptation to specific types of images and application scenarios.

With its impressive capabilities, Real-ESRGAN is a go-to choice for high-quality image upscaling in various domains.

Practical Use Cases for Image Upscaling

Applications Across Various Industries

Image upscaling has a wide array of applications across various industries. Here are some practical use cases:

  • Photography: Enhancing low-resolution photos for printing or digital display, improving overall image quality.
  • Digital Art: Upscaling digital paintings and illustrations to higher resolutions for detailed viewing and printing.
  • Video Games: Improving the texture resolution of old games or creating higher-quality assets for modern games.
  • Medical Imaging: Enhancing the resolution of medical scans (e.g., X-rays, MRIs) to aid in diagnosis and analysis.
  • Forensic Science: Upscaling surveillance footage to identify details and improve the clarity of evidence.
  • E-commerce: Enhancing product images for online stores to attract customers with high-quality visuals.
  • Historical Image Restoration: Restoring old and damaged photos to preserve and display historical moments.

The following table highlights these use cases in more detail:

Industry Use Case
Photography Enhancing photos for printing and display.
Digital Art Upscaling artwork for high-resolution viewing.
Video Games Improving texture resolution.
Medical Imaging Enhancing scans for diagnosis.
Forensic Science Improving surveillance footage clarity.
E-commerce Enhancing product images for online stores.
Historical Images Restoring old and damaged photos.
Film and TV Upscaling old film footage for modern displays.
Real Estate Enhancing property images for listings.
Security Improving the clarity of security camera footage for better identification.

By leveraging image upscaling, professionals can enhance visual content and improve various workflows.

Frequently Asked Questions (FAQ)

What is Real-ESRGAN?
Real-ESRGAN stands for Enhanced Super-Resolution Generative Adversarial Network. It's a deep learning model designed to upscale images, creating high-resolution outputs from low-resolution inputs. It excels at generating realistic details and reducing common upscaling artifacts.
Why is Python a good choice for image upscaling?
Python offers an extensive ecosystem of libraries such as Pillow, NumPy, and PyTorch, making it easy to manipulate images, apply complex algorithms, and work with machine learning models. It’s cross-platform and has a large community for support.
Where can I download Real-ESRGAN model weights?
You can download the Real-ESRGAN model weights from the official GitHub repository in the “Releases” section. Look for the ".pth" file.
How do I install the required Python packages?
You can install the required packages using pip, the Python package installer. Open your terminal or command prompt and run the following command: pip install basicsr Real-ESRGAN pillow numpy torch
What is the RRDBNet architecture?
RRDBNet stands for Residual in Residual Dense Block Network. It's a type of convolutional neural network (CNN) architecture used in Real-ESRGAN to generate realistic details while upscaling images. The residual connections help in training deeper networks.
Can I use Real-ESRGAN on my CPU?
Yes, you can run Real-ESRGAN on your CPU, but it will be slower than using a GPU. To run it on the CPU, set the device to 'cpu' in your code: torch.device('cpu').
Is Real-ESRGAN free to use?
Yes, the Real-ESRGAN model and the basic framework for using it are typically available for free. It's an open-source project, and the model weights and source code can be downloaded without any cost.
What are some practical applications of image upscaling?
Practical applications include enhancing photos for printing, upscaling artwork for high-resolution viewing, improving texture resolution in video games, enhancing medical scans, improving surveillance footage, and restoring historical images.

Related Questions

How does Real-ESRGAN compare to other image upscaling techniques?
Real-ESRGAN utilizes deep learning to infer missing details, significantly outperforming traditional interpolation methods in terms of quality. Compared to other deep learning models, Real-ESRGAN is specifically designed to generate more realistic details and reduce common upscaling artifacts. This involves creating a residual in residual dense block to enhance performance.
What are the hardware requirements for running Real-ESRGAN?
Real-ESRGAN can run on both CPUs and GPUs, but GPUs offer significantly faster performance. A GPU with at least 4GB of VRAM is recommended for large images. If using a CPU, ensure you have sufficient RAM to handle the image processing. While a GPU provides faster computation, the Real-ESRGAN model also functions with the CPU, though it might be slower. High amounts of RAM and processing power are needed for handling the image processing if running with the CPU. Ensure you meet minimum requirements when upscaling the images to enhance performance.
How can I fine-tune Real-ESRGAN for specific types of images?
To fine-tune Real-ESRGAN for specific types of images, you need a dataset of high-resolution images and their corresponding low-resolution versions. Use this dataset to train the Real-ESRGAN model, adjusting the model’s parameters to better fit the characteristics of your specific image type. Fine-tuning can significantly improve the quality of upscaled images for particular use cases. To fine-tune real-ESRGAN models, using custom datasets, allows them to adapt and optimize with specific image types and produce great results.
What image formats does Real-ESRGAN support?
Real-ESRGAN, particularly when used with Python's Pillow library, supports a wide range of image formats, including JPEG, PNG, TIFF, BMP, and GIF. Ensure the images are converted to a suitable format, such as RGB, before processing to ensure compatibility. Real-ESRGAN supports a variety of image formats but ensure the image is converted to RBG for maximum compatibility.

Most people like