How to convert images from the jpg format to ppm(P3)

1.7k Views Asked by At

I have recently learned how to read ppm3(P3) images in C++. I just read RGB pixels written in a plain format. I want to convert some certain jpg pictures to ppm3 and then experiment with different things, like identifying numbers there, the circled answers in exam papers, etc.

I have tried this website: https://convertio.co/pdf-ppm/, but it transformed a photo in the P6 format. Could anyone help?

2

There are 2 best solutions below

10
Mark Setchell On

You can use ImageMagick in the Terminal/shell:

magick INPUT.JPG -compress none OUTPUT.PPM

If you omit -compress none you'll get binary (i.e. P6) PPM output.


If using old v6 ImageMagick, that becomes:

convert INPUT.JPG -compress none OUTPUT.PPM

All the options, switches, operators and settings for ImageMagick are documented here.


If you want to convert PPM to JPEG, or to PNG, you can just use:

magick INPUT.PPM OUTPUT.JPG

or

magick INPUT.PPM OUTPUT.PNG

You can also programmatically create a random PPM file like this:

#!/bin/bash

W=5; H=4
echo "P3\n${W} ${H}\n255" > image.ppm
for ((i=0;i<$((W*H*3));i++)) ; do 
   echo $((RANDOM%255))
done >> image.ppm

Then enlarge for easy viewing and make into a PNG:

magick image.ppm -scale 200x result.png

enter image description here

Or, the same thing again, nut maybe slightly more elegantly and without creating an intermediate file:

#!/bin/bash

W=5; H=4
{
    printf "P3\n${W} ${H}\n255\n"
    for ((i=0;i<$((W*H*3));i++)) ; do 
      echo $((RANDOM%255))
    done
} | magick ppm:- -scale 200x result.png

If you prefer to use the lighter weight, but far less capable NetPBM tools, it would be:

jpegtopnm -plain INPUT.JPG > OUTPUT.PPM
0
K J On

For parsing pixels P6 is probably more useful for binary apps like C
P3 is about 4 times bigger than P6

both P3 and P6 are uncompressed format 1 entry = one pixel component
normally 3 components = 1 pixel (rgb) here is a white cow in a snowfield

enter image description here

each 255 on the Ascii =ÿ in the binary thus faster to count one byte text ÿ than 4 byte 255 the main advantage for P3 is when using an ascii editor to fettle the values as number key inputs (modify lower order numbers as 000 - 031 control code values becomes a problem in text editors)

SAFE binary bytes were used for Ansi Art as here this is a binary.ppm (just using safe non control codes. enter image description here

The binary version (P6 uncompressed) is most easily generated For PDF users by xpdf or poppler pdftoppm (one single executable no real need for more than that). http://www.xpdfreader.com/download.html

For Jpeg you can use jpegtopnm as described in other answer by Mark https://sourceforge.net/projects/netpbm/files/
for docs and other info see https://netpbm.sourceforge.net/doc/ for binaries on windows the Cygwin / GNUwin32 ports may be useful but older 2005 https://gnuwin32.sourceforge.net/packages/netpbm.htm One exe & 4 dlls

![enter image description here

for better description see https://en.wikipedia.org/wiki/Netpbm#File_formats
for windows related viewing see https://github.com/vbaderks/netpbm-wic-codec and also possibly conversion https://github.com/peirick/ZackViewer