How to Convert JPG to EXR Using Python
Converting image formats is a common task in digital workflows, especially when working with high dynamic range (HDR) images. The EXR (OpenEXR) format is widely used in visual effects and animation due to its ability to store high-quality, high-bit-depth images. If you need to convert a standard JPG to EXR using Python, this guide will walk you through the process step by step.
Prerequisites
Before we begin, ensure you have the following installed:
- Python 3.6 or later (recommended)
- Pillow (PIL Fork) – A popular Python imaging library
- OpenEXR – A Python module for handling EXR files
You can install the required libraries using pip:
pip install Pillow OpenEXR
Step-by-Step Conversion Process
1. Load the JPG Image
First, we'll use Pillow to open and read the JPG image:
from PIL import Image
# Load the JPG image
jpg_image = Image.open("input.jpg")
2. Convert the Image to RGB Mode
EXR files typically store data in RGB format. Ensure the image is in the correct mode:
if jpg_image.mode != "RGB":
jpg_image = jpg_image.convert("RGB")
3. Prepare the EXR File
We'll use the OpenEXR module to create an EXR file. This requires converting the image data into a format compatible with EXR.
import numpy as np
import OpenEXR
import Imath
# Convert the image to a numpy array
image_array = np.array(jpg_image, dtype=np.float32) / 255.0
# Extract RGB channels
red = image_array[:, :, 0].tostring()
green = image_array[:, :, 1].tostring()
blue = image_array[:, :, 2].tostring()
# Define the EXR header
header = OpenEXR.Header(jpg_image.width, jpg_image.height)
header['channels'] = {
'R': Imath.Channel(Imath.PixelType(OpenEXR.FLOAT)),
'G': Imath.Channel(Imath.PixelType(OpenEXR.FLOAT)),
'B': Imath.Channel(Imath.PixelType(OpenEXR.FLOAT))
}
4. Save as EXR
Finally, write the image data to an EXR file:
# Create the EXR file
exr_file = OpenEXR.OutputFile("output.exr", header)
# Write the channels
exr_file.writePixels({
'R': red,
'G': green,
'B': blue
})
exr_file.close()
Complete Code Example
Here’s the full script for converting JPG to EXR:
from PIL import Image
import numpy as np
import OpenEXR
import Imath
def jpg_to_exr(input_path, output_path):
# Load JPG image
jpg_image = Image.open(input_path)
# Convert to RGB if needed
if jpg_image.mode != "RGB":
jpg_image = jpg_image.convert("RGB")
# Convert to numpy array
image_array = np.array(jpg_image, dtype=np.float32) / 255.0
# Extract channels
red = image_array[:, :, 0].tostring()
green = image_array[:, :, 1].tostring()
blue = image_array[:, :, 2].tostring()
# Create EXR header
header = OpenEXR.Header(jpg_image.width, jpg_image.height)
header['channels'] = {
'R': Imath.Channel(Imath.PixelType(OpenEXR.FLOAT)),
'G': Imath.Channel(Imath.PixelType(OpenEXR.FLOAT)),
'B': Imath.Channel(Imath.PixelType(OpenEXR.FLOAT))
}
# Write EXR file
exr_file = OpenEXR.OutputFile(output_path, header)
exr_file.writePixels({'R': red, 'G': green, 'B': blue})
exr_file.close()
# Example usage
jpg_to_exr("input.jpg", "output.exr")
Conclusion
Converting a JPG to EXR in Python is straightforward using Pillow and OpenEXR. This method ensures high-quality conversion while preserving the necessary dynamic range for professional workflows. Whether you're working on VFX, animation, or HDR imaging, this script provides a reliable way to handle format conversions.
- How to convert JPG to EXR using Python
- Best Python library for EXR conversion
- Convert JPG to HDR EXR format
- Python script for JPG to EXR conversion
- OpenEXR Python example code
- High-quality image conversion Python
- Batch convert JPG to EXR in Python
- Working with EXR files in Python
- How to save images as EXR in Python
- Python image processing for VFX
- Convert standard image to HDR EXR
No comments:
Post a Comment