From b256a262f99e3cc977bbbb70792977a07c51c7eb Mon Sep 17 00:00:00 2001 From: Robin Nicole Date: Tue, 19 Nov 2024 10:05:06 +0100 Subject: [PATCH 1/2] Changed to absolute values --- bitmap.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bitmap.py b/bitmap.py index efc4237..28fb963 100644 --- a/bitmap.py +++ b/bitmap.py @@ -13,22 +13,22 @@ class Bitmap(object): """Create a bitmap image file (.bmp). - + Usage is simple: >>> from bitmap import Bitmap >>> bmp = Bitmap(10, 10, fill=(0, 100, 255)) # (r, g, b) background >>> bmp.pencil(1, 1, (128, 128, 128)) >>> bmp.save('test.bmp') """ - + def __init__(self, width, height, fill=(0, 0, 0)): """Intialize the bitmap file.""" self._width = width self._height = height self._pixels = [ - [fill for y in range(self._height)] - for x in range(self._width) + [fill for _ in range(self._height)] + for _ in range(self._width) ] self._row_pad = (self._width*3) % 4 @@ -53,7 +53,8 @@ def save(self, path): def _write_header(self, f): """Write the bitmap file header.""" - file_size = 0x36 + ((self._width + self._row_pad)*self._height*3) + file_size = 0x36 + ((abs(self._width) + + self._row_pad)*abs(self._height)*3) map( f.write, @@ -65,7 +66,9 @@ def _write_header(self, f): bytearray([0x36, 0x00, 0x00, 0x00]), # Data offset # DIB header, BITMAPINFOHEADER bytearray([0x28, 0x00, 0x00, 0x00]), # DIB header size + # TODO: Change according to header version struct.pack(' Date: Tue, 19 Nov 2024 10:39:04 +0100 Subject: [PATCH 2/2] Added an absolute value --- .gitignore | 1 + bitmap.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/bitmap.py b/bitmap.py index 28fb963..961f442 100644 --- a/bitmap.py +++ b/bitmap.py @@ -83,7 +83,7 @@ def _write_header(self, f): def _write_pixels(self, f): """Write the array of pixels to the file.""" - for y in reversed(range(self._height)): + for y in reversed(range(abs(self._height))): row = [self._pixels[x][y] for x in range(self._width)] row = [reversed(color) for color in row] row = list(itertools.chain.from_iterable(row))