如何编写普通pgm格式的PIL图像过滤器?

2024-05-23 19:14:34 发布

您现在位置:Python中文网/ 问答频道 /正文

如何为python映像库编写pgm纯ascii格式(P2)的筛选器。这里的问题是,基本的PIL过滤器假定每个像素的字节数不变。

我的目标是用Image.open()打开feep.pgm。请参阅http://netpbm.sourceforge.net/doc/pgm.html或下面的内容。

另一种解决方案是,我发现其他有良好记录的ascii灰度格式是由PIL和所有主要图形程序支持的。有什么建议吗?

测隙规:

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进行图形操作(google: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

米莎的回答稍有改进。必须保存Image.open以防止循环永不结束。如果pgm2pil返回None包装调用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()

Tags: imageimportnonedatasizereturnpilopen
1条回答
网友
1楼 · 发布于 2024-05-23 19:14:34

我目前处理这个问题的方式是通过numpy

  1. 将图像读入2Dnumpy数组。使用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进行图像加载的其他应用程序能够处理pgm。使用上述方法是可能的,但是您需要确保在第一次调用PIL.Image.open之前添加了上述包装器代码。您可以通过将包装器源代码添加到PIL源代码(如果您有访问权限)来确保这一点。

相关问题 更多 >