18

有没有办法在不涉及使用 PIL 的 Python 中读取 bmp 文件?PIL 不适用于我拥有的版本 3。我尝试使用 graphics.py 中的 Image 对象 Image(anchorPoint, filename),但这似乎只适用于 gif 文件。

4

8 回答 8

15

在 Python 中,它可以简单地理解为:

import os
from scipy import misc
path = 'your_file_path'
image= misc.imread(os.path.join(path,'image.bmp'), flatten= 0)

## flatten=0 if image is required as it is 
## flatten=1 to flatten the color layers into a single gray-scale layer
于 2014-04-01T19:11:35.803 回答
7

我意识到这是一个老问题,但我在自己解决这个问题时发现了它,我认为这可能会在未来对其他人有所帮助。

实际上,将 BMP 文件作为二进制数据读取非常容易。当然,这取决于您需要支持的支持范围和极端情况。

下面是一个简单的解析器,仅适用于 1920x1080 24 位 BMP(如从 MS Paint 中保存的)。不过应该很容易扩展。它以 python 列表的形式输出像素值,(255, 0, 0, 255, 0, 0, ...)例如红色图像。

如果您需要更强大的支持,请参阅有关如何正确读取此问题的标题的信息:How to read bmp file header in python? . 使用这些信息,您应该能够使用您需要的任何功能来扩展下面的简单解析器。

如果您需要,还有更多关于 BMP 文件格式的信息,请访问 wikipedia https://en.wikipedia.org/wiki/BMP_file_format 。

def read_rows(path):
    image_file = open(path, "rb")
    # Blindly skip the BMP header.
    image_file.seek(54)

    # We need to read pixels in as rows to later swap the order
    # since BMP stores pixels starting at the bottom left.
    rows = []
    row = []
    pixel_index = 0

    while True:
        if pixel_index == 1920:
            pixel_index = 0
            rows.insert(0, row)
            if len(row) != 1920 * 3:
                raise Exception("Row length is not 1920*3 but " + str(len(row)) + " / 3.0 = " + str(len(row) / 3.0))
            row = []
        pixel_index += 1

        r_string = image_file.read(1)
        g_string = image_file.read(1)
        b_string = image_file.read(1)

        if len(r_string) == 0:
            # This is expected to happen when we've read everything.
            if len(rows) != 1080:
                print "Warning!!! Read to the end of the file at the correct sub-pixel (red) but we've not read 1080 rows!"
            break

        if len(g_string) == 0:
            print "Warning!!! Got 0 length string for green. Breaking."
            break

        if len(b_string) == 0:
            print "Warning!!! Got 0 length string for blue. Breaking."
            break

        r = ord(r_string)
        g = ord(g_string)
        b = ord(b_string)

        row.append(b)
        row.append(g)
        row.append(r)

    image_file.close()

    return rows

def repack_sub_pixels(rows):
    print "Repacking pixels..."
    sub_pixels = []
    for row in rows:
        for sub_pixel in row:
            sub_pixels.append(sub_pixel)

    diff = len(sub_pixels) - 1920 * 1080 * 3
    print "Packed", len(sub_pixels), "sub-pixels."
    if diff != 0:
        print "Error! Number of sub-pixels packed does not match 1920*1080: (" + str(len(sub_pixels)) + " - 1920 * 1080 * 3 = " + str(diff) +")."

    return sub_pixels

rows = read_rows("my image.bmp")

# This list is raw sub-pixel values. A red image is for example (255, 0, 0, 255, 0, 0, ...).
sub_pixels = repack_sub_pixels(rows)
于 2017-11-20T12:34:06.927 回答
2

我必须在一个项目上工作,我需要使用 python 读取 BMP 文件,这很有趣,实际上最好的方法是审查 BMP 文件格式(https://en.wikipedia.org/wiki/ BMP_file_format)然后将其作为二进制文件读取,以提取数据。

您将需要使用 struct python 库来执行提取

您可以使用本教程查看它是如何进行的https://youtu.be/0Kwqdkhgbfw

于 2020-05-27T04:37:04.360 回答
1

如果您在 Windows 中执行此操作,该站点应该允许您启动 PIL(和许多其他流行的包)并在大多数版本的 Python 中运行:用于 Python 扩展包的非官方 Windows 二进制文件

于 2012-05-03T21:26:36.270 回答
1

PIL 到 Python 3.x 的通用端口称为“Pillow”。另外,我建议使用 pygame 库来完成简单的任务。它是一个库,充满了创建游戏的功能——从一些常见的图像格式中读取就是其中之一。也适用于 Python 3.x。

于 2013-08-04T22:28:54.457 回答
1

这取决于您要实现的目标以及在哪个平台上?

无论如何,使用 C 库加载 BMP 可能会起作用,例如http://code.google.com/p/libbmp/http://freeimage.sourceforge.net/,并且可以从 python 轻松调用 C 库,例如使用 ctypes 或将其包装为 python 模块。

或者你可以编译这个版本的 PIL https://github.com/sloonz/pil-py3k

于 2012-05-03T21:14:19.047 回答
1

为此使用枕头。安装后只需导入即可

from PIL import Image

然后就可以加载BMP文件了

img = Image.open('path_to_file\file.bmp')

如果您需要图像是一个 numpy 数组,请使用np.array

img = np.array(Image.open('path_to_file\file.bmp'))

numpy 数组只会是一维的。如果您的图像是 RGB,请使用reshape()将其调整为正确的形状。例如:

np.array(Image.open('path_to_file\file.bmp')).reshape(512,512,3)
于 2020-12-20T17:37:25.023 回答
0

使用优秀的 matplotlib 库

import matplotlib.pyplot as plt
im = plt.imread('image.bmp')
于 2021-05-20T12:42:43.783 回答