[python] Create Icons from Image File

Discuss and share scripts and script files...
Post Reply
WirlyWirly
Posts: 50
Joined: 21 Oct 2020 23:33

[python] Create Icons from Image File

Post by WirlyWirly »

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.

Code: Select all

Create Icons|run lax("C:\Path\to\WinPython Interpreter.exe" "C:\Path\to\script\CreateIcons.py" <selitems>)
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

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()
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.

highend
Posts: 11077
Joined: 06 Feb 2011 00:33

Re: [python] Create Icons from Image File

Post by highend »

Convert image(s) to icon.xys
(1.55 KiB) Downloaded 22 times
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

Post Reply