Python - Loading Image



Description

A image can be loaded and displayed through the program.In this example, we are going to load the image into our program namespace and will display the image.
Let us take the below image and will try to load the image.

rose

First import the Image module and load the image with open method in Image.Make sure to give the complete path of the image where it has been placed.The image name can only be given if the image resides in the same program directory.
# Import Pillow:
from PIL import Image
# Load the original image:
img = Image.open("initial_image.jpg")

Save the image file with save method from Image module.
img.save("final_image.jpg")

Image can be displayed along with saving the modified image.
img.show("final_image.jpg")

Syntax

Following is the syntax of functions been used.
# Import Pillow:
from PIL import Image
# Load the original image:
object = Image.open(image)
object.save(imaege)

Parameters

For the open method we will be passing the name of the image.
Note:If image is in same directory where your python program resides, you can give the image name itself. If the image resides in some other directroy, you are supposed to define the complete path of the image.

Return value

The image will be displayed first and then saved to the directory.

Example

The following example shows to open the image, display the image and saving it.
# Import Pillow:
from PIL import Image
# Load the original image:
image = "initial_image.jpg" img = Image.open(image)
img.show(image)
img.save("final_image.jpg")

Output


scenario


Advertisements