I was thinking of doing it with opencv, The pillow is more like a beginner, so try it first.
png_to_pbm.py
#Enable when displaying with jupyter.
#%matplotlib inline
from PIL import Image
import os
import matplotlib.pyplot as plt
file_name = 'piet.png'
#Open image file
img = Image.open(file_name)
#Convert to 1bit? Meaning of?
img_pbm = img.convert('1')
img_pbm.save('piet.pbm')
plt.imshow(img_pbm)
#'L'Then it becomes grayscale
#img_pgm = img.convert('L')
#'RGB'Then it becomes RGB
#img_ppm = img.convert('RGB')
If you want to resize, follow below.
png_to_pbm.py continued
lf_img_resize = lf_img.resize((512,128))
lf_img_resize.save('lf_img_resize.pbm')
plt.imshow(lf_img_resize)
Recommended Posts