out文件不是根据python中的代码

时间:2015-05-30 18:50:36

标签: python

我已经编写了这段代码来打印棋盘。 根据此代码,当x <256且y <256(第一个框)时,将执行代码的 Last ELSE 部分,其设置rgb =(255,255,255),这是rgb的白色,但输出图像显示第一个框为黑色,请帮忙。

守则是:

import struct

image_width = 512
image_height = 512
box_width = 256
box_height = 256
bits_per_pixel = 24

# bitmap file header
data = 'BM' # Windows 3.1x, 95, NT, ... etc.
data += struct.pack('i', bits_per_pixel / 8 * image_width * image_height) # The size of the BMP file in bytes = header + (color bytes * w * h)
data += struct.pack('h', 0) # Reserved
data += struct.pack('h', 0) # Reserved
data += struct.pack('i', 0) # The offset, i.e. starting address, of the byte where the bitmap image data (pixel array) can be found.

# bitmap header (BITMAPCOREHEADER)
data += struct.pack('i', 12) # The size of this header (12 bytes)
data += struct.pack('h', image_width) # width
data += struct.pack('h', image_height) # height
data += struct.pack('h', 1) # The number of color planes, must be 1
data += struct.pack('h', bits_per_pixel) # The number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32.

def rgb_to_str(r, g, b):
    return chr(r) + chr(g) + chr(b)

for y in xrange(image_height):
    for x in xrange(image_width):
        if (y / box_height) % 2:
            if (x / box_width) % 2:
                data += rgb_to_str(255, 255, 255)
            else:
                data += rgb_to_str(0, 0, 0)
        else:
            if (x / box_width) % 2:                
                data += rgb_to_str(0, 0, 0)
            else:
                data += rgb_to_str(255, 255, 255)

with open('out.bmp', 'wb') as f:
    f.write(data)

1 个答案:

答案 0 :(得分:3)

实际上,您似乎将我的代码从问题python code without libraries的答案中复制了 但是您忘记设置偏移量,它必须是26,但在您的版本中它是0

正确的位图文件头必须是:

# bitmap file header
data = 'BM' # Windows 3.1x, 95, NT, ... etc.
data += struct.pack('i', 26 + bits_per_pixel / 8 * image_width * image_height) # The size of the BMP file in bytes = header + (color bytes * w * h)
data += struct.pack('h', 0) # Reserved
data += struct.pack('h', 0) # Reserved
data += struct.pack('i', 26) # The offset, i.e. starting address, of the byte where the bitmap image data (pixel array) can be found.

顺便说一下,我发现了一个错误。 BMP的色彩映射是BGR(不是RGB),所以要写入彩色像素使用函数:

def rgb_to_bmp_data(r, g, b):
    return chr(b) + chr(g) + chr(r)

要修复颜色框,请尝试使用:

import math
row_bytes = width * (bits_per_pixel / 8)
row_padding = int(math.ceil(row_bytes / 4.0)) * 4 - row_bytes

for y in xrange(image_height - 1, -1, -1):
    for x in xrange(image_width):
        if (y / box_height) % 2:
            if (x / box_width) % 2:
                data += rgb_to_bmp_data(255, 255, 255)  # x=1, y=1
            else:
                data += rgb_to_bmp_data(0, 0, 0)  # x=0, y=1
        else:
            if (x / box_width) % 2:
                data += rgb_to_bmp_data(0, 0, 0)  # x=1, y=0
            else:
                data += rgb_to_bmp_data(255, 255, 255)  # x=0, y=0
    data += '\x00' * row_padding

现在根据代码:

enter image description here

我添加了x和y值。