Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Python-Fu usage
#2
(11-06-2024, 05:57 PM)BenjyvC Wrote: I queried Bing's Co-Pilot what tool and what code might serve to automate splitting an image into squares, this to conform to the format requirement of a cloud-based machine vision tool called Roboflow, which I'm intending to use to automate crack detection in concrete. (I've managed to learn enough about labelling in CVAT to download a json file containing the annotations in COCO 1.0, but am stuck attempting to train a model locally on my PC using detectron2.) To stay in motion while I await help on that, here's where I'm stuck using a Python-Fu script in Gimp.

When the code from Co-Pilot didn't produce anything, I asked it to simplify the operation to first confirm basic functionality using scripting in Gimp. It suggested creating a new file, but when that didn't produce anything, it further simplified to create a simple message, "Hello from Python-Fu". And when that only flashed the Console for a fraction of a sec, it included a prompt to pause for 10 seconds, which then at least that pause did work. That said, I'm not even seeing the pause when adding the create new image prompt:

from gimpfu import *

import time

def simple_message(image, drawable):
    gimp.message("Hello from Python-Fu!")
    time.sleep(10)  # Pause for 10 seconds

def create_image(image, drawable):
    save_dir = "D:/ALDOT_Model/Split_Test"  # Change this to your desired directory
    os.makedirs(save_dir, exist_ok=True)

    # Create a new image
    new_image = pdb.gimp_image_new(100, 100, RGB)
    new_layer = pdb.gimp_layer_new(new_image, 100, 100, RGB_IMAGE, "New Layer", 100, NORMAL_MODE)
    pdb.gimp_image_insert_layer(new_image, new_layer, None, -1)
    pdb.gimp_image_flatten(new_image)
    pdb.file_png_save(new_image, pdb.gimp_image_get_active_drawable(new_image), os.path.join(save_dir, "new_image.png"), "new_image.png", False, 9, True, True, True, True, True)
    gimp.message("Created and saved a new image as new_image.png")

register(
    "python_fu_create_image",
    "Create a new image and save it",
    "Create a new image and save it",
    "Your Name",
    "Your Name",
    "2024",
    "<Image>/Python-Fu/Create Image",
    "*",
    [],
    [],
    create_image)

main()

Many thanks for your insights!

If you want to split an image into squares, and save to file, there are already scripts for that, such as my own ofn-tiles.

But seriously, why make the matter more complicated with Gimp and its out-of-date Python (because currently you are stuck with Python 2.7, which is EOL since 2020). You can do the whole thing in modern and supported Python v3 using very common and popular libraries (typically Pillow). You should also investigate using OpenCV.

And be aware that AI doesn't know how old some code is. The code you post may work but is quite weird by today's standard, in fact it follows examples for Python-fu as written in the early noughties. A bit like if someone learned to speak English exclusively by reading Chaucer and Shakespeare. 

And to make you consider how unreliable AI-generated code is, consider that:
  • They define the simple_message() function, but there is no code to call it.
  • They create a create_image(image, drawable) function that takes two (unnecessary) arguments, but when they describe it to Gimp (the first [] in the register() call) it doesn't take any arguments. (the [] is an empty list, so no arguments).
And this is for very basic stuff. AI doesn't understand anything, they just fake it using advanced statistics. But this requires a huge training corpus, and there isn't that much Gimp Python script code published out there, especially where CoPilot pilfered its training data (GitHub).


If you insist in using Gimp for this, see this. But we keep in mind that if if can give indications, we are neither a debugging nor a scripting service.
Reply


Messages In This Thread
Basic Python-Fu usage - by BenjyvC - 11-06-2024, 05:57 PM
RE: Basic Python-Fu usage - by Ofnuts - 11-07-2024, 12:40 AM
RE: Basic Python-Fu usage - by BenjyvC - 11-08-2024, 09:54 PM
RE: Basic Python-Fu usage - by Ofnuts - 11-09-2024, 07:41 AM
RE: Basic Python-Fu usage - by BenjyvC - 11-11-2024, 04:13 PM

Forum Jump: