正確な例を使用したいくつかの背景概念:
# 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
参照
これで、PPMファイルを適切に書き換えたことがわかります(カラー画像の各ピクセルに対してRGBトリプレットが考慮されているため)
ファイルを開いて視覚化する
OpenCV(素晴らしい仕事をします)
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)
枕(またはPILと呼びます)
.ppm
ドキュメントには、次を使用してファイルを直接開くことができると記載されていますが、
from PIL import Image
img = Image.open("path_to_file")
参照
ただし、さらに詳しく調べると、バイナリバージョン(PPMの場合はP6と呼ばれる)のみがサポートされ、ASCIIバージョン(PPMの場合はP3と呼ばれる)はサポートされていないことがわかります。
参照
したがって、PILを使用するユースケースでは、理想的なオプションではありません❌。
を使用した視覚化の利点はmatplotlib.pyplot.imshow()
、上記のように当てはまります。