Ask Your Question
0

Save an image in pgm format + Python

asked 2013-09-01 14:02:18 -0600

casbelt gravatar image

updated 2013-09-01 14:14:09 -0600

berak gravatar image

Hi everyone, I've got a function that return an image. I would like to save this image but changing the format to pgm. How could I do that? Thanks in advance.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2013-09-01 14:18:46 -0600

berak gravatar image

updated 2013-09-01 14:42:02 -0600

writing the ppm file is quite easy:

cv2.imwrite("my.ppm",img)


i have no idea, what function_that_return_an_image() actually returns.

if your "image" is a in-memory representation of an image-file ( not plain pixels ), you have to decode it before:

img = cv2.imdecode(input,1)

edit flag offensive delete link more

Comments

Hi berak, thanks for your answer. I read about "imwrite" in the doc but it doesn't work on my code.

I'm using the same function that in this link (Aligining_faces_immages ---> CropFace): http://docs.opencv.org/modules/contrib/doc/facerec/facerec_tutorial.html?highlight=crop%20images#aligning-face-images

In the end of the example the image is saved with "save" but I would like to change the extension to .pgm ¿Is it possible?

casbelt gravatar imagecasbelt ( 2013-09-02 02:38:06 -0600 )edit
1

oh, i see.

the code in your link is is not using opencv(cv2) at all, but Image ( the PIL library ) instead.

(but even there it should be possible, just to change it from "arnie.jpg" to "arnie.pgm", no ? )

berak gravatar imageberak ( 2013-09-02 02:44:30 -0600 )edit

Surprisingly just changing the extension in the name works haha. Thank you berak.

casbelt gravatar imagecasbelt ( 2013-09-02 03:10:27 -0600 )edit

hey, pleasure.

berak gravatar imageberak ( 2013-09-02 03:12:01 -0600 )edit

Question Tools

Stats

Asked: 2013-09-01 14:02:18 -0600

Seen: 6,554 times

Last updated: Sep 01 '13