[Python] Create Icons from Image File
Posted: 21 Nov 2021 23:48
Just thought I'd post this little python script that I use whenever I need to create icons from a .png or .svg image file. Previously I used the GUI in IcoFX, but the task was repetitive and the GUI slow. I got tired of the process, especially considering how many custom icons I use within xyPlorer... which is a lot because I enjoy customizing the software I use...
You can run this script from the cmd line and pass files as arguments, but what I do is select image file(s) in xyPlorer, then call this script and pass the selected items as arguments. It spits out a single .ico file for each image I passed, so making a bunch of icons in one go is painless.
It'd probably be more practical for us if it was written in .xys, but I'm much more comfortable with python so that's what I went with. If anyone feels the need to re-write this in .xys then have at it. You can copy the ImageMagick flags straight from this script.
This script uses ImageMagick, so you'll need to download it and point the script to convert.exe. I recommend downloading the portable version of ImageMagick, just search for it on their downloads page: ImageMagick
If you're looking for a portable release of python, I definitely recommend checking out WinPython. Having access to Python on any system is a game-changer, especially when you combine it with the already impressive options in xyPlorer.
You can run this script from the cmd line and pass files as arguments, but what I do is select image file(s) in xyPlorer, then call this script and pass the selected items as arguments. It spits out a single .ico file for each image I passed, so making a bunch of icons in one go is painless.
Code: Select all
Create Icons|run lax("C:\Path\to\WinPython Interpreter.exe" "C:\Path\to\script\CreateIcons.py" <selitems>)
This script uses ImageMagick, so you'll need to download it and point the script to convert.exe. I recommend downloading the portable version of ImageMagick, just search for it on their downloads page: ImageMagick
Code: Select all
from pathlib import Path
import subprocess
import sys
# Full Path to "ImageMagick/convert.exe"
# example: Path('C:/Program Files/ImageMagick/convert.exe')
imagemagick = Path(sys.argv[0]).resolve().parents[2] / 'Command/ImageMagick/convert.exe'
for argument in sys.argv[1:]:
file = Path(argument).resolve()
output_filename = file.parent / f'{file.stem}.ico'
if file.suffix == '.svg':
command = [
str(imagemagick),
'-density',
'600',
'-background',
'none',
str(file),
'-resize',
'256x256',
'-define',
'icon:auto-resize=256,128,48,32,24,16',
str(output_filename)
]
elif file.suffix == '.png':
command = [
str(imagemagick),
'-background',
'none',
str(file),
'-define',
'icon:auto-resize=256,128,48,32,24,16',
str(output_filename)
]
else:
print('Not a supported filetype, exiting...')
sys.exit()
subprocess.Popen(command)
sys.exit()