![](/img/trans.png)
[英]How to specify the endiannes directly in the numpy datatype for a 16bit unsigned integer?
[英]How to get a 16bit Unsigned integer in python
我目前使用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])
如何让PIL输出无符号而不是有符号整数? 或者有一种非常简单的方法来获取PIL输入并在之后进行转换?
这是一个结构的解决方案,因为我不知道python如何二进制代表负数。
import struct
struct.unpack('H', struct.pack('h', number))
它将它打包为一个短(2个字节)并将其解压缩为unsigned short。
>>> import numpy as np
>>> np.array([-20357],dtype="uint16")
array([45179], dtype=uint16)
在你的情况下,当你完成所有循环,然后你在列表中
只需调用np.array(my_list,dtype="uint16")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.