Working with .bmp files in python 3

I have a bmp file. This is just a red square. I need to write a program with functions so that it has white stripes. What do I need to do:

  • download the bmp file.
  • read and rate the bmp file.
  • encode certain areas of the coordinates of the file that will be painted white.
  • close file
  • displays the final product file as output

I am new and have problems reading or displaying the original bmp file, not to mention editing the contents inside. it is not like opening a txt file and "readline ()". Also, when I copy the insert of the bmp file to the spdev folder of the pydev scl projects in eclipse, it does not appear in eclipse, so I don’t know if the computer recognizes that the file is there. I want to read about this before posting here, but I don't seem to get a lot of search engine results, since I'm not sure exactly what I should look for.

+6
source share
1 answer

An easy way to do this is with a third-party image processing library like PIL / Pillow . The code is simple enough so you can understand it just minutes from the examples in Image in documents ...

But if you are not allowed to do this, let's see how to do it manually.

Firstly, BMP is not a text file format, it is a binary format. This means you must read it in binary mode. And you cannot read it “line by line” because it does not have lines to read. Since the bytes object is not modified, you probably want to copy it to a bytearray to work. So:

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

Then you need to analyze the BMP file format. I assume that the main point of the exercise is to figure out how to do it yourself, so I will give you a link to a Wikipedia article that describes it better than Microsoft docs, and you can go from there.

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

I assume that you can ignore all options for BMP compression, otherwise this would be too complicated a destination. In fact, you can simply assume that all the headers will indicate the most common option and only the code for this. But you can ask your teacher about it.

Now that you have found part of the “pixel array” in BMP, and you understand how to interpret it from the DIB header, you can just set the pixels to white depending on what you want by setting the values ​​in the corresponding bytearray indices. For example, this 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, as soon as you change your red pixels to white, you can save them with:

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

Source: https://habr.com/ru/post/959014/


All Articles