Effortless AI Image Upscaling Without a GPU
Written on
Chapter 1: Introduction to AI Image Upscaling
In this article, I'll demonstrate how straightforward it is to develop a Python script that utilizes AI for image upscaling.
This piece serves as a continuation of my earlier article on generating AI images on your local machine. By using the script provided here, you can enhance AI-generated images to resolutions reaching 4096x4096, employing a scale factor of 8x. This guide assumes you have Python and PyCharm set up on your machine.
Section 1.1: Setting Up a New PyCharm Project
To begin, let's create a new PyCharm project titled 'AIUpscale.' As illustrated below, we should enable the option to create a main.py welcome script, which will generate an appropriate Run configuration for us automatically.
The resulting project structure should resemble this:
The autogenerated main.py file
Section 1.2: Installing Required Libraries
We will be utilizing an external library to facilitate our AI upscaling through the ESRGAN AI model. The following GitHub project provides the necessary resources:
To install the library, run the following command using the terminal in PyCharm:
Chapter 2: Crafting the Upscaling Script
Now, we can create the script that will accept a low-resolution image as input, upscale it using the AI model, and save the enhanced image. Here's a breakdown of the code:
import torch
from PIL import Image
from RealESRGAN import RealESRGAN
# Set device to CPU
device = torch.device('cpu')
# Load model and define scale factor
model = RealESRGAN(device, scale=4)
model.load_weights('weights/RealESRGAN_x4.pth', download=True)
# Load the input image
path_to_image = 'inputs/lr_image.png'
image = Image.open(path_to_image).convert('RGB')
# Perform image upscaling
sr_image = model.predict(image)
# Save the upscaled image
sr_image.save('results/sr_image.png')
In this script:
- We choose the CPU as our processing device to show that upscaling can be accomplished without a GPU.
- The model weights are set to auto-download upon the first execution. We're using a scale factor of 4, which means the original image will be enlarged by 4x, with other options being 2 and 8.
- The input image is loaded and converted to RGB format for proper processing by the AI model.
- We execute the upscale using the predict method and save the resulting image to disk.
Here's how the script appears in your PyCharm project:
The script code on PyCharm
Section 2.1: Running the Script
You can run the script by clicking the play button in the PyCharm interface. Initially, the script will download the necessary weights:
Downloading the weights to run our model
Afterward, the script will process the image. During execution, you may see a warning message:
Warning message about using GPU when a GPU is not installed
This warning arises due to a bug in the external library, even though we've explicitly set the script to operate on the CPU. I've submitted a pull request to address this issue.
Despite the warning, the script will execute correctly, and within 1 to 2 minutes—depending on your CPU's capabilities—the output image will be saved.
Section 2.2: Reviewing the Results
For demonstration, I used the following input image:
Input image with a resolution of 512x512
The upscaled output image, enhanced by a factor of 4x, is:
Output image with a resolution of 2048x2048
As demonstrated, the AI model not only increases the resolution but also enhances the clarity of the image.
Chapter 3: Conclusion
This article illustrated the simplicity of creating a script to upscale images with AI. The external library we utilized not only enlarges the image but also enhances its sharpness and overall appeal.
You can find the source code for this script, along with the images, at: