📜  Python中的 numpy.packbits()

📅  最后修改于: 2022-05-13 01:54:46.945000             🧑  作者: Mango

Python中的 numpy.packbits()

numpy.packbits()是另一个在 numpy 中进行二进制操作的函数。它用于将二进制值数组的元素打包成 uint8 数组中的位。通过在末尾插入零位将结果填充到完整字节。

代码#1:

# Python program explaining
# numpy.packbits() function
  
# importing numpy
import numpy as geek
  
# creating input array using 
# array function
in_arr = geek.array([[[1, 0, 1],
                     [0, 1, 0]],
                     [[1, 1, 0],
                        [0, 0, 1]]])
print ("Input array : ", in_arr) 
  
# packing elements of an array
# using packbits() function
out_arr = geek.packbits(in_arr)
  
print ("Output packed array : ", out_arr)
输出 :
Input array :  
[[[1 0 1]
  [0 1 0]]

 [[1 1 0]
  [0 0 1]]]
Output packed array :  [171  16]

代码#2:

# Python program explaining
# numpy.packbits() function
  
# importing numpy
import numpy as geek
  
# creating input array using 
# array function
in_arr = geek.array([[[0, 0, 1],
                     [1, 1, 0]],
                     [[1, 0, 0],
                        [1, 1, 1]]])
print ("Input array : ", in_arr) 
  
# packing elements of an array
# using packbits() function
out_arr = geek.packbits(in_arr, axis = 1)
  
print ("Output packed array along axis 1 : ", out_arr) 
输出 :
Input array :  [[[0 0 1]
  [1 1 0]]

 [[1 0 0]
  [1 1 1]]]
Output packed array along axis 1 :  [[[ 64  64 128]]

 [[192  64  64]]]