Record (write) PGM image file in P2 format (ASCII)

1

Is there a python command -opencv to write in PGM P2 (Ascii) format ?.  By default, python-opencv records it in binary (P5)

    
asked by Juan Carroza 07.04.2018 в 23:24
source

1 answer

0

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 ...

    
answered by 07.04.2018 в 23:59