如何从 numpy float32 数组创建图像?
How to create image from numpy float32 array?
我有一个 numpy.ndarray
数组,其中包含 float32
的值。图片尺寸应为 226×226。我尝试使用 PIL.Image
创建图像,但出现错误。我读到 PIL.Image.fromarray
需要一个对象和模式,对于 float,我需要在尝试时用 'F'
调用 fromarray
。
这是我尝试做的:
from PIL import Image
img = Image.fromarray(slice56, mode='F')
#type(slice56) = <type 'numpy.ndarray'>
#slice56 = array([ 0., 0., 0., ..., 0., 0., 0.], dtype=float32)
我得到这个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/site-packages/PIL/Image.py", line 1860, in fromarray
return frombuffer(mode, size, obj, "raw", mode, 0, 1)
File "/usr/lib64/python2.6/site-packages/PIL/Image.py", line 1805, in frombuffer
return apply(fromstring, (mode, size, data, decoder_name, args))
File "/usr/lib64/python2.6/site-packages/PIL/Image.py", line 1743, in fromstring
im = new(mode, size)
File "/usr/lib64/python2.6/site-packages/PIL/Image.py", line 1710, in new
return Image()._new(core.fill(mode, size, color))
TypeError: argument 2 must be sequence of length 2, not 1
任何人都可以建议如何做吗?或者如何解决这个错误?
我同意 DavidG 的回答是从 numpy 数组绘制图像的快速解决方案。但是,如果您有充分的理由坚持使用 PIL.Image
,最接近您已经完成的方法是这样的:
from PIL import Image
import numpy as np
slice56 = np.random.random((226, 226))
# convert values to 0 - 255 int8 format
formatted = (slice56 * 255 / np.max(slice56)).astype('uint8')
img = Image.fromarray(formatted)
img.show()
然后它将产生类似于下面给定随机数的结果:
感谢 John Titus Jungao
问题解决了。该数组是一维的,所以我执行了以下操作:
from PIL import Image
import numpy as np
slice56 = slice56.reshape((226,226))
formatted = (slice56 * 255 / np.max(slice56)).astype('uint8')
img = Image.fromarray(formatted)
img.save('slice56.png')
就是这样。
scipy.misc
是我最喜欢的方式。它更干净,而且只需一行即可完成。
看看它多漂亮:
import scipy.misc
img = scipy.misc.toimage(slice56, mode='L')
请查看here官方文档。
我有一个 numpy.ndarray
数组,其中包含 float32
的值。图片尺寸应为 226×226。我尝试使用 PIL.Image
创建图像,但出现错误。我读到 PIL.Image.fromarray
需要一个对象和模式,对于 float,我需要在尝试时用 'F'
调用 fromarray
。
这是我尝试做的:
from PIL import Image
img = Image.fromarray(slice56, mode='F')
#type(slice56) = <type 'numpy.ndarray'>
#slice56 = array([ 0., 0., 0., ..., 0., 0., 0.], dtype=float32)
我得到这个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/site-packages/PIL/Image.py", line 1860, in fromarray
return frombuffer(mode, size, obj, "raw", mode, 0, 1)
File "/usr/lib64/python2.6/site-packages/PIL/Image.py", line 1805, in frombuffer
return apply(fromstring, (mode, size, data, decoder_name, args))
File "/usr/lib64/python2.6/site-packages/PIL/Image.py", line 1743, in fromstring
im = new(mode, size)
File "/usr/lib64/python2.6/site-packages/PIL/Image.py", line 1710, in new
return Image()._new(core.fill(mode, size, color))
TypeError: argument 2 must be sequence of length 2, not 1
任何人都可以建议如何做吗?或者如何解决这个错误?
我同意 DavidG 的回答是从 numpy 数组绘制图像的快速解决方案。但是,如果您有充分的理由坚持使用 PIL.Image
,最接近您已经完成的方法是这样的:
from PIL import Image
import numpy as np
slice56 = np.random.random((226, 226))
# convert values to 0 - 255 int8 format
formatted = (slice56 * 255 / np.max(slice56)).astype('uint8')
img = Image.fromarray(formatted)
img.show()
然后它将产生类似于下面给定随机数的结果:
感谢 John Titus Jungao
问题解决了。该数组是一维的,所以我执行了以下操作:
from PIL import Image
import numpy as np
slice56 = slice56.reshape((226,226))
formatted = (slice56 * 255 / np.max(slice56)).astype('uint8')
img = Image.fromarray(formatted)
img.save('slice56.png')
就是这样。
scipy.misc
是我最喜欢的方式。它更干净,而且只需一行即可完成。
看看它多漂亮:
import scipy.misc
img = scipy.misc.toimage(slice56, mode='L')
请查看here官方文档。