Tue Aug 14, 2018 11:07 pm
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gimpfu import *
def celenicorn_auto_mirror(image, drawable, direction = 0) :
width = image.width
height = image.height
pdb.gimp_selection_all(image)
pdb.gimp_edit_copy_visible(image)
def process(vhflip, sizex, sizey, ofsx, ofsy, movx, movy):
pdb.gimp_image_resize(image, sizex, sizey, ofsx, ofsy)
newLayer = pdb.gimp_drawable_transform_flip_simple(pdb.gimp_edit_paste(drawable, False), vhflip, True, 0, False)
pdb.gimp_selection_none(image)
pdb.gimp_layer_resize_to_image_size(drawable)
pdb.gimp_layer_set_offsets(newLayer, movx, movy)
pdb.gimp_floating_sel_anchor(newLayer)
def up():
process(1, width, height * 2, 0, height, 0, 0)
def down():
process(1, width, height * 2, 0, 0, 0, height)
def left():
process(0, width * 2, height, width, 0, 0, 0)
def right():
process(0, width * 2, height, 0, 0, width, 0)
switcher = {
0: up,
1: down,
2: left,
3: right
}
func = switcher.get(direction, lambda:"Invalid Direction")
func()
register(
"celenicorn_auto_mirror",
"Mirror the image in a specific direction.",
"Mirror the image in a specific direction.",
"Arlo Horner",
"Arlo Horner",
"2018",
"<Image>/Celenicorn/Cosmic Auto Mirror",
"",
[
(PF_RADIO, "direction", "Mirror towards which direction: ", 0,
(
("Up", 0),
("Down", 1),
("Left", 2),
("Right", 3)
)
)
],
[],
celenicorn_auto_mirror)
main()
Tue Aug 14, 2018 11:49 pm
Wed Aug 15, 2018 7:14 am
Wed Aug 15, 2018 2:15 pm
ofnuts wrote:On my Linux this crashes Gimp about every other execution. One of the problem can be that you are using the clipboard more than necessary. For what you do you can just duplicate the layer, flip it, move it, and then merge down. Operations with the clipboard also impact other processes, which can be slow to react.
Also, you should bracket your code with undo_group_start()/undo_group_end() so that only one undo operation is necessary.
def celenicorn_auto_mirror(image, drawable, direction = 0) :
width = image.width
height = image.height
image.undo_group_start()
newLayer = drawable.copy()
image.add_layer(newLayer, 0)
def process(vhflip, sizex, sizey, ofsx, ofsy, movx, movy):
pdb.gimp_image_resize(image, sizex, sizey, ofsx, ofsy)
pdb.gimp_drawable_transform_flip_simple(newLayer, vhflip, True, 0, False)
pdb.gimp_layer_set_offsets(newLayer, movx, movy)
image.flatten()
def up():
process(1, width, height * 2, 0, height, 0, 0)
def down():
process(1, width, height * 2, 0, 0, 0, height)
def left():
process(0, width * 2, height, width, 0, 0, 0)
def right():
process(0, width * 2, height, 0, 0, width, 0)
switcher = {
0: up,
1: down,
2: left,
3: right
}
func = switcher.get(direction, lambda:"Invalid Direction")
func()
image.undo_group_end()
image.resize(sizex, sizey, ofsx, ofsy)
pdb.gimp_drawable_transform_flip_simple(newLayer, vhflip, True, 0, False)
newLayer.set_offsets(movx, movy)
image.flatten()
Wed Aug 15, 2018 4:11 pm
def celenicorn_auto_mirror(image, drawable, direction = 0) :
image.undo_group_start()
width = image.width
height = image.height
newLayer = drawable.copy()
image.add_layer(newLayer, 0)
dirParms={
0: (1, width, height * 2, 0, height, 0, 0),
1: (1, width, height * 2, 0, 0, 0, height),
2: (0, width * 2, height, width, 0, 0, 0),
3: (0, width * 2, height, 0, 0, width, 0)
}
vhflip, sizex, sizey, ofsx, ofsy, movx, movy=dirParms[direction]
pdb.gimp_image_resize(image, sizex, sizey, ofsx, ofsy)
pdb.gimp_drawable_transform_flip_simple(newLayer, vhflip, True, 0, False)
pdb.gimp_layer_set_offsets(newLayer, movx, movy)
image.flatten()
image.undo_group_end()