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.
Asked
Active
Viewed 320 times
1 Answers
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
-
Hi , thanks for your help. How can I get the values in U16. – SHIVANI DEEPAK CHOUDHARY Jan 22 '21 at 14:32
-
If U16 means unsigned 16-bit, just change the type like this `na = np.array(im).astype(np.uint16)` – Mark Setchell Jan 22 '21 at 14:34
-
Hi, It still doesn't match with the answer we get from Labview – SHIVANI DEEPAK CHOUDHARY Jan 22 '21 at 14:38
-
Your values seem to be scaled by a factor of 64, relative to what **PIL**, **OpenCV** and **ImageMagick** think, so you can use `na = (np.array(im)//64).astype(np.uint16)` – Mark Setchell Jan 22 '21 at 14:42
-
I needed one more help . After the calculations in my project I want to print the result including all the numbers. – SHIVANI DEEPAK CHOUDHARY Jan 22 '21 at 14:54
-
intensity_image = intensity_image.astype('uint16') print(intensity_image) But with this I dont get all the numbers. How can I print all the pixels in this. – SHIVANI DEEPAK CHOUDHARY Jan 22 '21 at 14:54
-
You could print a table like this https://stackoverflow.com/a/47254935/2836621 Or you could save as a CSV like this `np.savetxt("intensity.csv", intensity_image, delimiter=",")` and view with **Excel**. – Mark Setchell Jan 22 '21 at 14:59
-
Hi I get the following error - Expected 1D or 2D array, got 3D array instead – SHIVANI DEEPAK CHOUDHARY Jan 22 '21 at 15:02
-
You must be using a different image that is either colour (`na.shape` will be `H,W,3`) or has an alpha channel (`na.shape` will be `H,W,2` or `H,W,4`). What is the shape of your array - I provided code in my answer to print it. – Mark Setchell Jan 22 '21 at 15:06
-
Yes I get (256, 320, 3) uint16 – SHIVANI DEEPAK CHOUDHARY Jan 22 '21 at 15:36
-
Ok, you have a colour image with 3 channels - Red, Green and Blue not the 1-channel greyscale image you shared. You can't really print a 3-channel image in a 2D Excel spreadsheet, so you'll either need to convert it to greyscale, or make a spreadsheet for the red channel and another for he green and another for the blue. Please tell me which you want. – Mark Setchell Jan 22 '21 at 15:40
-
Hi when I try to print it directly it shows [[1000 1000 1000] [ 995 995 995] [ 991 991 991] ... [ 994 994 994] [ 994 994 994] [ 994 994 994]] – SHIVANI DEEPAK CHOUDHARY Jan 22 '21 at 15:50
-
Can you please tell me how to convert it to greyscale and print the pixels – SHIVANI DEEPAK CHOUDHARY Jan 22 '21 at 15:51
-
You can use `greyImage = cv2.cvtColor(colourImage, cv2.COLOR_BGR2GRAY)` – Mark Setchell Jan 22 '21 at 16:24