[Solved] OSError: cannot write mode RGBA as JPEG

In this tutorial we will look at one of the most common error in python OSError: cannot write mode RGBA as JPEG. OSError is a built-in exception in Python. It serves as the error class for os module in Python. Whenever any os related error occur, this error class is raised. There are many os related error like ‘file not found’, ‘disk full’ etc. RGBA is one of the OSError which is raised whenever there is mismatch with the file format of the image. We will understand this error much more in detail and look at the fix in the upcoming sections.

 

[Solved] OSError: cannot write mode RGBA as JPEG

Understanding RGBA Error in Python

Also read: How to Install Flask in VS Code and Build REST API : [ 9 Easy Steps]

The error message “OSError: cannot write mode RGBA as JPEG” occurs in Python when you attempt to save an image with RGBA color mode as a JPEG image. This error occurs due to a mismatch between the color mode of the image and the format you are trying to save it in. In this tutorial we will reproduce the error and then we will fix the error using appropriate file format. We use a Python package called ‘rembg’ to remove the background from images. Let us understand basic about this package in the next section.

 

Python rembg Package Overview

The ‘rembg’ package in Python is designed for removing the background from images, leaving the subject of the image isolated. It is a tool that employs deep learning techniques, specifically a pre-trained neural network, to perform this task accurately. The name ‘rembg’ is short for ‘remove background’.

There are many usage of rembg like it is used in Image editing and manipulation, Graphic design, Content creation, Automated image processing etc.

 

[Solved] OSError: cannot write mode RGBA as JPEG

Now that we have basic understanding of the error and the rembg package. Let us now look at an example where we will reproduce the issue and then fix it in the code.

 

Example: Image Background Remover

In the below example, I  have downloaded an image which by default gets saved in ‘.jpg’ format. Change the format and save the  file in ‘.png’ format. I have saved it as ‘input_flower.png’. You can download the image used in this example from here.

Next, I  have imported ‘remove’ function from ‘rembg‘ package.  Using this function we will remove the background from the image.
I have imported ‘Image’ function from ‘PIL’ package which will  be used to read and write the image file in the given path.
‘If’ condition will check if  image already exist in the given path without background. If so, it will print the message and exit the code with status 0. If output image does not exist, it will create. If there are no error, it will print the completion message and exit the code. Let us first save the output file in ‘.jpg’ format as shown below. Below is the code given.

from rembg import remove
from PIL import Image
import os

input_path = "C:\\Users\\linuxnasa\\Desktop\\python_projects\\input_flower.png"

#Correct file format is output_flower.png
output_path = "C:\\Users\\linuxnasa\\Desktop\\python_projects\\output_flower.jpg"

if  os.path.exists(output_path):
    print("\nConverted file already exist in path:", output_path)
    exit(0)

try:
    image = Image.open(input_path)
    output = remove(image)
    output.save(output_path)
    print("\nBackground removal completed successfully. Output saved to: ", output_path)

except Exception as e:
    print("\nAn error occurred during background removal:", str(e))

 

When you execute the code, you will see below error.
OUTPUT
PS C:\Users\linuxnasa\Desktop\python_projects> python .\image-bg-change.py
An error occurred during background removal: cannot write mode RGBA as JPEG

 

To fix this, change the file format of ‘output_flower.jpg’ to ‘output_flower.png’. Again execute the code. This time it will process the file and remove the background from the image as shown below.

OUTPUT
PS C:\Users\linuxnasa\Desktop\python_projects> python .\image-bg-change.py
Downloading data from 'https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx' to file 'C:\Users\coder\.u2net\u2net.onnx'.
100%|###############################################| 176M/176M [00:00<?, ?B/s]

Background removal completed successfully. Output saved to: C:\Users\linuxnasa\Desktop\python_projects\output_flower.png

 

Summary

Read more about rembg package from  pypi.org

 

 

Leave a Comment