Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"python.pythonPath": "/home/evyhaan/.local/share/virtualenvs/bitmap-cli-transformer-T-wSHdGM/bin/python",
"python.pythonPath": "/home/milo/.local/share/virtualenvs/bitmap-cli-transformer-g2MSi3y4/bin/python",
"python.linting.pylintEnabled": false,
"python.linting.pep8Enabled": false,
"python.linting.enabled": true
Expand Down
Binary file added assets/spock.bmp
Binary file not shown.
Binary file removed assets/spock.jpg
Binary file not shown.
5 changes: 4 additions & 1 deletion cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@
bitmap.make_grayscale()
for transform in transforms:
method = getattr(bitmap, transform)
bitmap.save_new(method())
if transform == 'custom_grayscale':
bitmap.save_custom(method())
else:
bitmap.save_new(method())
51 changes: 44 additions & 7 deletions modify.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def __init__(self, file_path):
"""
self.file_path = file_path
self.img = Image.open(self.file_path)
with open(file_path, 'rb') as file:
self.bin_img = bytearray(file.read())

def new_file_path(self, mod_type):
"""
Expand All @@ -26,6 +28,42 @@ def make_grayscale(self, img='self.img'):
img = self.img.convert('L')
return [new_path, img]

def _make_bi_int(self, byts):
return int.from_bytes(byts, byteorder='little')

def custom_grayscale(self, img='self.img'):
new_path = self.new_file_path('_grayscale')

file_header = self.bin_img[:14]
img_header_size = self._make_bi_int(self.bin_img[14:18])
img_header = self.bin_img[14:14 + img_header_size]
img_pix_location = self._make_bi_int(file_header[10:])
img_pix = self.bin_img[img_pix_location:]
img_width = self._make_bi_int(img_header[4:6])
img_height = self._make_bi_int(img_header[8:10])
img_bpp = self._make_bi_int(img_header[14:16])
img_cols = img_width * 3
img_cols_pad = ((img_cols + 3) & ~0x03) - img_cols

counter = 0
for i in range(0, len(img_pix) - 1):
if counter < img_cols and counter % 3 == 0:
b = img_pix[i]
g = img_pix[i + 1]
r = img_pix[i + 2]
avg = int(round((r + g + b) / 3))
img_pix[i] = avg
img_pix[i + 1] = avg
img_pix[i + 2] = avg
if counter == img_cols + img_cols_pad:
counter = 0
else:
counter += 1

new_bitmap = file_header + img_header + img_pix
return [new_bitmap, new_path]


def flip_horizontal(self, img='self.img'):
"""
Modify the input image with flip horizontal and returns the new path with the new image
Expand Down Expand Up @@ -66,12 +104,11 @@ def save_new(self, lst):
"""
lst[1].save(lst[0], 'bmp')

if __name__ == "__main__":
def save_custom(self, img_path):
with open(img_path[1], 'wb') as file:
file.write(img_path[0])

tiger = Bitmap('./assets/tiger.bmp')
if __name__ == "__main__":

tiger.save_new(tiger.make_grayscale())
tiger.save_new(tiger.flip_horizontal())
tiger.save_new(tiger.flip_vertical())
tiger.save_new(tiger.make_thumbnail())
tiger.save_new(tiger.invert_colors())
test = Bitmap('./assets/spock.bmp')
test.custom_grayscale()