Python 尝试使用noise、numpy和image创建噪波图像

Python 尝试使用noise、numpy和image创建噪波图像,python,image,numpy,Python,Image,Numpy,我试图用python创建一些柏林噪声图像,但遇到了一点问题。当我运行我的小脚本时,我遇到了一个异常。很明显,我没有掌握图像模块的使用方法,因为我尝试的每件事都会导致ValueError异常,并显示消息“缓冲区不够大” 以下是到目前为止我得到的信息: import numpy from noise import pnoise2, snoise2 import Image octaves = 1 freq = 16.0 * octaves y_max = 5 x_max = 5 imarray

我试图用python创建一些柏林噪声图像,但遇到了一点问题。当我运行我的小脚本时,我遇到了一个异常。很明显,我没有掌握图像模块的使用方法,因为我尝试的每件事都会导致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
连接到pydev调试器(build 143.1184)
回溯(最近一次呼叫最后一次):
文件“/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev.py”,第2407行,在
globals=debugger.run(setup['file'],None,None,is_模块)
文件“/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev.py”,第1798行,运行中
启动(文件、全局、局部)#执行脚本
文件“/Users/u4234/source/systools/load/noise_test.py”,第26行,in
im=Image.fromarray(arr,'RGBA')
fromarray中的文件“/Users/u4234/Library/Python/2.7/lib/Python/site packages/PIL-1.1.7-py2.7-macosx-10.10-x86_64.egg/Image.py”,第1902行
从缓冲区返回(模式,大小,对象,“原始”,原始模式,0,1)
frombuffer中的文件“/Users/u4234/Library/Python/2.7/lib/Python/site packages/PIL-1.1.7-py2.7-macosx-10.10-x86_64.egg/Image.py”,第1853行
核心映射缓冲区(数据、大小、解码器名称、无、0、参数)
ValueError:缓冲区不够大
进程已完成,退出代码为1

问题在于,您要求
PIL
创建一个新的
'RGBA'
图像,它需要4个通道,但您只传递一个二维数组,例如一个通道

如下所示,给定这些阵列:

arr1 = numpy.zeros((5, 5), dtype=numpy.uint8)  # A single 5x5 channel
arr2 = numpy.zeros((5, 5, 3), dtype=numpy.uint8)  # 3 5x5 channels
创建灰度
'L'
图像:

im = Image.fromarray(arr1, 'L')  # OK
im = Image.fromarray(arr2, 'L')  # ValueError: Too many dimensions.
创建不带alpha通道的彩色图像
“RGB”

im = Image.fromarray(arr1, 'RGB')  # ValueError: not enough image data
im = Image.fromarray(arr2, 'RGB')  # OK
请注意RGB模式的使用,因为您可能不关心alpha通道,否则只需向
numpy.zeros
构造函数添加其他维度即可


你可以把你的代码改写成

arr = numpy.zeros((5, 5, 1), dtype=numpy.uint8)

y_max, x_max, channels = arr.shape

for y in range(y_max):
    for x in range(x_max):
        val = int(snoise2(x / freq, y / freq, octaves) * 127.0 + 128.0)
        arr[y,x,0] = val

im = Image.fromarray(arr, 'L')
im.save('./blah.png')

生成灰度图像。

您是要创建彩色图像还是灰度图像?它应该是彩色的。虽然在这个例子中,我现在只做了一种颜色,那就是:)谢谢你。如果我能投你一票,我会的。但我的名声还不够好。衷心的“谢谢”行吗?当然,不客气!顺便提一下若它是图像坐标的函数,那个么有一种方法可以简化你们的噪声发生器。