GIMP Python, 将层复制到新图像中,然后复制其中的一部分
GIMP Python, copy layer into new image and then copy part of it
对于游戏的图块处理,我想将许多层(=图像)复制到一个新图像中并“挤出”每个图像。这意味着我想将每个图像扩展一个像素到顶部、底部、左侧 and/or 右侧。
为了测试这一点,我在 Python 宏中执行了以下操作;
- 创建新图像
- 复制现有图像
- 将新粘贴的图像移动到目标位置
- 复制新图像的顶部
- 将顶部粘贴到目标位置稍上方
这是代码
def copy_test123(image):
orglayer = image.layers[0]
# 1) create new image and layer
imgNew = gimp.Image(640, 480, RGB)
newLayer = gimp.Layer(imgNew, "copytest", 640, 480, RGBA_IMAGE, 100, NORMAL_MODE)
imgNew.add_layer(newLayer, 1)
# 2) Copy layer from original layer and paste it into a "floating" layer in the new image
pdb.gimp_edit_copy(orglayer)
floatingLayer = pdb.gimp_edit_paste(newLayer, TRUE)
# determine new position
xgoal = 120
ygoal = 80
# 3) Floating layer defaults to center, more to 120,80
xOffset, yOffset = floatingLayer.offsets
xOffset = xgoal - xOffset
yOffset = ygoal - yOffset
# Move the floating layer into the correct position
pdb.gimp_layer_translate(floatingLayer, xOffset, yOffset)
# NOW COPY THE TOP BAR (60 pixels high) FROM THE PASTED SELECTION
##
## # 4) copy the top part of the newly added image part
## pdb.gimp_image_select_rectangle(imgNew, CHANNEL_OP_REPLACE, xgoal, ygoal, orglayer.width, 60)
## pdb.gimp_edit_copy(floatingLayer)
## floatselection = pdb.gimp_edit_paste(newLayer, TRUE)
##
## # 5) move the new extra top part slightly above the image
## xOffset, yOffset = floatselection.offsets
## xOffset = xgoal - xOffset
## yOffset = ygoal - 60 - yOffset
##
## # Move the floating layer into the correct position
## pdb.gimp_layer_translate(floatselection, xOffset, yOffset)
# Create and show a new image window for our spritesheet
gimp.Display(imgNew)
gimp.displays_flush()
然而结果不是我所期望的,请看下面的截图。
如果我将第 4 步和第 5 步注释掉,就像在示例代码中一样,那么它将只显示新图像,而不会像预期的那样显示顶部的额外副本。但是,如果我 uncomment/activate 第 4 步和第 5 步的代码,那么结果是 只有 顶部并且它的位置不正确,它在图像中应该稍微高一点。
顺便说一句,在 gimp_edit_paste
下的 GIMP api documentation 中,它说您可以在选区“后面”复制并将选区用作蒙版,但我不明白这是什么意思。哪个选区,您正在粘贴的图像中的当前选区,还是刚刚复制的部分的选区?你为什么要用它作为面具?
有谁知道我做错了什么,我怎样才能得到预期的结果?
编辑:
感谢@xenoid 的回答,对于任何偶然发现此问题的人,这里是调整后的代码以获得所需的结果。
def copy_test123(image):
orglayer = image.layers[0]
# 1) create new image and layer
imgNew = gimp.Image(640, 480, RGB)
newLayer = gimp.Layer(imgNew, "copytest", 640, 480, RGBA_IMAGE, 100, NORMAL_MODE)
imgNew.add_layer(newLayer, 1)
# 2) Copy layer from original layer and paste it into a "floating" layer in the new image
pdb.gimp_edit_copy(orglayer)
floatingLayer = pdb.gimp_edit_paste(newLayer, FALSE)
# determine new position
xgoal = 120
ygoal = 80
# 3) Floating layer defaults to center, more to 120,80
xOffset, yOffset = floatingLayer.offsets
xOffset = xgoal - xOffset
yOffset = ygoal - yOffset
# Move the floating layer into the correct position
pdb.gimp_layer_translate(floatingLayer, xOffset, yOffset)
# Anchor the floating selection before making another selection
pdb.gimp_floating_sel_anchor(floatingLayer)
# NOW COPY THE TOP BAR (60 pixels high) FROM THE PASTED SELECTION
# 4) copy the top part of the newly added image part
pdb.gimp_image_select_rectangle(imgNew, CHANNEL_OP_REPLACE, xgoal, ygoal, orglayer.width, 60)
pdb.gimp_edit_copy(newLayer)
floatselection = pdb.gimp_edit_paste(newLayer, FALSE)
# 5) move the new extra top part slightly above the image
xOffset, yOffset = floatselection.offsets
xOffset = xgoal - xOffset
yOffset = ygoal - 60 - yOffset
# Move the floating layer into the correct position
pdb.gimp_layer_translate(floatselection, xOffset, yOffset)
# Create and show a new image window for our spritesheet
gimp.Display(imgNew)
gimp.displays_flush()
我测试有点晚了,但我认为你的问题是你的第二个粘贴在它被锚定之前替换了第一个粘贴中的浮动选择。您应该先锚定您的浮动选区 (pdb.gimp_floating_sel_anchor(floating_sel)
),然后再进行另一次剪切。
对于游戏的图块处理,我想将许多层(=图像)复制到一个新图像中并“挤出”每个图像。这意味着我想将每个图像扩展一个像素到顶部、底部、左侧 and/or 右侧。
为了测试这一点,我在 Python 宏中执行了以下操作;
- 创建新图像
- 复制现有图像
- 将新粘贴的图像移动到目标位置
- 复制新图像的顶部
- 将顶部粘贴到目标位置稍上方
这是代码
def copy_test123(image):
orglayer = image.layers[0]
# 1) create new image and layer
imgNew = gimp.Image(640, 480, RGB)
newLayer = gimp.Layer(imgNew, "copytest", 640, 480, RGBA_IMAGE, 100, NORMAL_MODE)
imgNew.add_layer(newLayer, 1)
# 2) Copy layer from original layer and paste it into a "floating" layer in the new image
pdb.gimp_edit_copy(orglayer)
floatingLayer = pdb.gimp_edit_paste(newLayer, TRUE)
# determine new position
xgoal = 120
ygoal = 80
# 3) Floating layer defaults to center, more to 120,80
xOffset, yOffset = floatingLayer.offsets
xOffset = xgoal - xOffset
yOffset = ygoal - yOffset
# Move the floating layer into the correct position
pdb.gimp_layer_translate(floatingLayer, xOffset, yOffset)
# NOW COPY THE TOP BAR (60 pixels high) FROM THE PASTED SELECTION
##
## # 4) copy the top part of the newly added image part
## pdb.gimp_image_select_rectangle(imgNew, CHANNEL_OP_REPLACE, xgoal, ygoal, orglayer.width, 60)
## pdb.gimp_edit_copy(floatingLayer)
## floatselection = pdb.gimp_edit_paste(newLayer, TRUE)
##
## # 5) move the new extra top part slightly above the image
## xOffset, yOffset = floatselection.offsets
## xOffset = xgoal - xOffset
## yOffset = ygoal - 60 - yOffset
##
## # Move the floating layer into the correct position
## pdb.gimp_layer_translate(floatselection, xOffset, yOffset)
# Create and show a new image window for our spritesheet
gimp.Display(imgNew)
gimp.displays_flush()
然而结果不是我所期望的,请看下面的截图。
如果我将第 4 步和第 5 步注释掉,就像在示例代码中一样,那么它将只显示新图像,而不会像预期的那样显示顶部的额外副本。但是,如果我 uncomment/activate 第 4 步和第 5 步的代码,那么结果是 只有 顶部并且它的位置不正确,它在图像中应该稍微高一点。
顺便说一句,在 gimp_edit_paste
下的 GIMP api documentation 中,它说您可以在选区“后面”复制并将选区用作蒙版,但我不明白这是什么意思。哪个选区,您正在粘贴的图像中的当前选区,还是刚刚复制的部分的选区?你为什么要用它作为面具?
有谁知道我做错了什么,我怎样才能得到预期的结果?
编辑: 感谢@xenoid 的回答,对于任何偶然发现此问题的人,这里是调整后的代码以获得所需的结果。
def copy_test123(image):
orglayer = image.layers[0]
# 1) create new image and layer
imgNew = gimp.Image(640, 480, RGB)
newLayer = gimp.Layer(imgNew, "copytest", 640, 480, RGBA_IMAGE, 100, NORMAL_MODE)
imgNew.add_layer(newLayer, 1)
# 2) Copy layer from original layer and paste it into a "floating" layer in the new image
pdb.gimp_edit_copy(orglayer)
floatingLayer = pdb.gimp_edit_paste(newLayer, FALSE)
# determine new position
xgoal = 120
ygoal = 80
# 3) Floating layer defaults to center, more to 120,80
xOffset, yOffset = floatingLayer.offsets
xOffset = xgoal - xOffset
yOffset = ygoal - yOffset
# Move the floating layer into the correct position
pdb.gimp_layer_translate(floatingLayer, xOffset, yOffset)
# Anchor the floating selection before making another selection
pdb.gimp_floating_sel_anchor(floatingLayer)
# NOW COPY THE TOP BAR (60 pixels high) FROM THE PASTED SELECTION
# 4) copy the top part of the newly added image part
pdb.gimp_image_select_rectangle(imgNew, CHANNEL_OP_REPLACE, xgoal, ygoal, orglayer.width, 60)
pdb.gimp_edit_copy(newLayer)
floatselection = pdb.gimp_edit_paste(newLayer, FALSE)
# 5) move the new extra top part slightly above the image
xOffset, yOffset = floatselection.offsets
xOffset = xgoal - xOffset
yOffset = ygoal - 60 - yOffset
# Move the floating layer into the correct position
pdb.gimp_layer_translate(floatselection, xOffset, yOffset)
# Create and show a new image window for our spritesheet
gimp.Display(imgNew)
gimp.displays_flush()
我测试有点晚了,但我认为你的问题是你的第二个粘贴在它被锚定之前替换了第一个粘贴中的浮动选择。您应该先锚定您的浮动选区 (pdb.gimp_floating_sel_anchor(floating_sel)
),然后再进行另一次剪切。