【发布时间】:2015-12-17 01:01:21
【问题描述】:
我正在尝试在 python 中创建一些 perlin 噪声图像,但遇到了一些问题。运行我的小脚本时出现异常。我显然不了解 Image 模块的使用情况,因为我尝试的所有操作都会导致 ValueError 异常,并显示消息“缓冲区不够大”
这是我目前得到的:
import numpy
from noise import pnoise2, snoise2
import Image
octaves = 1
freq = 16.0 * octaves
y_max = 5
x_max = 5
imarray = [[0 for x in range(y_max)] for x in range(x_max)]
totalcount = 0
for y in range(y_max):
for x in range(x_max):
val = "%s\n" % int(snoise2(x / freq, y / freq, octaves) * 127.0 + 128.0)
imarray[y][x] = val
totalcount += 1
arr = numpy.asarray(imarray).astype('uint8')
im = Image.fromarray(arr, 'RGBA')
im.save('./blah.png')
我得到的例外是:
Connected to pydev debugger (build 143.1184)
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 2407, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1798, in run
launch(file, globals, locals) # execute the script
File "/Users/u4234/source/systools/load/noise_test.py", line 26, in <module>
im = Image.fromarray(arr, 'RGBA')
File "/Users/u4234/Library/Python/2.7/lib/python/site-packages/PIL-1.1.7-py2.7-macosx-10.10-x86_64.egg/Image.py", line 1902, in fromarray
return frombuffer(mode, size, obj, "raw", rawmode, 0, 1)
File "/Users/u4234/Library/Python/2.7/lib/python/site-packages/PIL-1.1.7-py2.7-macosx-10.10-x86_64.egg/Image.py", line 1853, in frombuffer
core.map_buffer(data, size, decoder_name, None, 0, args)
ValueError: buffer is not large enough
Process finished with exit code 1
【问题讨论】:
-
您是要创建彩色图像还是灰度图像?
-
它应该是彩色的。虽然在这个例子中我现在只做一种颜色。