You are currently viewing How to Read and Convert PGM Image in Python

How to Read and Convert PGM Image in Python

A PGM file is typically used to save black-and-white scanned images. It saves RGB color information in 24-bits per pixel and supports 8-bit grayscale images as well. It can store thousands of pixels shades in the image. We can easily read these images using well-known python imaging libraries. In this article, we will utilize the OpenCV library. You can easily install this library using !pip install opencv-python in your environment. You can also use other libraries like Pillow, Imageio, Matplotlib, and Skimage to read PGM image in Python. Let’s start

A PGM file is typically used to save black-and-white scanned images. It saves RGB color information in 24-bits per pixel and supports 8-bit grayscale images as well. It can store thousands of pixels shades in the image. We can easily read these images using well-known python imaging libraries. In this article, we will utilize the OpenCV library. You can easily install this library using !pip install opencv-python in your environment. You can also use other libraries like Pillow, Imageio, Matplotlib, and Skimage to read PGM image in Python. Let’s start

Read PGM Image in Python

# Required Libraries
import cv2

# Location of the image

img_path = 'breast_cancer_image.pgm'
img = cv2.imread(img_path)

# Print Image Arrays
print(img)

Output:

[[[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]
 ...
[[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]]

Show PGM Image in Python

Now our PGM image is in NumPy array format. There are multiple ways to show this array in an image. Here, we will use the pyplot module from the Matplotlib python library.

# Required Library
from matplotlib import pyplot as plt

# Plot Image using Pyplot
plt.imshow(img)

Output:

Convert PGM to JPG Image in Python

The normal image viewers do not show the PGM image. So, you can easily convert images from PGM to JPG or any other common format. cv2.imwrite the function helps us to write the image into our desired format. Let’s code

cv2.imwrite('output.png', img)

Output:

True

Convert All Images from PGM to PNG in the Folder

If you have thousands of images in a folder and want to convert it from PGM to PNG format or any other common format at once. Then, you can follow the complete code.

import os
import cv2

# Dataset Folder Path
img_folder = '../input/mias-mammography/all-mias/'

# Destination Folder Path where you want to save Converted Images
output_folder = './output_folder/'

# Create a new folder if not exist
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# Loop through each file in the folder with specific file extension .pgm
# Read the image in NumPy array using CV2
# Write the array into desired image format

for file in os.listdir(img_folder):
    if file.endswith(".pgm"):
        im = cv2.imread(img_folder+file)
        cv2.imwrite(output_folder+file[3:-4]+'.jpg', im)
        print(file)

Leave a Reply