ModuleNotFoundError: No module named ‘Image’ in Python

You receive the error message “ModuleNotFoundError: No module named ‘Image'” in Python because you imported the Image module incorrectly. Do not miss this article; we will give you a few methods to overcome the error. Let’s start first by deeply discovering the error.

Reason for the error “ModuleNotFoundError: No module named ‘Image'” in Python

The root of the problem is that you make a mistake when importing the Image module. The Image module is a module from the Pillow package. As a result, if you just import the Image module without the Pillow package, you will get the error. This is an example that shows importing the Image package incorrectly and causes the error:

Code:

import Image
from rembg import remove
 
# Define paths to open and save the image
inputPath = "harris-hawk.jpg"
outputPath = "non-background-hawk.png"
 
# Read the image
readImage = Image.open(inputPath)
 
# Remove the background
outImage = remove(readImage)
 
# Save the image
outImage.save(outputPath)
 
# Open the image
with Image.open("non-background-hawk.png") as im:
    im.show()

Result:

Traceback (most recent call last):
  line 1, in <module>
    import Image
ModuleNotFoundError: No module named 'Image'

Import the Image module correctly

It is easy to eliminate the error; you just need to import the Pillow package (PIL) and then import the Image module. The correct syntax is:

from PIL import Image

Code:

from PIL import Image
from rembg import remove
 
# Define paths to open and save the image
inputPath = "harris-hawk.jpg"
outputPath = "non-background-hawk.png"
 
# Read the image
readImage = Image.open(inputPath)
 
# Remove the background
outImage = remove(readImage)
 
# Save the image
outImage.save(outputPath)
 
# Open the image
with Image.open("non-background-hawk.png") as im:
    im.show()

Result:

The piece of code above is to remove the background of the image. The responsibility of the Image module is to read the target image, and the “rembg” package provides the “remove” module to remove the background.

In the example above, we follow the following steps: 

  1. We create 2 paths to read the original image and save the image after removing the background.
  2. We use the Image module to read the image.
  3. We eliminate the background by the “remove” module and save the new image to the output path.
  4. To check the result, we can open the image on the IDE directly. 

This is the link to our simple project on Github. Take a visit to see more details and try it yourself. 

Summary

In summary, the error “ModuleNotFoundError: No module named ‘Image'” in Python occurs when you import the Image module incorrectly. The correct syntax is “from PIL import Image” instead of “import Image”. We hope you understand the problem of getting rid of a similar error in the feature.

Leave a Reply

Your email address will not be published. Required fields are marked *