Is there a python command -opencv to write in PGM P2 (Ascii) format ?. By default, python-opencv records it in binary (P5)
Is there a python command -opencv to write in PGM P2 (Ascii) format ?. By default, python-opencv records it in binary (P5)
You must use the CV_IMWRITE_PXM_BINARY
parameter assigning it the correct value. If the value is 1 (default) the format will be binary ( P5
), if the value is 0 it will be text format ( P2
). You can see the documentation of cv.imwrite
:
For PPM, PGM, or PBM, it can be a binary format flag (CV_IMWRITE_PXM_BINARY), 0 or 1. Default value is 1.
So you should do something like this:
cv2.imwrite("output.pgm", img, (cv2.IMWRITE_PXM_BINARY, 0))
If we open the file we can see how it is indicated as an ASCII PGM in the header:
P2
154 36
255
255 255 255 255 255 ...