简体   繁体   中英

How to use greyscale color in PIL's putpixel?

I need a function that creates the whitest value for 0 and the blackest value for 1 (all other are, like 0.5, are some tone of grey) to put in putpixel but I have no idea how to do it.

value = Greyscale(0.5) #or any other in the range of 0 and 1.
Image.putpixel((X, Y), (value))

How can I implement this? Or is there already a function for that? I searched the docs but couldn't find anything useful.

Depends on the bit depth of the L type image the values range from 0 to 255 with 8 bit depth.

For 8 bit depth the function is like:

def inverted_grayscale(v): 
    return int((1 - v) * 255)

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