I am trying to invert the icon which is .ico file depending on the windows theme . Depending on the them of windows, I will try to invert the tray icon. I haven't found any way to achieve this.
How to invert the icon using python 2.7 ?
def get_hicon(self, icon):
hicon = None
hinst = win32gui.GetModuleHandle(None)
if icon and os.path.isfile(icon):
icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
hicon = win32gui.LoadImage(hinst,
icon,
win32con.IMAGE_ICON,
ico_x,
ico_y,
icon_flags)
return hicon
I used this code to get the icon
You can utilize Pillow, invert the image and save it to a temporary file which will auto-delete after the context manager (with block) exits
# pip install pillow
from PIL import Image, ImageOps
from tempfile import NamedTemporaryFile
with NamedTemporaryFile() as file, Image.open("image.ico") as img:
old_mode = img.mode # loaded as RGBA!
rgb = img.convert("RGB")
inverted = ImageOps.invert(rgb)
inverted = inverted.convert(old_mode)
inverted.save(file, format="ICO", quality=100)
file.seek(0) # rewind so it can be read again
print(file.name)
# /tmp/tmpjo7pa_r6
# load/read icon here, probably self.get_hicon(file.name)
# file is automatically deleted here