如何为普通的pgm格式编写PIL图像滤镜?

5

我该如何为 Python Imaging Library 编写一个针对 PGM 纯 ASCII 格式(P2)的过滤器呢?问题在于,基本的 PIL 过滤器假定每个像素有恒定的字节数。

我的目标是使用 Image.open() 打开 feep.pgm。请参见http://netpbm.sourceforge.net/doc/pgm.html 或下方链接获得更多信息。

另一个解决方案是找到其他受 PIL 和所有主要图形程序支持的良好记录的灰度 ASCII 格式。有什么建议吗?

feep.pgm:

P2
# feep.pgm
24 7
15
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0
0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0
0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

编辑:谢谢您的回答,它有效...但是我需要使用Image.open()的解决方案。大多数Python程序都使用PIL进行图形处理(搜索:python image open)。因此,我需要能够向PIL注册滤镜。然后,我可以使用任何使用PIL的软件,如scipy、pylab等依赖程序。

编辑:好的,我现在明白了。下面是包装器pgm2pil.py:

import Image
import numpy

def pgm2pil(filename):

    try:
        inFile = open(filename)

        header = None
        size = None
        maxGray = None
        data = []

        for line in inFile:
            stripped = line.strip()

            if stripped[0] == '#': 
                continue
            elif header == None: 
                if stripped != 'P2': return None
                header = stripped
            elif size == None:
                size = map(int, stripped.split())
            elif maxGray == None:
                maxGray = int(stripped)
            else:
                for item in stripped.split():
                    data.append(int(item.strip()))

        data = numpy.reshape(data, (size[1],size[0]))/float(maxGray)*255
        return numpy.flipud(data)

    except:
        pass

    return None

def imageOpenWrapper(fname):
    pgm = pgm2pil(fname)
    if pgm is not None:
        return Image.fromarray(pgm)
    return origImageOpen(fname)

origImageOpen = Image.open
Image.open = imageOpenWrapper

对于misha的回答稍作修改。必须保存Image.open以防止无限循环。如果pgm2pil返回None,则包装器调用pgm2pil,pgm2pil返回None,然后调用pgm2pil...

下面是测试函数(feep_false.pgm是格式不正确的pgm,例如“P2”->“FOO”,而lena.pgm只是图像文件):

import pgm2pil
import pylab

try:
    pylab.imread('feep_false.pgm')
except IOError:
    pass
else:
    raise ValueError("feep_false should fail")

pylab.subplot(2,1,1)
a = pylab.imread('feep.pgm')
pylab.imshow(a)

pylab.subplot(2,1,2)
b = pylab.imread('lena.png')
pylab.imshow(b)

pylab.show()

谢谢您的回复。但是,为什么要返回numpy.flipud(data)?我只需要返回data。 - user1245262
很久以前我做过这个...原因是图像不在笛卡尔坐标系中(y轴翻转)。在我看来,图像应该在笛卡尔坐标系中而没有翻转(或者有翻转,我记不清了)。最终,在使用imread和imshow时,上述方法可以正确地显示图像。 - Juha
啊...原来如此。我应该想到的。再次感谢。 - user1245262
1个回答

6
我目前处理这个问题的方式是通过numpy
  1. 将图像读入2D的numpy数组中。您不需要使用numpy,但我发现它比常规Python 2D数组更容易使用
  2. 使用PIL.Image.fromarray将2D numpy数组转换为PIL.Image对象

如果您坚持使用PIL.Image.open,您可以编写一个包装器,首先尝试加载PGM文件(通过查看标头)。如果是PGM,则使用上述步骤加载图像,否则只需将责任移交给PIL.Image.open

下面是我用来将PBM图像转换为numpy数组的一些代码。
import re
import numpy

def pbm2numpy(filename):
    """
    Read a PBM into a numpy array.  Only supports ASCII PBM for now.
    """
    fin = None
    debug = True

    try:
        fin = open(filename, 'r')

        while True:
            header = fin.readline().strip()
            if header.startswith('#'):
                continue
            elif header == 'P1':
                break
            elif header == 'P4':
                assert False, 'Raw PBM reading not implemented yet'
            else:
                #
                # Unexpected header.
                #
                if debug:
                    print 'Bad mode:', header
                return None

        rows, cols = 0, 0
        while True:
            header = fin.readline().strip()
            if header.startswith('#'):
                continue

            match = re.match('^(\d+) (\d+)$', header)
            if match == None:
                if debug:
                    print 'Bad size:', repr(header)
                return None

            cols, rows = match.groups()
            break

        rows = int(rows)
        cols = int(cols)

        assert (rows, cols) != (0, 0)

        if debug:
            print 'Rows: %d, cols: %d' % (rows, cols)

        #
        # Initialise a 2D numpy array 
        #
        result = numpy.zeros((rows, cols), numpy.int8)

        pxs = []

        # 
        # Read to EOF.
        # 
        while True:
            line = fin.readline().strip()
            if line == '':
                break

            for c in line:
                if c == ' ':
                    continue

                pxs.append(int(c))

        if len(pxs) != rows*cols:
            if debug:
                print 'Insufficient image data:', len(pxs)
            return None

        for r in range(rows):
            for c in range(cols):
                #
                # Index into the numpy array and set the pixel value.
                #
                result[r, c] = pxs[r*cols + c]

        return result

    finally:
        if fin != None:
            fin.close()
        fin = None

    return None

您需要稍微修改它以适应您的目的,即:

  • 处理P2(ASCII,灰度)而不是P1(ASCII,二值)。
  • 如果您没有使用numpy,则使用其他容器。普通Python 2D数组也可以正常工作。

编辑

这是我如何处理包装器:

def pgm2pil(fname):
    #
    # This method returns a PIL.Image.  Use pbm2numpy function above as a
    # guide.  If it can't load the image, it returns None.
    #
    pass
    
def wrapper(fname):
    pgm = pgm2pil(fname)

    if pgm is not None:
        return pgm
    return PIL.Image.open(fname)

#
# This is the line that "adds" the wrapper
#
PIL.Image.open = wrapper

我没有编写pgm2pil,因为它与pgm2numpy非常相似。唯一的区别是它将结果存储在PIL.Image而不是numpy数组中。此外,我也没有测试包装器代码(抱歉,目前时间有点短),但这是一种非常常见的方法,所以我认为它应该可以工作。

现在,听起来你想让使用PIL进行图像加载的其他应用程序能够处理PGMs。使用上述方法是可能的,但您需要确保在第一次调用PIL.Image.open之前添加上述包装器代码。如果您有权限,您可以通过将包装器源代码添加到PIL源代码中来确保发生这种情况。


那么问题是:我该如何编写一个包装器?以及我该如何告诉Python我有一个用于 PIL.image.open 的包装器? - Juha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接