【问题标题】:Reloading a created float32 creates a tiled image重新加载创建的 float32 会创建平铺图像
【发布时间】:2016-06-02 03:15:40
【问题描述】:

我将 numpy 数组保存为原始文件(只有绿色通道,所以没有 RGB):

dtype_string = "float32"
>>> frames.shape
(40000L, 128L, 128L)

frames.astype(dtype_string).tofile(os.path.expanduser('~/Downloads/') + "aligned_" + str(ind) + ".raw")

这是plt.imshow(frames.astype(dtype_string)[400]) 的样子:

你可以下载我保存的raw here

现在我只想打开刚才保存的文件如下:

dat_type= "float32"

with open(g_file, "rb") as file:
    frames = np.fromfile(file, dtype=dat_type)
    total_number_of_frames = int(np.size(frames)/(width*height))
    print("n_frames: "+str(total_number_of_frames))
    frames = np.reshape(frames, (total_number_of_frames, width, height))
    frames = np.asarray(frames, dtype=dat_type)

但是在这之后plt.imshow(frames[400]) 看起来像这样

为什么我得到平铺图像?

【问题讨论】:

  • 保存前frames.shape是什么,重载后又是什么?
  • plt.imshow(frames[400][:][:]) 的哪个结果?

标签: python-2.7 numpy matplotlib


【解决方案1】:

将此答案视为扩展评论,谢谢。您给我们的示例并不代表 MWE,因为我们没有关于您在代码中使用的不同值的信息。

我把这段模仿你的代码拼凑在一起

import numpy as np
import matplotlib.pyplot as plt

dt = "float32"
# a sinusoidal surface that rotates about the origin
x, y = np.linspace(0,10, 51), np.linspace(-5, 5, 51)
X, Y = np.meshgrid(x, y)
a = np.linspace(-1, 1, 7)
z = np.sin(X[None,...]+a[...,None,None]*Y[None,...])

# reduce precision, plot
z = z.astype(dt)
plt.imshow(z[6]) ; plt.savefig('im6.png')

# output input
z.tofile('z.raw')
Z = np.fromfile('z.raw', dtype=dt)
Z = Z.reshape((7,51,51))

# plot
plt.imshow(Z[6]) ; plt.savefig('Im6.png')

这就是我从 shell 提示符得到的结果

$ md5sum Documents/tmp/?m6.png 
e4d9fc1358c85daec3f1680e5b9edcef  Documents/tmp/im6.png
e4d9fc1358c85daec3f1680e5b9edcef  Documents/tmp/Im6.png
$ 

即,这两个文件是相同的。代码的主要区别在于使用已知值进行数据数组整形,所以我猜测可能是整形出错了。

【讨论】:

  • 你是完全正确的。原来是宽度和高度的简单方式是错误的值,这当然会与重塑相混淆
猜你喜欢
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多