gpt4 book ai didi

python - 使用 python 在 ppm 上从黑色到红色渐变

转载 作者:行者123 更新时间:2023-12-01 05:49:06 25 4
gpt4 key购买 nike

Python

如何操作此代码来为我提供一个从左到右从黑色渐变为红色的图像,其中左侧为黑色,向右渐变为红色。

def main():
f = open("testImage.ppm","w")
f.write("P3 \n")
width=256
height=256
f.write("%d %d \n"%(width,height))
f.write("255 \n")
for i in range(width*height):
x=i%256
y=i/256
f.write("255, 0, 0 \n")
f.close()

main()

最佳答案

在你的循环中,你总是写255, 0, 0。这是红、绿、蓝三色。循环上方写入 255 指定最大值为 255。图像的宽度为 256 像素。 [0, 255] 范围内有 256 个值。因此,很容易推断出红色分量应该是 X 值。您可以将代码修改为如下所示:

f.write("%d, 0, 0 \n" % x)

关于python - 使用 python 在 ppm 上从黑色到红色渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15057700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com