22 abril, 2024

Exercise in Python: creating icons with PIL |

A few weeks ago, we were looking at how to create an executable file, from a «.py» using the library «pyinstaller». In this process we talked about the possibility of personifying the icon of our future «.exe» accessing some of the pages created for this purpose, or through the library and we were creating icons with PIL.

Well, this time, we are going to do it with you. We are going to create a simple program that allows us to create an image file, an icon for our program (file «.ico»), exercise which will help us put into practice some knowledge that we have recently acquired.

Let’s get started: creating icons with PIL

As usual, we will start by importing the necessary libraries and resources so that our program can work. Of which only «PIL» we will have to install it (if we have not already done so) on our device (using the command «pip»):

As you can see, we have imported the module «Image» from the bookstore «PIL». We have done the same with «you» (which we will use to set the path to the directory we want our program to run in) and «subprocess».

Since our program is going to create the file «.ico» from an image file that we have previously stored in a folder on our system. The first thing we will have to indicate to our program is the directory in which the image file is located from which we are going to create our icon (which will also be where the new generated file will be located).
To do this, we will create a variable («new route»), through which we will indicate the path to said directory. However, since there is a possibility that an error may be made when entering such information (an inappropriate string or a non-existent route), to prevent the program from failing in such cases, we will indicate that it displays the message «INVALID ROUTE» and since we have included this block in a «while»the program will ask us again for the path to the directory.

On the other hand, if the information entered corresponds to an existing directory («if os.path.isdir (new_path):») the program will change the directory in which it is executed with the method «.chdir()» which will be the one indicated with the input for «new route» (“os.chdir(new_path)”). In turn, since this block is being executed in a loop («while True:») we will use (in the latter case) «break» to finish it.

Once our program has the information regarding the directory in which it is going to be executed, it will ask us to enter the name of the image file that we want to convert to the format «.ico»said input will be stored in a variable named «im»:

Once we indicate the name of the file to use, our program will check if said file is located within the current directory («if im in os.listdir():»). If so, a variable will be created («yam») in which the file name without the extension («name=diiv[0]» where «divi» is the variable that contains the list with the name and extension of the original file, using the dot «.» as a separator). On the other hand, in the event that there is no file in the directory, with the specified name, we will show the message «File not found, im» (what we will do with a «else»).

As seen in the image above, obtaining the file name without the extension, we will use a statement «try:» so that our program «try»first of all, open the file indicated in the variable «im» using the method «Image.open()» («image=Image.open(im)») and secondly, save the file as a “.ico” file. For which we will use the «.save()» method to which we will pass as an argument the name of the original file (without the extension) plus the new extension «.ico» («image.save(name+».ico»)) after which, the program will display the message on the screen «’.ICO’ FILE SUCCESSFULLY CREATED». For its part, this double operation cannot be carried out (either because the indicated file cannot be opened, or because it is not suitable to be converted into «.ico»), as provided in the exception («except:»), displaying the message «The operation could not be completed.»

Finally, once the file is created «.ico»our program will ask us if we want to continue with its execution (to limit the possible responses of the user, with «n» for not and «s» To affirm, we will use the function «ns» previously defined) said user response will be stored in a new variable «conti». So if the answer is «n» will end the execution of the program, and if it is «s», the execution of the loop will simply continue, after clearing the console with «subprocess» in the case of windows (for linux we would use «os.system(«clear»)») in which the conversion operation is included:

You can see the complete code of this program in the following link to github. And well, this is how we are creating icons with PIL. Did you know how?

Self-taught programmer and developer. Posts weekly on the blog “The Botched Programmer” (wordpress) and I also collaborate on the pages “Commented Code” and “MathPy algorithms” From Facebook.

If you like Be part of the community, sharing relevant development information Web, Mobile, Big Data or Blockchain you can write to us at [email protected] .

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *