numpy 删除图像的MSB位

sc4hvdpw  于 2023-05-07  发布在  其他
关注(0)|答案(2)|浏览(139)

我可以删除图像的MSB位而不使用Python中的Open CV或Matlab吗

img = Image.open(image)
arrayimg = asarray(img)

我已经读取了图像nd它是转换为数组太,现在我想删除MSB的每个像素从这个图像
图像是尺寸为512*512的灰度2D图像
我试着将图像的大小调整到0,255,隐藏单元8,然后Map,但似乎不起作用

img1 = (img1 * 255).round().astype(np.uint8)

mask = np.full(img1.shape, 0x7E, dtype=np.uint8)
res = np.bitwise_and(img1, mask)
plt.imshow(res)
yhuiod9q

yhuiod9q1#

import numpy as np
import matplotlib.pyplot as plt

# Read the image
img = Image.open("image.png")

# Convert the image to an array
arrayimg = np.asarray(img)

# Get the shape of the image
height, width = arrayimg.shape

# Create a mask to remove the MSB bit
mask = np.full((height, width), 0x7E, dtype=np.uint8)

# Apply the mask to the image
res = np.bitwise_and(arrayimg, mask)

# Display the image
plt.imshow(res)
plt.show()

图片中的每个像素都将通过此代码删除其MSB位,然后将显示结果图像。此外,图像的文件大小可以减小,或者可以用这种方法产生风格化的外观。

h9a6wy2h

h9a6wy2h2#

如果你正在使用Python图像库(PIL),你可以使用eval函数:https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.eval

from PIL import Image
img1 = Image.open(image)
img2 = Image.eval(img1, lambda x: x // 2)

现在,与img相比,img2将使每个像素值减小2倍。这有效地清除了MSB。

相关问题