0

I have to integrate my python code in Labview and I am comparing the pixel value of the image in both. The Labview gives pixel values in U16 and hence I want to see the pixel values of the enter image description heresame image in python and see if the values are the same. Can someone please help me with the code for the same? My image is a png image black and white.

1 Answers1

0

You can use PIL or OpenCV or wand or scikit-image for that. Here is a PIL version:

from PIL import Image
import numpy as np

# Open image
im = Image.open('dXGat.png')

# Make into Numpy array for ease of access
na = np.array(im)

# Print shape (pixel dimensions) and data type
print(na.shape,na.dtype)    # prints (256, 320) int32

# Print brightest and darkest pixel
print(na.max(), na.min())

# Print top-left pixel
print(na[0,0])              # prints 25817

# WATCH OUT FOR INDEXING - IT IS ROW FIRST
# print first pixel in second row
print(na[1,0])              # prints 24151

# print first 4 columns of first 2 rows
print(na[0:2,0:4])

Output

array([[25817, 32223, 30301, 33504],
       [24151, 22934, 19859, 21460]], dtype=int32)

If you prefer to use OpenCV, change these lines:

from PIL import Image
import numpy as np

# Open image
im = Image.open('dXGat.png')

# Make into Numpy array for ease of access
na = np.array(im)

to this:

import cv2
import numpy as np

# Open image
na = cv2.imread('dXGat.png',cv2.IMREAD_UNCHANGED)

If you just want to one-time inspect the pixels, you can just use ImageMagick in the Terminal:

magick dXGat.png txt: | more

Sample Output

# ImageMagick pixel enumeration: 320,256,65535,gray
0,0: (25817)  #64D964D964D9  gray(39.3942%)
1,0: (32223)  #7DDF7DDF7DDF  gray(49.1691%)
2,0: (30301)  #765D765D765D  gray(46.2364%)
3,0: (33504)  #82E082E082E0  gray(51.1238%)
...
...
317,255: (20371)  #4F934F934F93  gray(31.0842%)
318,255: (20307)  #4F534F534F53  gray(30.9865%)
319,255: (20307)  #4F534F534F53  gray(30.9865%)
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432