无法打开图像。不是有效的位图文件或其格式目前不受支持
Cannot open image. Not a valid bitmap file or its format is not currently supported
我编写了这个 Python 程序来创建矩阵(二维数组)并将其保存到 .png 文件中。该程序编译并运行没有任何错误。即使 IMAGE.png 文件已创建,但 png 文件无法打开。当我尝试在 MSpaint 中打开它时,它显示:
Cannot open image. Not a valid bitmap file or its format is not currently supported.
源代码:
import numpy;
import png;
imagearray = numpy.zeros(shape=(512,512));
/* Code to insert one '1' in certain locations
of the numpy 2D Array. Rest of the location by default stores zero '0'.*/
f = open("IMAGE.png", 'wb');
f.write(imagearray);
f.close();
我不明白我哪里出错了,因为没有错误信息。请帮助。
PS- 我只想将矩阵保存为图像文件,所以如果您在 Python2.7 中有更好更简单的方法,请推荐。
谢谢。
并非每个阵列都与图像格式兼容。假设您指的是字节数组,那么您就是这样做的:
import os
import io
import Image
from array import array
def readimage(path):
count = os.stat(path).st_size
with open(path, "rb") as f:
return bytearray(f.read())
bytes = readimage(path+extension)
image = Image.open(io.BytesIO(bytes))
image.save(savepath)
代码片段取自 here。
希望这对你有所帮助,
雅丽.
这是一个使用 numpngw
创建位深度为 1 的图像的示例(即图像中的值只是 0 和 1)。示例直接取自numpngw
包的README文件:
import numpy as np
from numpngw import write_png
# Example 2
#
# Create a 1-bit grayscale image.
mask = np.zeros((48, 48), dtype=np.uint8)
mask[:2, :] = 1
mask[:, -2:] = 1
mask[4:6, :-4] = 1
mask[4:, -6:-4] = 1
mask[-16:, :16] = 1
mask[-32:-16, 16:32] = 1
write_png('example2.png', mask, bitdepth=1)
图片如下:
我编写了这个 Python 程序来创建矩阵(二维数组)并将其保存到 .png 文件中。该程序编译并运行没有任何错误。即使 IMAGE.png 文件已创建,但 png 文件无法打开。当我尝试在 MSpaint 中打开它时,它显示:
Cannot open image. Not a valid bitmap file or its format is not currently supported.
源代码:
import numpy;
import png;
imagearray = numpy.zeros(shape=(512,512));
/* Code to insert one '1' in certain locations
of the numpy 2D Array. Rest of the location by default stores zero '0'.*/
f = open("IMAGE.png", 'wb');
f.write(imagearray);
f.close();
我不明白我哪里出错了,因为没有错误信息。请帮助。
PS- 我只想将矩阵保存为图像文件,所以如果您在 Python2.7 中有更好更简单的方法,请推荐。
谢谢。
并非每个阵列都与图像格式兼容。假设您指的是字节数组,那么您就是这样做的:
import os
import io
import Image
from array import array
def readimage(path):
count = os.stat(path).st_size
with open(path, "rb") as f:
return bytearray(f.read())
bytes = readimage(path+extension)
image = Image.open(io.BytesIO(bytes))
image.save(savepath)
代码片段取自 here。
希望这对你有所帮助, 雅丽.
这是一个使用 numpngw
创建位深度为 1 的图像的示例(即图像中的值只是 0 和 1)。示例直接取自numpngw
包的README文件:
import numpy as np
from numpngw import write_png
# Example 2
#
# Create a 1-bit grayscale image.
mask = np.zeros((48, 48), dtype=np.uint8)
mask[:2, :] = 1
mask[:, -2:] = 1
mask[4:6, :-4] = 1
mask[4:, -6:-4] = 1
mask[-16:, :16] = 1
mask[-32:-16, 16:32] = 1
write_png('example2.png', mask, bitdepth=1)
图片如下: