Move a layer to specific X,Y position in Gimp

How do I move a layer to a specific XY position within the canvas in Gimp?

Currently, the only way I can find is to just eyeball it with the guides and/or mouse position. I want to specify the exact X and Y coordinates.


Solution 1:

I am afraid that Gimp doesn't include it because it is tedious. It is simply not the appropriate way of aligning elements when you are designing, though I recognize that sometimes it is useful as a short-cut. Anyway, the best (correct) approach is with guides:


A) Step 1 - Create the guides

  1. Go to Image->Guide->New Guide
  2. Specify if you want and horizontal or vertical guide
  3. Specify the number of pixels
  4. Repeat the procedure for another horizontal or vertical Guide (you can also do more guides to specify width and height)

Alternatively, you can also create the guides dragging from the rulers:

  1. Drag down (starting from the top ruler) a guide to the Y coordinate you want.
  2. Drag down (starting from the left ruler) a guide to the X coordinate you want.

B) Step 2 - Move the canvas

You can use the moving tool.

  1. Select your Layer
  2. Go to Tools -> Transform Tools -> Move
  3. Drag the Layer to the guides. Gimp will give you a hand with the exact pixels.

One of the design principle is that you should have things align in your whole project. Reducing the number of alignments (guides) helps you getting a cleaner design. I think this is why gimp does not include a tool to specify the exact coordinates. If you want to follow this design principle specifying exact coordinates one by one becomes just a tedious labour.

Solution 2:

  1. Pick enter image description here (alignment tool).
  2. Make it Relative to Image.
  3. Click on your layer (in the canvas).
  4. Enter X in the Offset field.
  5. Click on Distribute / enter image description here (left arrow).
  6. Enter Y in the Offset field.
  7. Click on Distribute / enter image description here (up arrow).

That's it!

Solution 3:

There is a script to do this that can be download from the GIMP Plugin registry. It is called:

Move Layer To (download).

To install:

  1. Move the script to %USERPROFILE\.gimp-2.8\scripts directory on Windows, ~/Library/Application Support/GIMP/2.8/scripts on OS X or ~/.gimp-2.8/scripts on Linux. (Official instructions)

  2. Clicks Filters -> Script-Fu -> Refresh scripts.

  3. The new menu item will appear at the bottom of the Layer menu Move to.

Solution 4:

I'm using GIMP 2.6.11.

With these lines of Python the active layer can be moved to an absolute position, like (32, 64), from the Python console:

>>> x_new = 32
>>> y_new = 64
>>> img = gimp.image_list()[0]
>>> layer = img.layers[0]
>>> x_off, y_off = layer.offsets
>>> pdb.gimp_layer_translate(layer, x_new - x_off, y_new - y_off)

Or you could do this more simply using gimp_layer_set_offsets like:

pdb.gimp_layer_set_offsets(layer, x_new, y_new)

Alternatively, if you only want to move the content of the layer:

Right-click, Layer -> Transform -> Offset

or Shift+Ctrl+O.

Solution 5:

There's a very convenient way to do this available since the Gimp v.2.10:

  1. double click on the layer you want to move (or right click on it and select "Edit Layer Attributes")

  2. the "Edit Layer Attributes" dialog will show up and there you can change the X/Y offsets to your needs

Simply easy like that! :)

Edit Layer X/Y Offsets Attributes

EDIT:

As @Michael asked about it in its comment to my answer, I'm adding a script that will move ALL image layers by specified x,y offsets.

To make it work you need to create a file in the Gimp script folder (some reference for this if you need it: here or here) with following content:

; This script is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This script is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.

;; Command is installed in "Layer->Move all layers..."
;;
;; The purpose of this script is to move all image layers by specified x,y offsets
;; X and Y offset parameters must be provided (use integer numbers as values)
;;


(define (dn-move-all-layers orig-image drawable
                                       x-offset y-offset)
  (define (get-all-layers img)
    (let* (
      (all-layers (gimp-image-get-layers img))
      (i (car all-layers))
      (bottom-to-top ())
     )
     (set! all-layers (cadr all-layers))
     (while (> i 0)
       (set! bottom-to-top (append bottom-to-top (cons (aref all-layers (- i 1)) '())))
       (set! i (- i 1))
     )
     bottom-to-top
    )
  )
  (define (move-layer orig-image layer-id offset-x offset-y)
    (gimp-layer-set-offsets
      layer-id
      offset-x
      offset-y
    )
  )
  (let* (
      (layers nil)
      (layerpos 1)
      (layer-id "")
      (x-os 0)
      (y-os 0)
      (orig-selection 0)
   )
   (gimp-image-undo-disable orig-image)
   (set! orig-selection (car (gimp-selection-save orig-image)))
   (gimp-selection-none orig-image)

   (set! x-os x-offset)
   (set! y-os y-offset)
   (set! layers (get-all-layers orig-image))
   (while (pair? layers)
     (move-layer orig-image (car layers) x-os y-os)
     (set! layers (cdr layers))
     (set! layerpos (+ layerpos 1))
   )
   (gimp-displays-flush)
   (gimp-selection-load orig-selection)
   (gimp-image-remove-channel orig-image orig-selection)
   (gimp-image-undo-enable orig-image)
  )
)

(script-fu-register "dn-move-all-layers"
 "Move all layers..."
 "Move each layer by specified x,y offsets."
 "danicotra"
 "danicotra"
 "08/08/2019"
 ""
 SF-IMAGE "Input image" 0
 SF-DRAWABLE "Drawable" 0
 SF-VALUE "X offset" "0"
 SF-VALUE "Y offset" "0"
)

(script-fu-menu-register "dn-move-all-layers"
                         "<Image>/Layer/")

If you do it right you will find a new command in the "Layer" menu called "Move all layers...", launch it and a dialog will show up letting you to decide X and Y offsets. That's it.