简体   繁体   中英

RLE8 image support/decompression with Pillow (PIL fork)

I'm using Pillow (version 5.2.0) on Python3 to open both PNG and BMP images, and display them with a Tkinter GUI. The PNG images display correctly with no issues, however, I'm encountering an IOError ("Unsupported BMP compression") with some of the BMP images, when Pillow's BmpImagePlugin.py is used.

Using the bitmap plugin's source and some print statements, I found that the exception is thrown at line 193, and that the images causing the exception are compressed using RLE8 (denoted by the dictionary on line 63); all others work because they're a RAW format. It would seem to me that if a compression type is listed in that dictionary it should be supported, but apparently that isn't the case.

My question: is anyone aware of a workaround in Pillow or of any other python library that can open RLE8 bitmap images? Here's an image displaying my PATH environment, as well as the command-line error described in a comment below.

编辑:

Path issues

第二次编辑

I note that your first image ( test1.bmp ) appears to be corrupt and ImageMagick reports it has incorrect length.

Your second image does not appear to be compressed with RLE8 compression and also is a palettised image, but with alpha/transparency.

Your third image is palletised, non-alpha with RLE8 compression.

My version of PIL can read only the second file - the first and third, which are RLE encoded cannot be read.


You asked for a workaround - may I suggest pyvips which can read the files without issues:

import pyvips
from PIL import Image

# Load troublesome file using vips, and write to a memory buffer
image = pyvips.Image.new_from_file('test1.bmp')
mem_img = image.write_to_memory()

# Read from memory buffer into Numpy array
imgnp=np.frombuffer(mem_img, dtype=np.uint8).reshape(image.height, image.width, 3)
# Convert Numpy array to PIL Image and write to disk
Image.fromarray(imgnp).save('result.png')

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