Page 1 of 1

Converting to PGM to stdout from TIF file does not result in PGM output

Posted: 2019-04-02T11:19:07-07:00
by argan
I am confused about how to convince "convert" to provide the output I need. I want to convert images to PGM format for script processing of images as plain numbers using pipes.

I found out that outputting to a stdout and to a file will not always result in the same format. The last should be P3 (color) but is actually P2 (gray).

Code: Select all

convert rose: -depth 8 -set colorspace Gray -compress none -format pgm - | head -n 1                 # P2
convert rose: -depth 8 -set colorspace RGB  -compress none -format pgm - | head -n 1                 # P3
convert rose: -depth 8 -set colorspace Gray -compress none -format pgm test.pgm ; head -n 1 test.pgm # P2
convert rose: -depth 8 -set colorspace RGB  -compress none -format pgm test.pgm ; head -n 1 test.pgm # P2 -- why???
Also, TIFF images are not being converted to PGM when output is stdout. Converting the rose to TIFF, then converting the rose TIFF to PGM will not provide PGM of ony kind but will continue to be a TIFF.

Code: Select all

convert rose: rose.tif
convert rose.tif -depth 8 -set colorspace RGB -compress none -format pgm - > test.dat
identify test.dat           # test.dat TIFF 70x46 70x46+0+0 8-bit sRGB 9924B 0.000u 0:00.000
How do I obtain PGM (P2/P3) data on stdout from a TIFF file?

Thanks

ImageMagick 7.0.8-36 Q16 on gentoo linux

Re: Converting to PGM to stdout from TIF file does not result in PGM output

Posted: 2019-04-02T11:26:45-07:00
by snibgo
In "convert", "-format" doesn't mean what you think it means.

Instead, prefix the output name with "PGM:", including the colon, eg:

Code: Select all

convert rose.tif PGM:test.dat
... or (probably slower) ...

Code: Select all

convert rose.tif PGM:- >test.dat

[SOLVED] Re: Converting to PGM to stdout from TIF file does not result in PGM output

Posted: 2019-04-02T13:04:33-07:00
by argan
Works perfectly, thanks.