From a9518740b59c19ed03995c3e914dd7afc2de71ed Mon Sep 17 00:00:00 2001 From: TilCreator <tilman.jackel@gmx.de> Date: Fri, 30 Aug 2019 15:56:52 +0200 Subject: [PATCH] Fix code style --- tools/image_to_pixels.py | 61 +++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/tools/image_to_pixels.py b/tools/image_to_pixels.py index 2ee6ec9b..8f9be49f 100644 --- a/tools/image_to_pixels.py +++ b/tools/image_to_pixels.py @@ -3,12 +3,30 @@ from copy import deepcopy import argparse -parser = argparse.ArgumentParser(description='Converts images to card10 parsable images') -parser.add_argument('--ignore_alpha', '-a', dest='alpha', action='store_false', help='Ignore alpha') -parser.add_argument('--use_exact_alpha', '-e', dest='exact_alpha', action='store_true', help='Use exact alpha') -parser.add_argument('--debug', '-d', dest='debug', action='store_true', help='Debug') -parser.add_argument('--background', '-b', type=lambda hexCode : tuple([int(hexCode.lstrip('#')[i:i+2], 16) for i in (0, 2, 4)]), default='000000', help='Background color in rgb hex') -parser.add_argument('images', nargs='*', default=[], help='path to images') +parser = argparse.ArgumentParser( + description="Converts images to card10 parsable images" +) +parser.add_argument( + "--ignore_alpha", "-a", dest="alpha", action="store_false", help="Ignore alpha" +) +parser.add_argument( + "--use_exact_alpha", + "-e", + dest="exact_alpha", + action="store_true", + help="Use exact alpha", +) +parser.add_argument("--debug", "-d", dest="debug", action="store_true", help="Debug") +parser.add_argument( + "--background", + "-b", + type=lambda hexCode: tuple( + [int(hexCode.lstrip("#")[i : i + 2], 16) for i in (0, 2, 4)] + ), + default="000000", + help="Background color in rgb hex", +) +parser.add_argument("images", nargs="*", default=[], help="path to images") a = parser.parse_args() @@ -16,15 +34,15 @@ if a.debug: print(a) for path in a.images: - img = Image.open(path, 'r') + img = Image.open(path, "r") mask = deepcopy(img) mask_p = mask.load() - bg = Image.new('RGB', img.size, a.background) + bg = Image.new("RGB", img.size, a.background) bg.paste(img, None, img) img = bg - + for y in range(img.size[1]): for x in range(img.size[0]): if not a.alpha: @@ -32,14 +50,17 @@ for path in a.images: elif not a.exact_alpha: mask_p[x, y] = (*mask_p[x, y][:3], (mask_p[x, y][3] // 255) * 255) else: - mask_p[x, y] = (*mask_p[x, y][:3], min((mask_p[x, y][3] // 1) * 255, 255)) + mask_p[x, y] = ( + *mask_p[x, y][:3], + min((mask_p[x, y][3] // 1) * 255, 255), + ) if a.debug: - #mask.show() + # mask.show() img.show() - show_img = Image.new('RGBA', img.size, (0, 0, 0, 0)) + show_img = Image.new("RGBA", img.size, (0, 0, 0, 0)) show_img.paste(img, None, mask) - #show_img.show() + # show_img.show() data = bytearray() @@ -48,10 +69,10 @@ for path in a.images: for y in range(img.size[1]): for x in range(img.size[0]): if mask_p[x, y][3] > 0: - msb=(img_p[x, y][0] & 0xF8) | (img_p[x, y][1] >> 5); - lsb=((img_p[x, y][1] & 0x1C) << 3) | (img_p[x, y][2] >> 3); - data.extend(x.to_bytes(length=1, byteorder='big')) - data.extend(y.to_bytes(length=1, byteorder='big')) - data.extend(lsb.to_bytes(length=1, byteorder='big')) - data.extend(msb.to_bytes(length=1, byteorder='big')) - print(path[path.rfind('/') + 1:path.rfind('.')], '=', data) + msb = (img_p[x, y][0] & 0xF8) | (img_p[x, y][1] >> 5) + lsb = ((img_p[x, y][1] & 0x1C) << 3) | (img_p[x, y][2] >> 3) + data.extend(x.to_bytes(length=1, byteorder="big")) + data.extend(y.to_bytes(length=1, byteorder="big")) + data.extend(lsb.to_bytes(length=1, byteorder="big")) + data.extend(msb.to_bytes(length=1, byteorder="big")) + print(path[path.rfind("/") + 1 : path.rfind(".")], "=", data) -- GitLab