2

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?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Have you found or written a library to read `jpg`? If not I would start by searching for a library. – Richard Critten Nov 29 '22 at 21:29
  • Imagemagick [claims](https://imagemagick.org/script/formats.php) support. You might want to look into it. Also `-Compress None` might help to get P3 specifically. – n0rd Nov 29 '22 at 21:30

2 Answers2

2

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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I have no idea what the ImagineMagick is. Do I just have to open the Command Prompt and type "magick INPUT.JPG -compress none OUTPUT.PPM" there? After doing so, it said: "'magick' is not recognized as an internal or external command, operable program or batch file.". When I tried "convert INPUT.JPG -compress none OUTPUT.PPM", it said: "Invalid Parameter - -compress". Or do I have to download that ImagineMagick thing? – luka mosiashvili Nov 29 '22 at 21:42
  • You would need to install it. Either from here https://imagemagick.org/ or I believe Windows folk can use Chocolatey https://chocolatey.org/ though I have little idea about Windows. – Mark Setchell Nov 29 '22 at 21:45
  • I got the impression he wants to do this programatically. If asking for programming suggestions or how to use ImageMagick is an answer then it's off topic as not a programming question or answer. – Rob Nov 29 '22 at 21:55
  • 1
    @Rob I don't think that's correct. There's a legitimate and popular [imagemagick] tag with 9,000 questions on **ImageMagick**. There is a separate [magick++] tag for programmatic access from C++. If running `sed`, or `awk` or `tr` to transform/convert text from one format/layout to another is programming, then so is running **ImageMagick** to transform images. – Mark Setchell Nov 29 '22 at 22:10
  • Writing programs that interact with ImageMagick is fine. Just running it by itself is off topic. There are also a lot of systemd and iptables questions here. Most of them are off topic and get closed for the same reason. I haven't seen anything programming related here. – Rob Nov 29 '22 at 22:27
  • Mark, can you show the other direction? --- i.e.,how to read a P3 file into Imagemagick. Perhaps the OP wanted to know how to convert from P3 to some other binary image format. – fmw42 Nov 29 '22 at 22:27
  • @Rob, for my part, I do not think it is off-topic. – fmw42 Nov 29 '22 at 22:28
  • @Rob Please don't underestimate the programming capabilities (FFTs, morphology, colourspace transformations, quantisation, template matching, animation, affine transformations etc) available and programmable from the command line within **ImageMagick** just because this question happens to be at the simpler end of the spectrum. And I stand by my earlier comments about `sed` and `awk`. – Mark Setchell Nov 29 '22 at 22:41
  • sed and awk are programming. ImageMagick, which I have used for 20 something years, is a program, as is LibreOffice and Photoshop though IM can be used with code. All are off topic here unless they involve programming. @fmw42 – Rob Nov 29 '22 at 23:36
  • Let's agree to disagree, else we'll get told off for misusing comments. – Mark Setchell Nov 29 '22 at 23:58
0

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

K J
  • 8,045
  • 3
  • 14
  • 36