Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
translating every text layer
#1
Hi there,
I made this script that translates every text layer of the layer group selected, or the text layer selected.
As you can see  at lines 50 to 54, I had to skip the text layers witches text are None because it generated an error and interrupted the script. But I don't understand why some of my text layers return the None value when I ask them their text. All those text layers have a text. Any clue anyone ?

Code:
               # lines 50 to 54
                # Vérifier si le texte n'est pas de type NoneType
                if text is not None:
                    # Traduire le texte en utilisant la bibliothèque translate
                    translated_text = translator.translate(text)
                else:
                    translated_text = "ERREUR TEXTE NON TRADUIT\n{}".format(text)


Another question I have is about the libraries I had to include. I added them directly next to the .py that needed them and it works. But I guess it is not the elegant way of doing so. If I do another script needing the same libraries, I would have to include them once more. Is there a way to include libraries in the gimp specific python installation ?
the dependencies are as followed :
traducion.py (my script) needs translate
translate.py needs providers
providers/mymemory_translated.py needs requests

the main script is :

Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gimpfu import *
from translate import Translator

def translate_text_layers(image, drawable):
    image.undo_group_start()
    
    translator = Translator(to_lang="fr", from_lang="es")
    print("------------start--------------")
    pdb.gimp_message_set_handler(MESSAGE_BOX)
    
    # Recuperation du groupe de calques selectionne
    group = pdb.gimp_image_get_active_layer(image)
    
    # Récupération du groupe de calques "text-fr" s'il existe
    group_text = None
    for layer in image.layers:
        if pdb.gimp_item_is_group(layer) and pdb.gimp_item_get_name(layer) == "text-fr":
            group_text = layer
            break

    if group_text is None:
        # Créer un groupe de calques nommé "text-fr" s'il n'existe pas
        group_text = pdb.gimp_layer_group_new(image)
        pdb.gimp_item_set_name(group_text, "text-fr")
        pdb.gimp_image_insert_layer(image, group_text, None, 0)

    if pdb.gimp_item_is_group(group):
        print("----------layer is group-------------")
        # Creer un groupe de calques nommé "text-fr"
        group_text = pdb.gimp_layer_group_new(image)
        pdb.gimp_item_set_name(group_text, "text-fr")
        pdb.gimp_image_insert_layer(image, group_text, None, 0)
        # Parcours de tous les calques du groupe
        for layer in group.layers:
            # Vérification si le calque est de type texte
            if pdb.gimp_item_is_text_layer(layer):
                # Dupliquer le calque
                duplicate = pdb.gimp_layer_copy(layer, True)
                
                # Ajouter le calque duplique au groupe "text-fr"
                pdb.gimp_image_insert_layer(image, duplicate, group_text, 0)
                
                # Récupérer le contenu texte du calque dupliqué
                text = pdb.gimp_text_layer_get_text(duplicate)
                
                # Vérifier si le texte n'est pas de type NoneType
                if text is not None:
                    # Traduire le texte en utilisant la bibliothèque translate
                    translated_text = translator.translate(text)
                else:
                    translated_text = "ERREUR TEXTE NON TRADUIT\n{}".format(text)

                
                # Mettre à jour le texte du calque dupliqué avec la traduction
                pdb.gimp_text_layer_set_text(duplicate, translated_text)

        # Actualisation de l'affichage de l'image
        pdb.gimp_displays_flush()
    elif pdb.gimp_item_is_text_layer(group):
        print("----------layer is text-------------")
        # Si le calque sélectionné est un calque de texte
        # Dupliquer le calque
        duplicate = pdb.gimp_layer_copy(group, True)
        
        # Ajouter le calque duplique au groupe "text-fr"
        pdb.gimp_image_insert_layer(image, duplicate, group_text, 0)
        
        # Récupérer le contenu texte du calque dupliqué
        text = pdb.gimp_text_layer_get_text(duplicate)
        
        # Traduire le texte en utilisant la bibliothèque translate
        translated_text = translator.translate(text)
        
        # Mettre à jour le texte du calque dupliqué avec la traduction
        pdb.gimp_text_layer_set_text(duplicate, translated_text)
    else:
        print("--------Veuillez sélectionner un groupe de calques ou un calque de texte.----")
        gimp.message("Veuillez sélectionner un groupe de calques ou un calque de texte.")
    
    pdb.gimp_message("fini")
    # restore stuff
    image.undo_group_end()

register(
    "python-fu-translate-text-layers",
    "Translate text layers from French to English",
    "Translate text layers from French to English",
    "Your Name",
    "Your Name",
    "2023",
    "<Image>/Filters/Language/Translate Text Layers",
    "*",
    [],
    [],
    translate_text_layers)

main()


Attached Files
.zip   traduction.zip (Size: 470.93 KB / Downloads: 627)
Reply


Messages In This Thread
translating every text layer - by jacques_duflos - 07-12-2023, 11:42 PM
RE: translating every text layer - by Ofnuts - 07-13-2023, 06:51 AM
RE: translating every text layer - by Ofnuts - 07-19-2023, 06:37 AM
RE: translating every text layer - by Ofnuts - 07-21-2023, 07:31 AM
RE: translating every text layer - by Ofnuts - 07-25-2023, 06:23 AM
RE: translating every text layer - by ttt - 10-18-2024, 09:55 PM

Forum Jump: