Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Crop Scale Desaturate script - ChatGPT seems to make a mess
#1
Question 
Hi,

To not unnecessarily bother the people on this forum, I tried to have ChatGPT put a script togther for me. But ChatGPT cannot manage, however hard I try to prompt it clearly.

Here's the question:

Write me a complete python script for the Gimp that does the following for me. I will start by manually activating the crop tool and marking an area for cropping. after I mark the area I want to start the script and then it does the following for me:
- Run a crop on the crop selection I already made;
- Adjust the longest side of the image to 2000 pixels, (and the other side to ratio)
- Reduce the saturation to 0


The best it came up with after about 10 attempts, was this:
Code:
def crop_scale_desaturate(image, drawable):
    # Crop the image to the current selection
    pdb.gimp_edit_clear(drawable)
    pdb.gimp_selection_none(image)
    pdb.gimp_selection_invert(image)
    pdb.gimp_edit_cut(drawable)
    pdb.gimp_selection_none(image)

    # Scale the image to a maximum size of 2000 pixels on the longest side
    max_size = 2000
    width, height = drawable.width, drawable.height
    if width > height:
        scale_width = max_size
        scale_height = (height * max_size) // width
    else:
        scale_width = (width * max_size) // height
        scale_height = max_size
    pdb.gimp_image_scale(image, scale_width, scale_height)

    # Desaturate the image
    pdb.gimp_desaturate_full(cropped_image, DESATURATE_LIGHTNESS)

    # Show the results
    gimp.displays_flush()

This is sort of the best of the 10 worlds. Is it close? It is not working, that is for sure. The closest that I got to result, was the whole image resized and a black rectangle in the middle...

I have no idea why it needs to clear / select none / invert / cut / select none. I would just want to "hit enter", since I already made the selection ready for cropping. But I guess that does not exsist.
When I ask it why we need all those, GPT says that it is not needed, but then it puts it right back in every time. I guess this is called hallucinating.

I also tried with rectangular selection (instead of using the croptool), and crop from there, But I can't get that to work either.


Code:
    # get current selection
    selection = pdb.gimp_selection_save(image)

    # Crop image
    pdb.gimp_edit_copy(drawable)
    cropped_image = pdb.gimp_edit_paste_as_new()
    pdb.gimp_floating_sel_to_layer(cropped_image)
    pdb.gimp_selection_none(image)

    # Crop prev selected area
    pdb.gimp_image_select_item(image, CHANNEL_OP_REPLACE, selection)
    pdb.gimp_edit_clear(cropped_image)
    pdb.gimp_selection_none(image)

How would you go about this?

Resizing, desaturation and registration are working.

Thanks, Thomas.
Reply
#2
(04-26-2023, 09:24 PM)Some inprovements to my post in red .tkemmere Wrote: Hi,

To not unnecessarily bother the people on this forum, I tried to have ChatGPT put a script togther for me. But ChatGPT cannot manage, however hard I try to prompt it clearly.

Here's the question:

Write me a complete python script for the Gimp that does the following for me. I will start by manually activating the crop tool and marking an area for cropping. After I mark the area I want to start the script and then it should do the following for me:
- Run a crop on the crop selection I made beforehand;
- Adjust the longest side of the image to 2000 pixels, (and the other side to ratio)
- Reduce the saturation to 0


The best it came up with after about 10 attempts, was probably this:
Code:
def crop_scale_desaturate(image, drawable):
    # Crop the image to the current selection
    pdb.gimp_edit_clear(drawable)
    pdb.gimp_selection_none(image)
    pdb.gimp_selection_invert(image)
    pdb.gimp_edit_cut(drawable)
    pdb.gimp_selection_none(image)

    # Scale the image to a maximum size of 2000 pixels on the longest side
    max_size = 2000
    width, height = drawable.width, drawable.height
    if width > height:
        scale_width = max_size
        scale_height = (height * max_size) // width
    else:
        scale_width = (width * max_size) // height
        scale_height = max_size
    pdb.gimp_image_scale(image, scale_width, scale_height)

    # Desaturate the image
    pdb.gimp_desaturate_full(cropped_image, DESATURATE_LIGHTNESS)

    # Show the results
    gimp.displays_flush()

This is sort of the best of the 10 worlds. Is it close? It is not working, that's for sure. The closest that I got to result, was the whole image resized and a black rectangle in the middle...

I have no idea why it needs to clear / select none / invert / cut / select none. I would just want to "hit enter", since I already made the selection ready for cropping. But I guess that does not exist.
When I ask it why we need all those, GPT says that it is not needed, but then it puts it right back in every time. I guess this is called hallucinating.

I also tried with rectangular selection (instead of using the crop tool), and crop from there, But I can't get that to work either.

Code:
    # get current selection
    selection = pdb.gimp_selection_save(image)

    # Crop image
    pdb.gimp_edit_copy(drawable)
    cropped_image = pdb.gimp_edit_paste_as_new()
    pdb.gimp_floating_sel_to_layer(cropped_image)
    pdb.gimp_selection_none(image)

    # Crop prev selected area
    pdb.gimp_image_select_item(image, CHANNEL_OP_REPLACE, selection)
    pdb.gimp_edit_clear(cropped_image)
    pdb.gimp_selection_none(image)

At one stage it also came up with a separate window that was supposed to have the result, which was actually nice. But is was black.


Code:
   # Deselect
   pdb.gimp_selection_none(image)
   
   # copy
   floating_sel = pdb.gimp_edit_copy(drawable)
   new_image = pdb.gimp_edit_paste_as_new(floating_sel)
   
   # select
   pdb.gimp_rect_select(new_image, 0, 0, new_image.width, new_image.height, 0, False, 0)
   pdb.gimp_edit_cut(new_image.layers[0])
   
   # Crop
   pdb.gimp_image_crop(new_image, new_image.width, new_image.height, 0, 0)
   
   # Schale
   if new_image.width > new_image.height:
       pdb.gimp_image_scale(new_image, 2000, new_image.height * 2000 / new_image.width)
   else:
       pdb.gimp_image_scale(new_image, new_image.width * 2000 / new_image.height, 2000)
   
   # Desaturate
   cropped_image = new_image.layers[0]
   pdb.gimp_desaturate_full(cropped_image, DESATURATE_LIGHTNESS)
   
   # Show
   pdb.gimp_display_new(new_image)

How would you go about this? Did ChatGPT actually make any sense anywhere?
I guess yes, cause resizing, desaturation and registration are working, that is not where the problem is.

Thanks if any body is available to help me out!
Regards
, Thomas.
Reply
#3
Here's how I'd do part of your requirement:
Code:
selection, x1, y1, x2, y2 = pdb.gimp_selection_bounds(Image)  
pdb.gimp_image_crop(Image, (x2-x1), (y2 - y1), x1, y1)
pdb.gimp_drawable_desaturate(Drawable, 2)

You have to have made a selection using the Rectangle select tool, not using the crop tool.
Reply


Forum Jump: