本文介绍了将Numpy数组保存为图像(说明)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在上一篇文章中找到了答案:。唯一的问题是,没有太多关于使用PyPNG模块的指令。

I found my answer in a previous post: Saving a Numpy array as an image. The only problem being, there isn't much instruction on using the PyPNG module.

网上只有几个例子 -

http://nullege.com/codes/search/png.Writer.write

There are only a few examples online--http://packages.python.org/pypng/ex.html#numpyhttp://nullege.com/codes/search/png.Writer.write

但是我如何根据.write错误做这样的事情:

But what do I do in light of .write errors like this:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/png.py", line 638, in write
    nrows = self.write_passes(outfile, rows)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/png.py", line 783, in write_passes
    extend(row)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/png.py", line 780, in <lambda>
    return lambda sl: f(map(int, sl))
TypeError: argument 2 to map() must support iteration

这是我的代码中发生错误的地方,PCA_tool.py(错误发生在folder.write(outfilename,PrincipalComponent)之后:

Here's where the error happens in my code, PCA_tool.py (The error comes after "folder.write(outfilename, PrincipalComponent"):

#PrincipalComponent.save(path+'transform_'+str(each)+'.png', format='PNG')
outfilename = open(str(path)+'transformed/transform_'+str(each)+'.png', 'wb')
folder = png.Writer(m,n,greyscale=True)
folder.write(outfilename, PrincipalComponent)
outfilename.close()

sys.exit(0)

我正在尝试将8400元素numpy.ndarray保存为= 80列,m = 105行灰度png图像。

I'm trying to save a 8400 element numpy.ndarray as a n=80 column, m=105 row greyscale png image.

谢谢,

推荐答案

您可能最好使用PIL:

You might be better off using PIL:

import Image
import numpy as np

data = np.random.random((100,100))

#Rescale to 0-255 and convert to uint8
rescaled = (255.0 / data.max() * (data - data.min())).astype(np.uint8)

im = Image.fromarray(rescaled)
im.save('test.png')

这篇关于将Numpy数组保存为图像(说明)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 17:37