3

I'll make a rectangle selection in image. I have to read the left, right, up and down dimensions of this selected region in full image. The following image is an example of this.

enter image description here

Edit:

Can we print the following variables in the window or debug screen?

Units in mm.

L = X of Selection Bounding Box
U = Y of Selection Bounding Box
D = H of Image - (H of Selection Bounding Box + U)
R = W of Image - (W of Selection Bounding Box + L)

2 Answers 2

3

This example image is 200px x 200px. If you don't know it, you can find the size by clicking Image > Canvas Size

enter image description here

The Crop Tool options show the X and Y position of the top left corner of the crop, and the X and Y size of the crop. So it's simple arithmetic to work out the distances.

Left = 33px

Up = 49px

Right = 200-33-129 = 38px

Down = 200-49-96 = 55px

2
  • You're right. But can Gimp do this automatically? Can a script be embedded in Gimp? Because I have a lot of pictures to do. Or is there a program that can do this?
    – Özgür
    Commented May 28, 2019 at 0:35
  • GIMP can run Script-fu and Python-fu scripts - but don't ask me how to write one. I've no idea.
    – Billy Kerr
    Commented May 28, 2019 at 0:37
3

A very basic python script that does it:

#!/usr/bin/env python
from gimpfu import *

def selectionMargins(image):
    selection,x1,y1,x2,y2=pdb.gimp_selection_bounds(image)
    if not selection:
        pdb.gimp_message("No selection")
    else:
        gimp.message('L=%d\nU=%d\nR=%d\nD=%d' % (x1,y1,image.width-x2,image.height-y2))

### Registration
desc='Show selection margins'
register(
    'selection-margins',desc,desc,'','','2019',desc,'*',
    [(PF_IMAGE, "image", "Input image", None),],[],
    selectionMargins,menu="<Image>/Select"
)

main()
  • Copy the script to a text file with .py extension (note: python is sensitive to the indentation of the code)
  • See Edit>Preferences>Folder>Plugins for the folder whre the resulting file should be
  • On OSX and Linux, make it executable
  • The menu entry is at the bottom of the Select menu in an image window.
  • Output is in a dialog or in the error console window if it up

enter image description here

It wouldn't be difficult to improve the script to append to a CSV file a line with the image file name/path and the values.

2
  • Of course, very basic for you. I don't know this matter. I'm away from my computer right now. I'll try as soon as I get to the computer.
    – Özgür
    Commented May 30, 2019 at 11:54
  • 5 lines of code, once you have removed the boilerplate :)
    – xenoid
    Commented May 30, 2019 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.