[英]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.