简体   繁体   中英

PIL images are saving in grayscale

I'm trying to generate some fractal images and I can do it fine. However, I have some problems when saving the images, because it saves them in grayscale instead of the colours. When I open them in python, it shows the right colours.

The code is as follows

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")

I'm using macOS X, Python 2.7.5.

You are creating images in L , or Luminance mode. That means they are greyscale images, one band of colour.

You need to use the RGB mode instead:

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

This does require you to use tuples of (R, G, B) values when specifying pixels, not a simple integer. The ImageDraw module does support setting colours using strings as well ( '#rrggbb' and related).

You don't show us how you define col in your code, so it is hard to state if you are using the correct format for an RGB image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM