如何在python中获取16位无符号整数

Mar*_*ong 6 python unsigned integer bit python-imaging-library

我目前使用 python PIL 从图像中读取像素。这些像素为 16 位灰度且无符号。但是,每当 PIL 读取它们时,它都会认为它们已签名并将应该类似于45179 的值转换为-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])
Run Code Online (Sandbox Code Playgroud)

我如何让 PIL 输出无符号而不是有符号整数?或者是否有一种非常简单的方法来获取 PIL 输入并在之后进行转换?

Use*_*ser 6

这是一个带有结构的解决方案,因为我不知道 python 如何以二进制方式表示负数。

import struct
struct.unpack('H', struct.pack('h', number))
Run Code Online (Sandbox Code Playgroud)

它将它打包为一个 short(2 个字节)并将其解包为一个无符号的 short。