Some background concept using your exact example:
.ppm
is one of the file formats in which image data is stored so that it is more human readable.
It stands for Portable PixMap format
These files are usually of the following format:
# Optional Comments likes this one
# The first line is the image header which contains the format followed by width and height
P3 7 1
# Second line contains the maximum value possible for each color point
255
# Third line onwards, it contains the pixels represented in rows(7) and columns(1)
0 0 0
201 24 24
24 201 45
24 54 201
201 24 182
24 201 178
104 59 14
Reference
So you can see that you have properly rewrite your PPM file (since RGB triplets are considered for each pixel in a color image)
Opening and visualizing the file
OpenCV (Does a fantastic job)
import cv2
import matplotlib.pyplot as plt
img = cv2.imread("\path to the image")
# Remember, opencv by default reads images in BGR rather than RGB
# So we fix that by the following
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
# Now, for small images like yours or any similar ones we use for example purpose to understand image processing operations or computer graphics
# Using opencv's cv2.imshow()
# Or google.colab.patches.cv2_imshow() [in case we are on Google Colab]
# Would not be of much use as the output would be very small to visualize
# Instead using matplotlib.pyplot.imshow() would give a decent visualization
plt.imshow(img)
Pillow (or as we call it as PIL)
Although the documentation states that we can directly open .ppm
files using:
from PIL import Image
img = Image.open("path_to_file")
Reference
However, when we inspect further we can see that they only support the binary version (otherwise called P6 for PPM) and not the ASCII version (otherwise called P3 for PPM).
Reference
Hence, for your use case using PIL would not be an ideal option❌.
The benefit of visualization using matplotlib.pyplot.imshow()
shall hold true as above.