Python模糊图像的创作,创造出一幅由黑色到whi的美丽色彩画

2024-04-26 00:07:19 发布

您现在位置:Python中文网/ 问答频道 /正文

在pyton上,我想创建一个从黑到白的图片,我编写了以下代码。但我认为我犯了一个很小的错误,这就是结果。在

enter image description here

我其实想创造一个类似的形象。你能看到我在哪里犯了错误吗?在

enter image description here

  import numpy as np
from PIL import Image
width = 100
height = 100
img = np.zeros((height, width), dtype=np.uint8)
xx, yy=np.mgrid[:height, :width]
circle = (xx - 50)**2 + (yy- 50)**2
for x in range (img.shape[0]):
    for y in range (img.shape[1]):
        intensity = circle[x][y]
        img[x][y]= intensity
Image.fromarray(img, 'L').show()
Image.fromarray(img, 'L').save ('circlebad.png', 'PNG')

<;-----------------------------------编辑------------------------------->

当我插入;intensity = intensity / 512我的问题解决了。最后代码

^{pr2}$

Tags: 代码inimageimportimgfor错误np
1条回答
网友
1楼 · 发布于 2024-04-26 00:07:19

正如其他人所指出的,您在顶部图像中获得输出的原因是因为强度需要在range(256)中,而Numpy算法有效地对代码产生的值进行% 256。在

这是您代码的修复版本。在

import numpy as np
from PIL import Image

width = height = 320
radius = (width - 1) / 2
xx, yy = np.mgrid[:height, :width]

# Compute the raw values
circle = (xx - radius) ** 2 + (yy - radius) ** 2

#Brightness control
maxval = 255
valscale = maxval / (2 * radius ** 2)
circle = (circle * valscale).astype(np.uint8)

img = Image.fromarray(circle, 'L')
img.show()
img.save('circle.png')

输出:圆形.png

greyscale circle

相关问题 更多 >