unexpected problems creating text to png to ico in Python

46 Views Asked by At

I'm using Python 3.11.4 in Visual Studio Code on Windows 11. After executing my following Python-Code I receive 2 saved pictures: a png- and an ico-file (Hello.png & Hello.ico) as expected. But there is something wrong with the files. When I try to rename Hello.png (for example to World.png), than I receive an Windows-Errormessage with the text:

"Die Aktion kann nicht abgeschlossen werden, da die Datei in Windows-Explorer geöffnet ist. Schließen Sie die Datei und wiederholen Sie den Vorgang."

In English:

"The action cannot be completed because the file is open in Windows Explorer. Close the file and try again."

But the file is not opened. After restarting Windows, the file can be renamed.

import os
from PIL import Image, ImageDraw, ImageFont

def generate_png(text):
    # Bild erstellen
    image = Image.new("RGB", (256, 256), (255, 255, 255))
    draw = ImageDraw.Draw(image)
    
    # Schriftart und -größe festlegen
    font_size = 1
    font = ImageFont.truetype("arialbd.ttf", font_size)
    text_width = draw.textlength(text, font=font)
    bbox = draw.textbbox((0, 0), text, font=font)
    text_height = bbox[3] - bbox[1]
    while text_width < image.width and text_height < image.height:
        font_size += 1
        font = ImageFont.truetype("arialbd.ttf", font_size)
        text_width = draw.textlength(text, font=font)
        bbox = draw.textbbox((0, 0), text, font=font)
        text_height = bbox[3] - bbox[1]
    font_size -= 1
    font = ImageFont.truetype("arialbd.ttf", font_size)
    
    # Text auf das Bild zeichnen
    bbox = draw.textbbox((0, 0), text, font=font)
    x = (256 - bbox[2] - bbox[0]) // 2
    y = (256 - bbox[3] - bbox[1]) // 2
    draw.text((x, y), text, fill=(0, 0, 0), font=font)  # fill=(0, 0, 0) -> schwarzer Text

    # Ordner "generierte_Bilder" erstellen, falls er noch nicht existiert
    folder_path = "generated_pics"
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
    
    # Bild speichern
    image_path = os.path.join(folder_path, f"{text}.png")
    image.save(image_path)
    image.close()

    return image_path

def generate_icon(png_file):
    # PNG-Bild laden
    image = Image.open(png_file)
    
    # Icon-Datei erstellen
    icon = Image.new("RGBA", (256, 256))

    # Bildgrößen für das Icon definieren
    sizes = [(16, 16), (32, 32), (48, 48), (64, 64), (96, 96), (128, 128), (256, 256)]

    # PNG-Bild in verschiedene Größen konvertieren und im Icon platzieren
    for size in sizes:
        resized_image = image.resize(size, Image.Resampling.LANCZOS)
        offset = ((256 - size[0]) // 2, (256 - size[1]) // 2)
        icon.paste(resized_image, offset)

    image.close()

    # Icon als ICO-Datei speichern
    pre, ext = os.path.splitext(png_file)
    icon_file = png_file
    os.rename(icon_file, pre + ".ico")
    icon.save(icon_file, format="ICO")
    
def generate_pic():
    text = "Hello"
    png_path = generate_png(text)
    generate_icon(png_path)

if __name__ == "__main__":
    generate_pic()

When I comment the line

generate_icon(png_path)

out, than the renaming of the png works fine after executing the code. But the goal is to receive an ico-file.

What changes need to be made to the code so that a png and then an ico file are created from the text, but in such a way that after the png file has been created, it can be renamed?

0

There are 0 best solutions below