This python script is designed to batch upscale historic images, with the option to colorize the image as well. The script uses the Wand and Pillow libraries to process images. It also uses the Replicate library to access pre-trained machine learning models in the cloud for upscaling and colorization.
- Uses AI to colorize (model: bigcolor) and upscale (model: real-esrgan) old photos
- Utilizes paid replicate.com API for cloud computing
- Built with Python 3.9.6
Original Image - Source
Processed Image - Script Result
This year I wanted to gift my grandma a couple of historic images of her hometown from the time when she grew up. After curating about 130 photos I had the idea of using AI to enhance the low-resolution black-and-white photos. That's why I quickly threw together this script to batch-process the folder of images.
- Install requirements:
pip3 install -r requirements.txt
- Install ImageMagick via terminal (example macOS):
brew install imagemagick
- Set replicate.com token with the terminal (example macOS):
export REPLICATE_API_TOKEN=[token]
(this is recommended for not having to provide the key every time the script runs) - Run the script via the terminal like:
python3 script.py
Run in the terminal, and afterward restart the terminal:
brew install imagemagick
echo 'export MAGICK_HOME=/opt/homebrew/opt/imagemagick/' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/imagemagick/bin:$PATH"' >> ~/.zshrc
The script will output the processed image to a file with the same name as the input image, but with "_upscaled" or "_upscaled_colorized.jpeg" added to the filename. For example, if the input image is "image.png", the output image will be "image_upscaled_colorized.jpeg".
- The replicate.com API is not free, from my experience it costs about 1 USD for processing 200 images
- The colorization might not be very accurate. Landscapes and cities work reasonably well, but the AI model seems to struggle a bit with people. (this probably could be improved by manually cherry-picking the best of the five image variants that the bigcolor model produces)
- Currently, the script only supports JPG and PNG images as input (this probably could be extended quite easily)