Save image to disk from a NumPy array

1

I have the following exercise:

  

a. Write a function that implements the gamma transformation of a grayscale image. The    function must receive as input parameters: a grayscale image (uint8) and the parameter    gamma. The output parameter of the function must be a grayscale image (uint8). For Υ    avoid losing precision in the calculation of gray level values, convert the image to double    before calculating the gamma transformation. The image must be converted back to uint8 before    return from the function.

     

b. Select a color image and apply gamma transformation   for Y = 0.05, 0.10, 0.20, 0.50, 1, 1.5.2.5, 5.0, 10.0, 25.0.

This is a point of the Artificial Vision workshop, what happens is that I can see the image in the terminal, but I need the image to be saved on my PC and I do not know why it does not save me.

This way I have the code but I do not know what I would miss, or in what error:

from PIL import Image, ImageOps #importe de libreria PIL
import numpy as np #importe de libreria numpy
import matplotlib.pyplot as plt #importe de libreria matplotlib
import sys #improte de libreria SYS

sys.path.append('D:\Vision')  #la ruta de los archivos USB
import tranformacion_lineal as trans
Im_g = Image.open('bears.jpg').convert('L')

Im_ga = np.double(np.array(Im_g)) #Transofrmacion de la imagen a DOUBLE
Im2 = trans.my_gamma( Im_ga, 0.05) #Aca se manda la imagen  y el valor para el exponente  al metodo para Y=0.05, 0.10, 0.20, 0.50, 1, 1.5,2.5, 5.0, 10.0, 25.0.
plt.gray()

plt.imshow(np.uint8(Im_ga)) # esta linea no es necesaria ya se esta transformado en my_gamma
plt.axis('off')
plt.figure()

plt.gray()
plt.imshow(Im2)
plt.axis('off')

Im_g = Image.save('D:\Vision')
    
asked by Basilio Saldarriaga 27.08.2018 в 15:32
source

1 answer

3

You try to use the correct method, Image.save , but not in the correct way. It is an instance method of the class Image that is part of the module PIL.Image and must therefore be used on an instance of it, such as the one that returns PIL.Image.open .

In your case, when doing Image.save('D:\Vision') , you try to use the function save of the module PIL.Image , a function that does not exist, instead of the instance method save of the class PIL.Image.Image .

Since you have the image to save in an array of NumPy you must use PIL.Image.fromarray , which returns an instance of PIL.Image.Image to later use its save method:

import numpy as np
from PIL import Image



img = Image.open("bears.jpg").convert("L")
im_ga = np.double(np.array(img)) 
im_fin = np.uint8(im_ga)

res_img = Image.fromarray(im_fin, mode="L")
res_img.save("D:/Vision/result.jpg")
  

Note: You should never use the inverse slash ( \ ) without escaping in chains representing a route, that character is used to specify escape sequences ( \n , \r , \t , \U , etc) so it causes errors when interpreting the path or syntax errors directly. Instead, scan it ( "D:\Vision\bears.jpg" ) or use the forward slash that is universal: "D:/Vision/bears.jpg" . Using raw strings ( r"D:\..." ) is also possible, but in certain situations they can also cause problems if the path is not normalized first ( os.path.normpath ).

    
answered by 27.08.2018 / 17:23
source