我试着用RSA加密和解密图像。为此,我需要将图像读取为灰度,然后应用键并将uint16类型数组保存到支持16位数据的png或任何图像格式中。然后,我需要读取16位数据,将其转换为数组并进行解密。现在,以前我尝试将图像保存为.tif,并且当我阅读它时
img = sk.imread('image.tiff', plugin = 'tifffile')
它将图像处理为RGB,这不是我想要的。现在,我想将uint16类型数组保存到一个16位png映像中,该图像的值在0到65536之间,然后再将其作为uint16类型数据读取。我尝试将这些值保存到一个16位png文件中。
img16 = img.astype(np.uint16)
imgOut = Image.fromarray(img16)
imgOut.save('en.png')
这给了我一个错误:OSError: cannot write mode I;16 as PNG
我也试过imgOut = Image.fromarray(img16, 'I')
,但这是not enough image data
请帮助我将16位数据保存到.png图像中。谢谢。
发布于 2019-03-23 10:36:45
有几种可能性..。
首先,使用imageio
编写16位PNG:
import imageio
import numpy as np
# Construct 16-bit gradient greyscale image
im = np.arange(65536,dtype=np.uint16).reshape(256,256)
# Save as PNG with imageio
imageio.imwrite('result.png',im)
然后,您可以从磁盘中读取图像,并将第一个像素更改为中间灰色(32768),如下所示:
# Now read image back from disk into Numpy array
im2 = imageio.imread('result.png')
# Change first pixel to mid-grey
im2[0][0] = 32768
或者,如果您不喜欢imageio
,可以使用PIL/Pillow
并保存一个16位TIFF:
from PIL import Image
import numpy as np
# Construct 16-bit gradient greyscale image
im = np.arange(65536,dtype=np.uint16).reshape(256,256)
# Save as TIFF with PIL/Pillow
Image.fromarray(im).save('result.tif')
然后,您可以从磁盘中读取图像,并将第一个像素更改为中间灰色,如下所示:
# Read image back from disk into PIL Image
im2 = Image.open('result.tif')
# Convert PIL Image to Numpy array
im2 = np.array(im2)
# Make first pixel mid-grey
im2[0][0] = 32768
关键词:图像,图像处理,Python,Numpy,PIL,Pillow,imageio,TIF,TIFF,PNG,16位,16位,短,无符号短,保存,写.
https://stackoverflow.com/questions/55311985
复制相似问题