How to get 16 bit unsigned integer in python

I am currently viewing image pixels using python PIL. These pixels have a 16-digit shade of gray and are unsigned. However, when PIL reads them in it, they think they are signed and make values ​​that should be something like 45179 at -20357.

org_Image = Image.open(image)
org_Data = org_Image.load()
width, height = org_Image.size

    for y in range(0, height):
        temprow_data = []
        for x in range(0, width):
             temprow_data.append(org_Data[x, y])

How do I get PIL to output unsigned instead of integers? Or is there a very simple way to enter PIL and convert it after?

+4
source share
2 answers

Here is a solution with structs, since I don't know how python represents negative numbers in binary expression.

import struct
struct.unpack('H', struct.pack('h', number))

(2 ) unsigned short.

+3
>>> import numpy as np
>>> np.array([-20357],dtype="uint16")
array([45179], dtype=uint16)

, ,

np.array(my_list,dtype="uint16")

+2

All Articles