The fastest way to extract color bits in a .pgm image?

I have hundreds of 128 x 128.pgm files with some shapes on them, and I think their color gamut is 255 (not sure about this, although it would be nice if the solution could also take this into), and I need extract these colors for image processing. Thus, I would like to get a 128 x 128 matrix with each element having a value from 0 to 255, with the condition of 256 colors.

As for the language, something in Python / Java / C # will do, preferably in that order. I can use either Windows or Linux, so exclusive libraries are not a problem.

+3
source share
2 answers

As for the python solution, I believe that PIL supports .pgm files. In this case (using numpy as an array container, but this part is optional):

(Edit: re-read your question and realized that you especially need shades of gray, not RGB ... Which I had to implement from the .pgm format, anyway ...)

import Image
import numpy as np

im = Image.open('test.pgm')

# Convert to grayscale (single 8-bit band), if it not already...
im = im.convert('L')

# "data" is a uint8 (0-255) numpy array...
data = np.asarray(im)
+1
source

P or G ray M ap format is so trivial that I consider it a matter of honor to write a parser for pgm files yourself in any language that you need. Each value read from a PGM file must correspond to an RGB byte.

255 * Value / Max_Value
.

good luck.

0
source

Source: https://habr.com/ru/post/1764553/


All Articles