创建二进制PBM/PGM/PPM [英] Create binary PBM/PGM/PPM

查看:196
本文介绍了创建二进制PBM/PGM/PPM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何创建二进制PBM/PGM/PPM文件.据我所知,每种格式有两种类型:普通格式和原始格式.例如,黑色PBM 5x5的结构如下所示:

I'm trying to understand how to create binary PBM/PGM/PPM files. As I know there are two types for each format: plain and raw. For example, structure of black PBM 5x5 looks like this:

P1
# This is a comment
5 5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

因此,您看到的很简单:白色为0,黑色为1.但是,PBM具有原始版本,如下所示:

So as you see it is simple: white is 0, black is 1. However, PBM has raw version, which looks like this:

'P4\n# This is a comment\n5 5\n\xf8\xf8\xf8\xf8\xf8'

我该怎么办? PBM格式说明如下:

How can I do it? Description of PBM format says:

A raster of Height rows, in order from top to bottom. Each row is Width bits, packed 8 to a byte, with don't care bits to fill out the last byte in the row. Each bit represents a pixel: 1 is black, 0 is white. The order of the pixels is left to right. The order of their storage within each file byte is most significant bit to least significant bit. The order of the file bytes is from the beginning of the file toward the end of the file. A row of an image is horizontal. A column is vertical. The pixels in the image are square and contiguous.

A raster of Height rows, in order from top to bottom. Each row is Width bits, packed 8 to a byte, with don't care bits to fill out the last byte in the row. Each bit represents a pixel: 1 is black, 0 is white. The order of the pixels is left to right. The order of their storage within each file byte is most significant bit to least significant bit. The order of the file bytes is from the beginning of the file toward the end of the file. A row of an image is horizontal. A column is vertical. The pixels in the image are square and contiguous.

我不明白我需要做什么;我怀疑我可能需要使用structarray.array,但是我不确定.我需要你的帮助;您可以在Python中举一个如何创建此类文件的示例吗?

I don't understand what I need to do; I suspect that I may need to use struct or array.array, but I'm not sure. I need your help; could you give an example in Python how to create such file?

>>> size = (5, 5)
>>> array = [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]
>>> create_pbm(size, array)
'P4\n5 5\n\xf8\xf8\xf8\xf8\xf8'

我需要一个很好的速度,因为我需要处理更大的图像(例如2000x5000).但是问题是我需要使用没有ctypes和库的纯Python.请给我一个小例子,说明如何创建二进制PBM文件吗?

I need a good speed, because I need to process much larger images (e.g. 2000x5000). But the problem is that I need to use pure Python, without ctypes and libraries. Could you help me, please, and give a small example how to create binary PBM files?

如果您能告诉我有关二进制PGM和PPM处理的信息,那将更加令人惊奇.

It will be even more amazing, if you can tell me about binary PGM and PPM processing.

谢谢!

推荐答案

我确定这里有很多改进的空间(就效率而言),但这是否可以正常工作?

I'm sure there's lots of room for improvement here, (in terms of efficiency) but does this work correctly?

import struct
def create_pbm(size,lst):
    out = ['P4\n'+' '.join(map(str,size))+'\n'] #header
    for j in xrange(0,len(lst),size[1]):
        #single row of data
        row = lst[j:j+size[1]]
        #padded string which can be turned into a number with `int`
        s = ''.join(map(str,row))+'0000000'
        #Turn the string into a number and pack it (into unsigned int) using struct. 
        vals = [struct.pack('B',int(s[i*8:(i+1)*8],2)) for i in xrange(size[0]//8+1) ]
        out.append(''.join(vals))
    return ''.join(out)

a = [1]*25 #flat black image.
print repr(create_pbm((5,5),a))

编辑

至于阅读,这似乎可行:

As for reading, this seems to work:

def read_pbm(fname):
    with open(fname) as f:
        data = [x for x in f if not x.startswith('#')] #remove comments
    p_whatever = data.pop(0)  #P4 ... don't know if that's important...
    dimensions = map(int,data.pop(0).split())
    arr = []
    col_number = 0
    for c in data.pop(0):
        integer = struct.unpack('B',c)[0]
        col_number += 8
        bits = map(int,bin(integer)[2:])
        arr.extend(bits[:min(8,dimensions[0]-col_number)])
        if(col_number > dimensions[0]):
            col_number = 0 

    return (dimensions, arr)

这些文件格式需要为正方形吗?这似乎不太可能.我很可能在维度部分中混合了行/列.随时检查;-).

Do these file formats need to be square? that seems unlikely. It's quite possible that I got rows/columns mixed up in the dimensions portion. Feel free to check that ;-).

这篇关于创建二进制PBM/PGM/PPM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆