[Python] Create Icons from Image File

Discuss and share scripts and script files...
Post Reply
WirlyWirly
Posts: 195
Joined: 21 Oct 2020 23:33
Location: Through the Looking-Glass

[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.
Last edited by WirlyWirly on 17 May 2022 15:40, edited 1 time in total.

highend
Posts: 13274
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 245 times
One of my scripts helped you out? Please donate via Paypal

yogi
Posts: 100
Joined: 18 Feb 2010 09:05

Re: [python] Create Icons from Image File

Post by yogi »

hello. sorry to redo this topic but I do not understand the use. I downloaded imagemagik, I changed the convert.exe links, but I don't understand the 2nd line with another address for convert.exe file??
Suddenly nothing is happening to me, can you help me??? I talk of file .xys

thank you. Yogi

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

Re: [python] Create Icons from Image File

Post by highend »

It's a comment, it's ignored by XY anyway
One of my scripts helped you out? Please donate via Paypal

yogi
Posts: 100
Joined: 18 Feb 2010 09:05

Re: [python] Create Icons from Image File

Post by yogi »

ok but why is this not working? how should i use it? make a user button??? I can't get it to set up. it would help me so much, because currently I use toycon; it converts ne .ico images by drag and drop and it's really great, except that you have to open it to use it. if I could integrate it into a xys maybe??

yogi
Posts: 100
Joined: 18 Feb 2010 09:05

Re: [python] Create Icons from Image File

Post by yogi »

Its OK !!!

yogi
Posts: 100
Joined: 18 Feb 2010 09:05

Re: [python] Create Icons from Image File

Post by yogi »

one last question for this awesome script, a recursive mode is possible?????

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

Re: [python] Create Icons from Image File

Post by highend »

If you rewrite it to make this possible... It's possible^^
One of my scripts helped you out? Please donate via Paypal

yogi
Posts: 100
Joined: 18 Feb 2010 09:05

Re: [Python] Create Icons from Image File

Post by yogi »

Thanks for the reply, I was expecting that.
Apart from a tiny handful of people, in fact no one here really knows how to answer for scripts; and I also feel annoyance that I also understand! if I knew how to do it, I wouldn't post, and the problem is that even when I reread the scripts I take, I can't understand the approach of the xys language. So yes I'm sorry to post these kind of questions and I'm sorry that you're the only one to bother to answer! and frankly I would like to do without you if I were able!!

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

Re: [Python] Create Icons from Image File

Post by highend »

My position is: You're a long time member. Contribute!

The approach of the "language" is the same as in all other languages? quicksearch() / foreach...
One of my scripts helped you out? Please donate via Paypal

WirlyWirly
Posts: 195
Joined: 21 Oct 2020 23:33
Location: Through the Looking-Glass

Re: [Python] Create Icons from Image File

Post by WirlyWirly »

You don't really need a recursive script. Such a script would find all the .png and .svg files in a folder and then convert them to .ico.

You could do the same thing with a quick search *.svg | *.png. Once you have the results, highlight all of them and run the script. It uses a loop so it'll run as many images as you want. They'll be outputted to whatever folder the source file was in.

Do another search for .ico files, maybe sort-by date-created, and move them somewhere else. Done.

yogi
Posts: 100
Joined: 18 Feb 2010 09:05

Re: [Python] Create Icons from Image File

Post by yogi »

Hi! yes there is this possibility but on this account as much as I use my "toyicon" conversion software, because a drag and drop from the folder and sub-folders examines and converts all image files into icons. I would have preferred without even looking if the folder contained images click on the script and let imagmagick work!

Post Reply