PIL图像保存为灰色

2024-04-27 04:00:58 发布

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

我正在尝试生成一些分形图像,我可以做得很好。但是,我在保存图像时遇到了一些问题,因为它将图像保存为灰度而不是颜色。当我用python打开它们时,它会显示正确的颜色。

代码如下

from PIL import Image
import ImageDraw
from scipy import misc
from array import *
import matplotlib.pyplot as plt
import scipy

image = Image.new("L",(SIZE, SIZE)) # create a image SIZE x SIZE
d = ImageDraw.Draw(image)
#iterate over x and y, setting a col value for each pixel
d.point((x, y), col ) # it then colors the point (x,y)
image.save("beta"+str(alpha)+".png", "PNG")

我使用的是MacOSX,Python2.7.5。


Tags: 代码from图像imageimportsizepil颜色
1条回答
网友
1楼 · 发布于 2024-04-27 04:00:58

您正在L或亮度模式下创建图像。这意味着它们是灰度图像,一个色带。

您需要改用RGB模式:

image = Image.new("RGB", (SIZE, SIZE))

这确实要求在指定像素时使用(R, G, B)值的元组,而不是简单的整数。ImageDraw模块也支持使用字符串('#rrggbb'和相关字符串)设置颜色。

您没有向我们展示如何在代码中定义col,因此很难说明是否对RGB图像使用了正确的格式。

相关问题 更多 >