Jürgen K. Jürgen K. - 1 month ago 23
Python Question

Saving image in python

I'm new to python. What I want to do is to read in an image, convert it to gray value and save it.

This is what I have so far:

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
args = vars(ap.parse_args())

#Greyvalue image
im = Image.open(args["image"])
im_grey = im.convert('LA') # convert to grayscale


Now my problem is how to save it. As far as I unterstood there are a lot of different modules (Using python 2.7) Could someone give my an example?

Thanks

Answer

Method 1: save method

im_grey.save('greyscale.png')

Use Image_object.save() method

Method 2: imsave method

import matplotlib.image as mpimg
mpimg.imsave("greyscale.png", im_grey)