From eedf7545c8691497b624d04b60e90545eac7f42d Mon Sep 17 00:00:00 2001 From: Eberhard Beilharz Date: Tue, 11 Jul 2023 12:13:45 +0200 Subject: [PATCH 1/2] refactor(linux): Refactor some code --- .../keyman-config/keyman_config/convertico.py | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/linux/keyman-config/keyman_config/convertico.py b/linux/keyman-config/keyman_config/convertico.py index 1a836f9610..13c651887a 100755 --- a/linux/keyman-config/keyman_config/convertico.py +++ b/linux/keyman-config/keyman_config/convertico.py @@ -8,7 +8,6 @@ import sys import numpy as np from PIL import Image, ImageFile -Image.LOAD_TRUNCATED_IMAGES = True ImageFile.LOAD_TRUNCATED_IMAGES = True @@ -20,8 +19,7 @@ def changeblacktowhite(im): white_areas = (red == 0) & (blue == 0) & (green == 0) data[..., :-1][white_areas.T] = (255, 255, 255) # Transpose back needed - im2 = Image.fromarray(data) - return im2 + return Image.fromarray(data) def checkandsaveico(icofile): @@ -35,32 +33,34 @@ def checkandsaveico(icofile): icofile (str): path to ico file """ name, ext = os.path.splitext(icofile) - bmpfile = name + ".bmp" + bmpfile = f"{name}.bmp" if ext == '.ico': - im = Image.open(icofile) - im = im.convert('RGBA') - im2 = im - num, colour = max(im.getcolors(im.size[0] * im.size[1])) - logging.debug("checkandsaveico maxcolour: num {0}: colour {1}".format(num, colour)) - if num > 160 and colour == (0, 0, 0, 0): - logging.info("checkandsaveico:" + icofile + " mostly black so changing black to white") - im2 = changeblacktowhite(im) - im2.save(bmpfile) - + _convert_ico_to_bmp(icofile, bmpfile) try: - im3 = Image.open(bmpfile) - im4 = im3.resize([64, 64], Image.ANTIALIAS) - # Using .bmp.png file extension so it won't conflict if the package already contains .png - im4.save(bmpfile + '.png', 'png') + with Image.open(bmpfile) as im3: + with im3.resize((64, 64), Image.ANTIALIAS) as im4: + # Using .bmp.png file extension so it won't conflict if the package already contains .png + im4.save(f'{bmpfile}.png', 'png') except (IOError, OSError): logging.error("Cannot convert %s to png", icofile) - pass finally: # Clean up intermediary .bmp file if it was generated if ext == '.ico': os.remove(bmpfile) +def _convert_ico_to_bmp(icofile, bmpfile): + with Image.open(icofile) as im: + with im.convert('RGBA') as im2: + num, colour = max(im.getcolors(im2.size[0] * im2.size[1])) + logging.debug(f"checkandsaveico maxcolour: num {num}: colour {colour}") + if num > 160 and colour == (0, 0, 0, 0): + logging.info(f"checkandsaveico:{icofile} mostly black so changing black to white") + im2.close() + im2 = changeblacktowhite(im) + im2.save(bmpfile) + + def extractico(kmxfile): """ Extract icon file from compiled kmx keyboard @@ -95,9 +95,9 @@ def extractico(kmxfile): # Read first two bytes to determine if icon is .bmp or .ico if bitmap.startswith(b'BM'): - imagefilename = imagefilename + ".bmp" + imagefilename = f"{imagefilename}.bmp" else: - imagefilename = imagefilename + ".ico" + imagefilename = f"{imagefilename}.ico" try: with open(imagefilename, mode='wb') as imagefile: From 3cc9f09d820c01e3bdf728f98dca3fbad881063c Mon Sep 17 00:00:00 2001 From: Eberhard Beilharz Date: Tue, 11 Jul 2023 12:14:26 +0200 Subject: [PATCH 2/2] fix(linux): Replace deprecated `Image.ANTIALIAS` Fixes #9244. --- linux/keyman-config/keyman_config/convertico.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux/keyman-config/keyman_config/convertico.py b/linux/keyman-config/keyman_config/convertico.py index 13c651887a..fa6f7b39ab 100755 --- a/linux/keyman-config/keyman_config/convertico.py +++ b/linux/keyman-config/keyman_config/convertico.py @@ -38,7 +38,7 @@ def checkandsaveico(icofile): _convert_ico_to_bmp(icofile, bmpfile) try: with Image.open(bmpfile) as im3: - with im3.resize((64, 64), Image.ANTIALIAS) as im4: + with im3.resize((64, 64), Image.LANCZOS) as im4: # Using .bmp.png file extension so it won't conflict if the package already contains .png im4.save(f'{bmpfile}.png', 'png') except (IOError, OSError):