How to Read Values From a Ppm File C++

Solarian Programmer

My programming ramblings

PPM image from scratch in Python 3

Posted on October 25, 2017 by Paul

Someone asked me if it is possible to write epitome files in pure Python, without any libraries. The answer is, of class, you can implement whatsoever epitome format in pure Python, every bit long every bit you have a articulate description of the paradigm format and you lot know how to write binary files.

In this article, I volition bear witness you lot how to write binary PPM images in pure Python. Linux and recent versions of macOS take native back up for PPM files, so you tin open a PPM prototype using the default OS image viewer. For Windows, you can employ a program like IrfanView. The PPM file format tin store uncompressed RGB images in binary or homo readable, ASCII format. As a side note, the PPM format is not an efficient format to store images, however information technology is relatively easy to implement and use, from a programming point of view.

A PPM image starts with a header that consists of:

  • A magic number P3 or P6. P3 means that the file is stored in ASCII format, P6 means the file is stored in binary.
  • Whitespace character.
  • The width and meridian of the paradigm, formatted as 2 ASCII decimal characters, separated by a whitespace character.
  • Whitespace character.
  • The maximum color value, ASCII decimal characters, larger than zero and smaller than 65536. Typically, the maximum value is 255.
  • Whitespace character. Usually the LF character.

Delight note, that in the above whitespace could be blank, TAB, CR, LF.

Side by side, we store the actual image data starting from the top left corner of the prototype, in row-major fashion, were each pixel consists of an (R, Yard, B) colour triplet. If the max color value is less than 256, each color value is stored as an unsigned integer of one byte length in binary. For max colour value larger than 256 we use ii bytes for the color data. You can read more about the PPM format at http://netpbm.sourceforge.net/physician/ppm.html.

In order to store the image information efficiently, in Python, nosotros volition use the array module. Arrays in Python are like to lists simply they can store only a single data blazon.

This is how y'all create an eight elements array of unsigned characters in Python:

                                                    1                                    import                  assortment                  ii                                    three                                    my_array                  =                  array                  .                  array                  (                  'B'                  ,                  [                  0                  ]                  *                  8                  )                  4                                    5                                    print                  (                  "my_array has"                  ,                  len                  (                  my_array                  ),                  "elements"                  )                  6                                    impress                  (                  "The size of ane element of my_array is"                  ,                  my_array                  .                  itemsize                  ,                  "bytes"                  )                  7                                    impress                  (                  "my_array ="                  ,                  my_array                  )                              

This is what I run into, on my automobile, if I run the to a higher place code:

                                                    1                                    ~ $ python examination.py                  two                                    my_array has 8 elements                  iii                                    The size of 1 chemical element of my_array is i bytes                  four                                    my_array = array('B', [0, 0, 0, 0, 0, 0, 0, 0])                  5                                    ~ $                              

Delight annotation, that the type of my_array in the higher up example corresponds to uint8_t in recent versions of C and C++.

If you want to allocate space for an RGB image of (width x height) pixels, in Python, we can use:

                                                    ane                                    import                  array                  ii                                    3                                    width                  =                  256                  four                                    height                  =                  128                  5                                    half-dozen                                    image                  =                  assortment                  .                  assortment                  (                  'B'                  ,                  [                  0                  ,                  0                  ,                  0                  ]                  *                  width                  *                  height                  )                              

We tin fill the header of a PPM image with:

                                                    1                                    width                  =                  256                  2                                    peak                  =                  128                  3                                    maxval                  =                  255                  4                                    5                                    # PPM header                  6                                    ppm_header                  =                  f                  'P6 {width} {meridian} {maxval}                  \n                  '                              

The final line from the higher up code snippet volition raise an error, if your version of Python is older than 3.half dozen. Here is an alternative for older Python versions:

                                                    1                                    # PPM header                  ii                                    ppm_header                  =                  'P6 '                  +                  str                  (                  width                  )                  +                  ' '                  +                  str                  (                  height                  )                  +                  ' '                  +                  str                  (                  maxval                  )                  +                  '                  \due north                  '                              

Once you accept the prototype data and the PPM header prepared, you lot can save them in a binary file. Here is a complete example that will create a PPM image of 256x128 pixels filled with the blueish color:

                                                                        1                                    import                  array                                      2                                                        3                                    # PPM header                                      iv                                    width                  =                  256                                      five                                    acme                  =                  128                                      6                                    maxval                  =                  255                                      7                                    ppm_header                  =                  f                  'P6 {width} {height} {maxval}                  \n                  '                                      eight                                                        9                                    # PPM image data (filled with blueish)                  10                                    image                  =                  array                  .                  array                  (                  'B'                  ,                  [                  0                  ,                  0                  ,                  255                  ]                  *                  width                  *                  summit                  )                  eleven                                    12                                    # Salvage the PPM image as a binary file                  13                                    with                  open                  (                  'blue_example.ppm'                  ,                  'wb'                  )                  every bit                  f                  :                  14                                    f                  .                  write                  (                  bytearray                  (                  ppm_header                  ,                  'ascii'                  ))                  15                                    epitome                  .                  tofile                  (                  f                  )                              

Red filled rectangle on blue background

If you want to alter a particular pixel value (x, y) in the in a higher place image, you tin can access it with something similar:

                                                    1                                    # Change the (x, y) pixel color to red                  2                                    index                  =                  3                  *                  (                  y                  *                  width                  +                  10                  )                  3                                    image                  [                  index                  ]                  =                  255                  # blood-red channel                  4                                    image                  [                  index                  +                  1                  ]                  =                  0                  # light-green channel                  v                                    image                  [                  index                  +                  ii                  ]                  =                  0                  # blue aqueduct                              

Please annotation, that x goes from left to right and y goes from top to bottom. To illustrate, here is how y'all can fill with red a rectangle with the origin at (10, 10), a width of 50 pixels and a height of eighty pixels:

                                                                        1                                    import                  array                                      2                                                        3                                    # PPM header                                      4                                    width                  =                  256                                      5                                    superlative                  =                  128                                      six                                    maxval                  =                  255                                      vii                                    ppm_header                  =                  f                  'P6 {width} {acme} {maxval}                  \n                  '                                      8                                                        nine                                    # PPM image data (filled with blue)                  10                                    image                  =                  array                  .                  assortment                  (                  'B'                  ,                  [                  0                  ,                  0                  ,                  255                  ]                  *                  width                  *                  height                  )                  11                                    12                                    # Fill with ruby the rectangle with origin at (10, 10) and width x top = fifty 10 fourscore pixels                  xiii                                    for                  y                  in                  range                  (                  10                  ,                  90                  ):                  14                                    for                  x                  in                  range                  (                  10                  ,                  lx                  ):                  15                                    alphabetize                  =                  3                  *                  (                  y                  *                  width                  +                  x                  )                  16                                    image                  [                  index                  ]                  =                  255                  # red channel                  17                                    image                  [                  index                  +                  ane                  ]                  =                  0                  # green channel                  eighteen                                    image                  [                  alphabetize                  +                  2                  ]                  =                  0                  # blue channel                  xix                                    20                                    # Save the PPM paradigm as a binary file                  21                                    with                  open                  (                  'blue_red_example.ppm'                  ,                  'wb'                  )                  as                  f                  :                  22                                    f                  .                  write                  (                  bytearray                  (                  ppm_header                  ,                  'ascii'                  ))                  23                                    image                  .                  tofile                  (                  f                  )                              

Red filled rectangle on blue background

Encapsulating the in a higher place functionality in a PPM form is left as an practice for the reader.

If y'all want to learn more nigh Python, I recommend reading Python Crash Class past Eric Matthes:

Another proficient Python book, for more than advanced users, is Python Playground by Mahesh Venkitachalam:



dowdject1963.blogspot.com

Source: https://solarianprogrammer.com/2017/10/25/ppm-image-python-3/

0 Response to "How to Read Values From a Ppm File C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel