简体   繁体   中英

PIL Image opening turns white pixels into black pixels

When I open a FULLY WHITE file with pillow ( from PIL import Image ) and then obtain the color of all of the pixels, you SHOULD see something like [255, 255, 255, 255, 255, 255]..... but instead i only see [0, 0, 0, 0, 0, 0]..... , code is as follows:

from PIL import Image
image = Image.open("index.png", "r")
pixels = list(image.getdata())
print(pixels)

Your code doesn't convert white pixels values to black pixels. It somehow represents pixel values in different way. We can check whether it converts white pixel values to black pixels using RGB color domain. Code is shown below:

from PIL import Image
import numpy as np
img = Image.open("index.png") # open colour image


imgRgb = img.convert('RGB')
pixels = list(imgRgb.getdata())
width, height = imgRgb.size
pixels = np.asarray([pixels[i * width:(i + 1) * width] for i in range(height)], dtype=int)

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