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

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
argan
Posts: 2
Joined: 2019-04-02T11:09:50-07:00
Authentication code: 1152

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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
snibgo's IM pages: im.snibgo.com
argan
Posts: 2
Joined: 2019-04-02T11:09:50-07:00
Authentication code: 1152

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

Post by argan »

Works perfectly, thanks.
Post Reply