Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Out of Sequence?
#1
Python 
I wrote a python script that removes a background colour from an image, orientates the remainder, resizes the image and then overwrites the existing image file. All of the functions work as expected...but they don't happen in the order expected. The final two lines (below) happen in reverse order.


Quote:    pdb.gimp_image_resize(imageToBeResized, 900, 1500, widthoffset, heightoffset)
    pdb.gimp_file_save(imageToBeResized, drawable, imageFileName, imageFileName)


 So the image will overwrite before being resized, and I have no idea why this is the case.

  1. Running the second line twice makes no difference - it will continue to overwrite without resizing.
  2. Duplicating both lines (1,2,1,2) actually shifts the affected area so that it saves in 900*1500 but to the image focus' upper left.
Anyone have any idea why this would be?

Full code below for reference:

Quote:def postmatch_resize(image):
    imageFileName = pdb.gimp_image_get_filename(image)
    imageToBeResized = gimp.image_list()[0]
    drawable = pdb.gimp_image_active_drawable(imageToBeResized)
    pdb.gimp_image_crop(imageToBeResized, 1900, 1500, 0, 0)
    colorToDelete1 = gimpcolor.RGB(9, 246, 57)
    colorToDelete2 = gimpcolor.RGB(6, 185, 42)
    colorToDelete3 = gimpcolor.RGB(7, 216, 50)
    colorToDelete4 = gimpcolor.RGB(4, 123, 28)
    colorToDelete5 = gimpcolor.RGB(5, 154, 35)
    colorToDelete6 = gimpcolor.RGB(3, 93, 21)
    colorToDelete7 = gimpcolor.RGB(2, 62, 14)
    pdb.gimp_image_select_color(imageToBeResized, 0, drawable, colorToDelete1)
    if pdb.gimp_selection_is_empty(imageToBeResized) == False:
        pdb.gimp_edit_clear(drawable)
    pdb.gimp_image_select_color(imageToBeResized, 0, drawable, colorToDelete2)
    if pdb.gimp_selection_is_empty(imageToBeResized) == False:
        pdb.gimp_edit_clear(drawable)
    pdb.gimp_image_select_color(imageToBeResized, 0, drawable, colorToDelete3)
    if pdb.gimp_selection_is_empty(imageToBeResized) == False:
        pdb.gimp_edit_clear(drawable)
    pdb.gimp_image_select_color(imageToBeResized, 0, drawable, colorToDelete4)
    if pdb.gimp_selection_is_empty(imageToBeResized) == False:
        pdb.gimp_edit_clear(drawable)
    pdb.gimp_image_select_color(imageToBeResized, 0, drawable, colorToDelete5)
    if pdb.gimp_selection_is_empty(imageToBeResized) == False:
        pdb.gimp_edit_clear(drawable)
    pdb.gimp_image_select_color(imageToBeResized, 0, drawable, colorToDelete6)
    if pdb.gimp_selection_is_empty(imageToBeResized) == False:
        pdb.gimp_edit_clear(drawable)
    pdb.gimp_image_select_color(imageToBeResized, 0, drawable, colorToDelete7)
    if pdb.gimp_selection_is_empty(imageToBeResized) == False:
        pdb.gimp_edit_clear(drawable)
    pdb.plug_in_autocrop(imageToBeResized, drawable)
    preheight = pdb.gimp_image_height(imageToBeResized)
    prewidth = pdb.gimp_image_width(imageToBeResized)
    heightoffset = 1500 - preheight
    widthoffset = (900 - prewidth)/2
    pdb.gimp_image_resize(imageToBeResized, 900, 1500, widthoffset, heightoffset)
    pdb.gimp_file_save(imageToBeResized, drawable, imageFileName, imageFileName)

I doubt very much that the above cobbled together solution is optimal so I'll take pointers but I'm also curious as to why it doesn't work sequentially? I thought that was the whole point.
Reply
#2
  • pdb.gimp_image_resize() is the API equivalent of Image>Canvas size, but it only applies to the canvas, so the layers are not affected  (as if you selected Resize layers: None).
  • pdb.gimp_file_save() saves the drawable, not the image. Since the previous call did not change the layer size, the saved image has the original size.
  • The easiest way to fix this is to call pdb.gimp_layer_new_from_visible() (this creates a layer which is what you see in the canvas...) and save that layer (you don't even need to explicitly add it to the image IIRC).
  • A more proper way is to crop the layer before you save it.
Reply
#3
(09-22-2023, 08:32 PM)Ofnuts Wrote:
  • pdb.gimp_image_resize() is the API equivalent of Image>Canvas size, but it only applies to the canvas, so the layers are not affected  (as if you selected Resize layers: None).
  • pdb.gimp_file_save() saves the drawable, not the image. Since the previous call did not change the layer size, the saved image has the original size.
  • The easiest way to fix this is to call pdb.gimp_layer_new_from_visible() (this creates a layer which is what you see in the canvas...) and save that layer (you don't even need to explicitly add it to the image IIRC).
  • A more proper way is to crop the layer before you save it.

Aha! That makes it really clear. Thanks for your help!
Reply


Forum Jump: