I have a group of RGBA images saved in a folder, my goal is to convert these images into another folder in a pgm format, below is the code:
path1 = file/path/where/image/are/stored
path2 = file/path/where/pgm/images/will/be/saved
list = os.listdir(path1)
for file in listing:
#Transforms an RGBA with channel into an RGB only
image_rgb = Image.open(file).convert('RGB')
#Color separation stains to detect microscopic cells
ihc_hed = rgb2hed(image_rgb)
#Trasnforms the image into a numpy array of the UINT8 Type
cv_img = ihc_hed.astype(np.uint8)
# create color boundaries boundaries detecting black and blue stains
lower = np.array([0,0,0], dtype = "uint8")
upper = np.array([0,0,255], dtype = "uint8")
#calculates the pixel within the specified boundaries and create a mask
mask = cv2.inRange(cv_img, lower, upper)
img = Image.fromarray(mask,'L')
img.save(path2+file,'pgm')
however I get an error stating KeyError: 'PGM', it seems that the 'pgm' format is not in the modes
Thanks for the advice :)
As far as I can see scikit image uses the Python Imaging Library plugin for saving image files. PIL does not support PGM.
Refer to http://effbot.org/imagingbook/decoder.htm for how to write your own file decoder for PIL.
Extract: