在python 3中使用.bmp文件 [英] working with .bmp files in python 3

查看:211
本文介绍了在python 3中使用.bmp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bmp文件.它只是一个红色正方形.我必须编写一个带有函数的程序,使其具有白色条纹.我需要做的事情:

I have a bmp file. It is just a red square. I have to write a program with functions to make it have white stripes. Things I would need to do:

  • 加载bmp文件.
  • 阅读并评估bmp文件.
  • 将文件的某些区域的坐标编码为白色.
  • 关闭文件
  • 将最终产品文件显示为输出

我是新手,在读取或显示原始bmp文件时遇到问题,更不用说在其中编辑内容了.它与打开txt文件和"readline()"不同.另外,当我将bmp文件复制粘贴到eclipse中的pydev项目src文件夹中时,它不会出现在eclipse上,因此我不知道计算机如何识别该文件.我想在此处发布之前对其进行仔细阅读,但是我似乎并没有获得太多搜索结果,因为我不确定我到底应该搜索什么.

i am a novice, and am having trouble reading or displaying the original bmp file, let alone edit the content inside. it is not similar to opening a txt file and "readline()". also, when i copy paste the bmp file in the pydev projects src folder in eclipse, it does not show up on eclipse, so i don't know if how the computer would recognize that the file is there. i want to read up on it before posting here, but i don't seem to get much results googling, since i am not sure exactly what i should search for.

推荐答案

最简单的方法是使用第三方图像处理库,例如 Image 模块文档...

The easy way to do this is with a third-party image-processing library like PIL/Pillow. The code is simple enough that you could figure it out in a few minutes from the examples on the Image module docs…

但是如果您不允许这样做,让我们看看如何手动进行.

But if you're not allowed to do that, let's look at how to do this manually.

首先,BMP不是文本文件格式,而是 binary 格式.这意味着您必须以二进制模式读取它.而且您不能逐行"阅读它,因为它没有要阅读的文本行.由于bytes对象是不可变的,因此您可能需要将其复制到bytearray中进行处理.所以:

First, BMP isn't a text file format, it's a binary format. That means you have to read it in binary mode. And you can't read it "line by line", because it doesn't have lines of text to read. Since a bytes object isn't mutable, you will probably want to copy it into a bytearray to work with. So:

with open('spam.bmp', 'rb') as f:
    data = bytearray(f.read())

接下来,您需要解析BMP文件格式.我认为练习的重点是自己弄清楚该怎么做,所以我将为您提供指向 Wikipedia文章的链接,它比Microsoft文档更好地描述了它,您可以从那里开始.

Next, you need to parse the BMP file format. I assume the main point of the exercise is figuring out how to do that yourself, so I'll give you a link to Wikipedia's article, which describes it better than the Microsoft docs, and you can go from there.

标准库中的 struct 模块对于解释标头非常有帮助;与通过读取data[offset]data[offset+1]等并将它们重新组合为32位数字相比,使用struct.unpack_from('<L', data, offset)读取32位小尾数要容易得多.

The struct module in the standard library will be very helpful for interpreting the headers; it's much easier to read a 32-bit little-endian number with struct.unpack_from('<L', data, offset) than with by reading data[offset], data[offset+1], etc. and re-combining them into a 32-bit number.

我猜您可以忽略BMP压缩的所有选项-否则,这将很难分配.实际上,您可能只是假设所有标头都将指定最常见的变体,并且只为此指定代码.但是您可能想请教您的老师.

I'm guessing you can ignore all the options for BMP compression—otherwise, this would be way too hard an assignment. In fact, you can probably just assume that all of the headers will specify the most common variant and only code for that. But you might want to ask your teacher for feedback on that.

现在,一旦找到了BMP的像素阵列"部分,并且已经找到了如何从DIB标头中对其进行解释的方法,则可以通过将BMP设置为任意位置,将像素设置为白色.字节数组的适当索引处的值.例如,结果可能很简单:

Now, once you've found the "pixel array" portion of the BMP, and you've figured out how to interpret it from the DIB header, you can just set pixels to white at whichever positions you want by setting the values at the appropriate indexes of the bytearray. For example, it may turn out to be as simple as:

pos = pixel_array_offset + row_size * y + pixel_size * x
data[pos:pos+3] = 255, 255, 255

最后,将红色像素更改为白色后,可以使用以下方法进行保存:

Finally, once you've changed your red pixels to white, you can save it with:

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

这篇关于在python 3中使用.bmp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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