Image.open()打开图片显示全白,如何修复及获取正确像素数组?
Hey Vinny, sorry you're hitting this weird issue with Pillow showing a pure white image instead of your actual picture. I’ve dealt with similar headaches before, so let’s walk through the most likely causes and fixes, plus some alternative libraries you can try.
1. Wrong File Path or Corrupted Image
First things first—double-check the basics:
- Print the
filenamevariable to confirm it’s pointing to the correct image file:print(filename). A tiny typo in the path (like missing a folder name) can lead Pillow to load an empty/blank image (or sometimes it’ll throw an error, but not always for corrupted files). - Manually open the image with a standard viewer (like Photos on Windows, Preview on Mac) to make sure the image itself isn’t actually pure white (yes, I’ve facepalmed over this before).
- If the image is corrupted, re-download or restore a working copy of the file.
2. Transparency/Image Mode Conflicts
If your image uses an RGBA mode (with a transparency channel), some default image viewers used by im.show() will render transparent areas as white. Here’s how to fix it:
- Convert the image to RGB mode before showing it:
from PIL import Image im = Image.open(filename).convert('RGB') im.show() - You can check the image mode first with
print(im.mode)—if it’sRGBA,L(grayscale), or another non-RGB mode, converting to RGB often resolves display quirks.
3. Pillow Version or Platform Display Bugs
Older versions of Pillow can have bugs with im.show() on certain operating systems (especially Linux, where it relies on system image viewers). Try these steps:
- Upgrade Pillow to the latest version:
pip install --upgrade pillow - Verify if the image data is actually correct by saving it to a file instead of using
im.show():
If the savedim = Image.open(filename) im.save('test_output.png')test_output.pnglooks normal, the issue is with theim.show()method, not the image loading. You can use a different viewer or one of the alternative libraries below to display it.
4. Unsupported Image Formats
Pillow might struggle with rare or specialized formats (like some compressed TIFFs, RAW camera files, or proprietary formats). Fixes here include:
- Convert the image to a common format (PNG/JPG) using another tool first, then load it with Pillow.
- For formats like TIFF, make sure you have the required system dependencies installed (e.g.,
libtiff5-devon Debian/Ubuntu), then reinstall Pillow to enable support.
If Pillow isn’t working out for you, these libraries are reliable for loading images and accessing their pixel data:
1. OpenCV (cv2)
OpenCV is the go-to for computer vision tasks, and it handles image loading consistently:
import cv2 # Load image (default is BGR mode; convert to RGB if needed) img = cv2.imread(filename) img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Display the image cv2.imshow('Loaded Image', img_rgb) cv2.waitKey(0) # Wait for a key press to close the window cv2.destroyAllWindows() # Access the pixel array directly print(img_rgb) # Will show non-255 values if the image isn't white
2. Matplotlib
Matplotlib is primarily for plotting, but it’s great for quick image loading and display:
import matplotlib.pyplot as plt import matplotlib.image as mpimg img = mpimg.imread(filename) plt.imshow(img) plt.show() # The pixel array is stored in `img` print(img)
3. scikit-image
Perfect for scientific image processing tasks, with robust loading capabilities:
from skimage import io img = io.imread(filename) io.imshow(img) io.show() # Get the pixel array print(img)
内容的提问来源于stack exchange,提问作者Vinny Jacobsen

