diff --git a/tools/companion-gen-icons.py b/tools/companion-gen-icons.py index e2a7e851a..b34144306 100755 --- a/tools/companion-gen-icons.py +++ b/tools/companion-gen-icons.py @@ -18,29 +18,32 @@ from PIL import Image # if False, then sips will be used instead of ImageMagick useMagick = True -logo_filename = "edgetx-logo.png" +LOGO_FILENAME = "edgetx-logo.png" PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) -images_dir = os.path.join(PROJECT_ROOT, "companion", "src", "images") -win_icons_dir = os.path.join(PROJECT_ROOT, "companion", "src", "images", "winicons") -linux_icons_dir = os.path.join(PROJECT_ROOT, "companion", "src", "images", "linuxicons") -mac_icons_dir = os.path.join(PROJECT_ROOT, "companion", "src", "images", "macicons") -start_dir = os.getcwd() - +TOOLS_DIR = os.path.dirname(__file__) +IMAGES_DIR = os.path.join(PROJECT_ROOT, "companion", "src", "images") +WIN_ICONS_DIR = os.path.join(PROJECT_ROOT, "companion", "src", "images", "winicons") +LINUX_ICONS_DIR = os.path.join(PROJECT_ROOT, "companion", "src", "images", "linuxicons") +MAC_ICONS_DIR = os.path.join(PROJECT_ROOT, "companion", "src", "images", "macicons") +START_DIR = os.getcwd() +ABS_LOGO_FILENAME = os.path.abspath(os.path.join(TOOLS_DIR,LOGO_FILENAME)) def cleanup(): - os.chdir(start_dir) - os.remove(logo_filename) + os.chdir(TOOLS_DIR) + os.remove(ABS_LOGO_FILENAME) # Remove temporary mac icons folder - if os.path.exists(mac_icons_dir): - shutil.rmtree(mac_icons_dir) + if os.path.exists(MAC_ICONS_DIR): + shutil.rmtree(MAC_ICONS_DIR) if os.path.exists("icns.tar.gz"): os.remove("icns.tar.gz") if os.path.exists("icnsify"): os.remove("icnsify") + + os.chdir(START_DIR) quit() @@ -54,46 +57,49 @@ def downloadFile(url: str, outFile: str): # Startup checks -if not os.path.exists(images_dir) or not os.path.exists(win_icons_dir) or not os.path.exists(linux_icons_dir): +if not os.path.exists(IMAGES_DIR) or not os.path.exists(WIN_ICONS_DIR) or not os.path.exists(LINUX_ICONS_DIR): print("Couldn't find a required directory!") - print("Images => " + images_dir) - print("Windows => " + win_icons_dir) - print("Linux => " + linux_icons_dir) + print("Images => " + IMAGES_DIR) + print("Windows => " + WIN_ICONS_DIR) + print("Linux => " + LINUX_ICONS_DIR) quit() # Download and save logo if we don't have it already -if not os.path.exists(logo_filename): +if not os.path.exists(LOGO_FILENAME): print("Downloading logo...") - downloadFile('https://raw.githubusercontent.com/EdgeTX/edgetx.github.io/master/images/edgetx-v2.png', logo_filename) + downloadFile('https://raw.githubusercontent.com/EdgeTX/edgetx.github.io/master/images/edgetx-v2.png', LOGO_FILENAME) print("Generate 96x96 icon.png... ") -os.chdir(images_dir) -if os.path.exists(images_dir + os.sep + 'icon.png'): - os.remove(images_dir + os.sep + 'icon.png') -img = Image.open(start_dir + os.sep + logo_filename).resize((96, 96)) -img.save(images_dir + os.sep + 'icon.png') +os.chdir(IMAGES_DIR) +icon_png = os.path.join(IMAGES_DIR,'icon.png') +if os.path.exists(icon_png): + os.remove(icon_png) +img = Image.open(ABS_LOGO_FILENAME).resize((96, 96)) +img.save(icon_png) print("Generate Linux Icons... ", end="") -os.chdir(linux_icons_dir) +os.chdir(LINUX_ICONS_DIR) + linux_resolutions = [16, 22, 24, 32, 48, 128, 256, 512] for size in linux_resolutions: - new_image_folder = os.path.join(linux_icons_dir, str(size) + 'x' + str(size)) - if os.path.exists(new_image_folder + os.sep + 'companion.png'): - os.remove(new_image_folder + os.sep + 'companion.png') + new_image_folder = os.path.join(LINUX_ICONS_DIR, str(size) + 'x' + str(size)) + companion_png = os.path.join(new_image_folder, 'companion.png') + if os.path.exists(companion_png): + os.remove(companion_png) elif not os.path.exists(new_image_folder): os.mkdir(new_image_folder) print(str(size) + " ", end="") - img = Image.open(start_dir + os.sep + logo_filename).resize((size, size)) - img.save(new_image_folder + os.sep + 'companion.png') + img = Image.open(ABS_LOGO_FILENAME).resize((size, size)) + img.save(companion_png) print("\nGenerate Windows Icons... ", end="") -os.chdir(win_icons_dir) +os.chdir(WIN_ICONS_DIR) windows_resolutions = [16, 20, 24, 30, 32, 36, 40, 48, 60, 64, 72, 80, 96, 128, 256] for size in windows_resolutions: - if os.path.exists(win_icons_dir + os.sep + 'edgetx_' + str(size) + '.ico'): - os.remove(win_icons_dir + os.sep + 'edgetx_' + str(size) + '.ico') - img = Image.open(start_dir + os.sep + logo_filename).resize((size, size)) + if os.path.exists('edgetx_' + str(size) + '.ico'): + os.remove('edgetx_' + str(size) + '.ico') + img = Image.open(ABS_LOGO_FILENAME).resize((size, size)) print(str(size) + " ", end="") img.save('edgetx_' + str(size) + '.ico') @@ -104,7 +110,7 @@ print("\nWindows All in One...") icon_sizes = [(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)] -img = Image.open(start_dir + os.sep + logo_filename) +img = Image.open(ABS_LOGO_FILENAME) if os.path.exists('edgetx.ico'): os.remove('edgetx.ico') img.save('edgetx.ico', sizes=icon_sizes) @@ -117,7 +123,7 @@ libicns @ https://icns.sourceforge.io\ print("Generate Mac icon set...") if platform.system() == "Linux": - icns_linux_gz = os.path.join(start_dir, "icns.tar.gz") + icns_linux_gz = os.path.join(TOOLS_DIR, "icns.tar.gz") print("Downloading icns (Go) for Linux amd64...") downloadFile('https://github.com/JackMordaunt/icns/releases/download/v2.1.2/icns_2.1.2_Linux_x86_64.tar.gz', icns_linux_gz) @@ -126,21 +132,21 @@ if platform.system() == "Linux": tar = tarfile.open(icns_linux_gz, "r:gz") for member in tar.getmembers(): if "icnsify" in member.name: - tar.extract(member, start_dir) + tar.extract(member, TOOLS_DIR) tar.close() - if not os.path.exists(os.path.join(start_dir, "icnsify")): + if not os.path.exists(os.path.join(TOOLS_DIR, "icnsify")): print("Something went wrong setting up icns! Some alternatives are") print(alternative_icns_tools) else: print("Creating MacOS icon set...") subprocess.call( [ - os.path.join(start_dir, "icnsify"), + os.path.join(TOOLS_DIR, "icnsify"), "-i", - os.path.join(start_dir, logo_filename), + ABS_LOGO_FILENAME, "-o", - os.path.join(images_dir, "iconmac.icns") + os.path.join(IMAGES_DIR, "iconmac.icns") ] ) @@ -201,10 +207,10 @@ elif platform.system == "Darwin": [ "magick", "convert", - logo_filename, + ABS_LOGO_FILENAME, "-resize", str(ip.width), - mac_icons_dir / ip.getIconName() + MAC_ICONS_DIR / ip.getIconName() ] ) else: @@ -214,9 +220,9 @@ elif platform.system == "Darwin": "-z", str(ip.width), str(ip.width), - logo_filename, + ABS_LOGO_FILENAME, "--out", - mac_icons_dir / ip.getIconName() + MAC_ICONS_DIR / ip.getIconName() ] ) @@ -226,9 +232,9 @@ elif platform.system == "Darwin": "iconutil", "-c", "icns", - mac_icons_dir, + MAC_ICONS_DIR, "-o", - images_dir / "iconmac.icns" + IMAGES_DIR / "iconmac.icns" ] )