python-3.x - 从二进制 8 位图像访问 LSB 和 MSB
问题描述
我正在研究问题所在的加密:
对于图像的每个像素,将像素十进制值转换为二进制 8 位。然后将每个 8 位像素的 MSB 和 LSB 分开,并将其转换为十进制。
我已将图像转换为每个像素的 8 位二进制文件。现在我不知道如何分离 MSB 和 LSB。
这是我的代码:
#taking input image
input = cv2.imread('test.webp')
#print("before\n\n")
cv2.imshow('image before',input)
#Get input size converting to a fixed size
height, width = input.shape\[:2\]
#Desired "pixelated" size
w, h = (256, 256)
#Resize input to "pixelated" size
temp = cv2.resize(input, (w, h), interpolation=cv2.INTER_LINEAR)
print(temp)
#Initialize output image
#the converted pixed into 8bit image
output = cv2.resize(temp, (width, height), interpolation=cv2.INTER_NEAREST)
cv2.imwrite("pixelate.jpg", output)
#print(output)
#output
print('after\n\n')
cv2.imshow('image after',output)
cv2.waitKey(0)
cv2.destroyAllWindows()][1]
解决方案
首先,读取图像并打印其数据类型
img = cv2.imread('test.webp')
print(img.dtype)
如果数据类型不是uint8,则将图像缩放到0-255并将其数据类型更改为uint8。
现在,屏蔽每个像素的第一个(LSB)和最后一个(MSB)位
import numpy as np
mask = np.full(img.shape, 0x7E, dtype=np.uint8)
res = np.bitwise_and(img, mask)
import matplotlib.pyplot as plt
plt.imshow(res)
推荐阅读
- android - Fcm Notification not triggered in background or when device is rebooted
- python - How to check a radio button based on user input in a Django Template?
- node.js - how can i use res.sendfile method in a post request
- python - 将数据框附加到具有不同列数的现有 csv 文件
- r - 如何在忽略 NA 的同时计算 R 中的唯一值
- javascript - 使用带有多个配置的 TinyMCE 字符映射插件
- linux - socket read/recv得到的数据长度不确定
- akka.net - 通过路径向 Actor 发送查询参数
- python - 根据另一个列表中的值过滤列表
- oracle - 索引嵌套循环连接忽略 SQL 语句