06-10-2023, 08:27 PM
(This post was last modified: 06-10-2023, 08:29 PM by jacques_duflos.)
(06-10-2023, 07:23 AM)Ofnuts Wrote:glade to hear that IAs are not ready to replace us all just yet . I'm particularly impressed by the generation of a functional code that actually does what I asked in a human non-precise way.(06-10-2023, 12:38 AM)jacques_duflos Wrote: I made it with chatgpt, it's the first time I use it to program, and I'm impressed.
Me, somewhat less so... looks like the intern's code
About your observations :
- glob.glob : I didn't know about this module. Is it "just" to gain one code line ? I can replace the two lines
Code:
fichiers_dossier = os.listdir(dossier)
fichiers_xcf = [fichier for fichier in fichiers_dossier if fichier.endswith(".xcf")] - by the one line
Code:
fichiers_xcf = glob.glob(os.path.join(dossier, '*.xcf'))
- duplicating the image : chatgpt originally just omitted the layer argument of the save function. I googled the problem and found a thread on stack overflow that claimed that the duplicate technique was the easiest and not-so-resource-consuming solution. But I'll take your advice, thanks.
- skipping the directory creation : it was to avoid the error generation if the directory already exist. Chatgpt first proposed this line :
os.makedirs(dossier_rendu, exist_ok=True)but it didn't work, because of the python version used by gimp I think (?). would the codeCode:
if not os.path.exists(dossier_rendu):os.makedirs(dossier_rendu)
now my code looks like that
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# GIMP plugin to export all .xcf files of the folder in a /rendu/ folder
# (c) Jacques Duflos 2023
#
# History:
#
# v0.0: 2023-xx-xx: First published version
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import random
import sys, os
from glob import glob
from gimpfu import *
import gimpcolor
debug = False
def trace(s):
if debug:
print "**** "
print s
print "**** "
trace("export group test")
def export_groupe(image):
# Chemin complet vers le fichier actif
chemin_fichier = image.filename
# Obtenir le chemin du dossier contenant le fichier
dossier = os.path.dirname(chemin_fichier)
# Chemin du dossier de rendu
dossier_rendu = os.path.join(dossier, 'Rendu')
# Check if the render directory already exists
if not os.path.exists(dossier_rendu):os.makedirs(dossier_rendu)
# Obtenir la liste des fichiers xcf dans le même dossier
fichiers_xcf = glob(os.path.join(dossier, '*.xcf'))
trace(fichiers_xcf)
# Exporter les fichiers .xcf vers le dossier de rendu
for fichier in fichiers_xcf:
nom_fichier = os.path.basename(fichier)
trace(nom_fichier)
nom_fichier, _ = os.path.splitext(nom_fichier)
trace(nom_fichier)
# Charger le fichier .xcf dans GIMP
image_xcf = pdb.gimp_xcf_load(0, fichier, fichier)
# Exporter le fichier dans le dossier de rendu
trace(dossier_rendu)
trace(nom_fichier)
chemin_rendu = os.path.join(dossier_rendu, nom_fichier + ".png")
#trace(chemin_rendu)
pdb.gimp_file_save(image_xcf, pdb.gimp_layer_new_from_visible(image_xcf, image_xcf, "## saved"), chemin_rendu,chemin_rendu)
pdb.gimp_image_delete(image_xcf)
### Registrations
whoiam='\n'+os.path.abspath(sys.argv[0])
register(
'export-groupe',
'exporter tous les .xcf du dossier %s' % whoiam,
'exporter tous les .xcf du dossier',
'Jacques Duflos','Jacques Duflos','2023',
'exporter tous les .xcf du dossier...',
'*',
[
(PF_IMAGE, 'image', 'Image', None),
],
[],
export_groupe,
menu='<Image>/Layer'
)
main()