Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Python-Fu usage
#1
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!
Reply
#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
#3
Apologies, your reply went to my junk folder, now remedied. How terribly naive of me, dipping into something so easily exposed as too silly for words, but thanks for taking the time to break it down why "As Brian Kernighan wrote, "The most effective debugging tool is still careful thought, coupled with judiciously placed print statements."" Last quote within a quote today, but when you say, "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." how would you advise I take that on?

I'm of meager means, but don't mind paying for a script, if you can advise how that might work over this forum. Otherwise, you clearly know your way around with code, if this at all interests you. I'm simply needing to conform to a cloud-based ML solution, Roboflow, that only takes square images. My aspect being roughly 1.5, my thought was to tile a 7952x5304 raster into three files, (1) 5304x5304 flanked by (2) 2048x2048, with a 4x2048 gap between them. No expectations, just thought I'd ask. Thanks, in any event for the reality check.
Reply
#4
For this kind of thing, I would produce just two images, both 5304*5304, with a 2656px overlap in the center. Or if you want a centered image, three 5304*5304.

In any case since you have the coordinates of the squares, you can use something as simple as ImageMagick in a shell script or .BAT, see for instance here.
Reply
#5
(11-09-2024, 07:41 AM)Ofnuts Wrote: For this kind of thing, I would produce just two images, both 5304*5304, with a 2656px overlap in the center. Or if you want a centered image, three 5304*5304.

In any case since you have the coordinates of the squares, you can use something as simple as ImageMagick in a shell script or .BAT, see for instance here.

What a clever idea! I'll jump on ImageMagick and see if I can make sense of the instance. Again, many thanks.
Reply


Forum Jump: