Page 1 of 1

[Python] Create Icons from Image File

Posted: 21 Nov 2021 23:48
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.

Re: [python] Create Icons from Image File

Posted: 22 Nov 2021 00:17
by highend
Convert image(s) to icon.xys
(1.55 KiB) Downloaded 249 times

Re: [python] Create Icons from Image File

Posted: 04 May 2022 13:07
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

Re: [python] Create Icons from Image File

Posted: 04 May 2022 13:14
by highend
It's a comment, it's ignored by XY anyway

Re: [python] Create Icons from Image File

Posted: 04 May 2022 21:05
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??

Re: [python] Create Icons from Image File

Posted: 06 May 2022 12:12
by yogi
Its OK !!!

Re: [python] Create Icons from Image File

Posted: 17 May 2022 12:44
by yogi
one last question for this awesome script, a recursive mode is possible?????

Re: [python] Create Icons from Image File

Posted: 17 May 2022 13:26
by highend
If you rewrite it to make this possible... It's possible^^

Re: [Python] Create Icons from Image File

Posted: 17 May 2022 21:09
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!!

Re: [Python] Create Icons from Image File

Posted: 17 May 2022 21:22
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...

Re: [Python] Create Icons from Image File

Posted: 18 May 2022 00:56
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.

Re: [Python] Create Icons from Image File

Posted: 18 May 2022 12:52
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!