Python模糊的图像创建,以创建从黑色到白色的精美彩色绘画 [英] Python blurred image creation, to create a beautifully colored painting from black to white
问题描述
在Pyhton上,我想创建一张从黑色到白色的图片,并编写了以下代码.但是我认为我犯了一个非常小的错误,这就是结果.
on Pyhton I wanted to create a picture that goes from black to white, and I wrote the following code. But I think I'm doing a very small mistake, and that's the result.
我实际上想创建一个相似的图像.可以看到我在哪里犯错了吗?
I actually wanted to create a similar image. Can you see where I made a mistake?
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')
< ----------------------------------编辑---------------------------------------->
<----------------------------------Edit---------------------------------------->
当我插入时; intensity =强度/512
我的问题解决了.最后的密码;
When I insert; intensity = intensity / 512
my problem solved. Last codes;
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]
intensity = intensity / 512
img[x][y]= intensity
Image.fromarray(img, 'L').show()
Image.fromarray(img, 'L').save ('circlebad.png', 'PNG')
推荐答案
正如其他人指出的那样,您在顶部图像中获得输出的原因是因为强度必须在 range(256)>code>,而Numpy算术实际上对代码所产生的值进行了
%256
.
As others have noted, the reason you are getting the output in the top image is because the intensities need to be in range(256)
, and Numpy arithmetic is effectively doing % 256
on the values your code is producing.
这是您代码的经过修复的版本.
Here's a repaired version of your code.
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')
输出:circle.png
这篇关于Python模糊的图像创建,以创建从黑色到白色的精美彩色绘画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!