Gimp,python-fu:相对于图像中的背景图层对齐图层

时间:2017-05-30 00:26:24

标签: python gimp script-fu python-fu gimpfu

以下代码用于相对于底部背景图层对齐图层。

import os    
from gimpfu import *
import gimpfu    
import logging

def scale(logo, tdrawable, imageName):
    logger = logging.getLogger()
    logger.info('got logo file {0}'.format(imageName))    
    newImage = pdb.gimp_image_new(450, 300, RGB)    
    newLayer = pdb.gimp_layer_new(newImage, 450, 300, 0, "background", 100.0, NORMAL_MODE)

    pdb.gimp_context_set_background((255, 255, 255))
    # pdb.gimp_context_set_foreground((255, 255, 255))
    pdb.gimp_drawable_fill(newLayer, gimpfu.BACKGROUND_FILL)
    logger.info('Created new background image {0}')
    newImage.add_layer(newLayer, 0)

    logger.info('loading logo {0}'.format(imageName))
    logo = pdb.gimp_file_load(imageName, imageName)
    visibleLayer = pdb.gimp_layer_new_from_visible(logo, newImage, 'logo')
    pdb.gimp_image_add_layer(newImage, visibleLayer, 0)


    logger.info('Scaling logo to 435 width')
    pdb.gimp_layer_scale(visibleLayer, 435, 100, True)

    # logger.info('Align logo to background image')
    pdb.plug_in_align_layers(newImage, tdrawable, True, True)

    flattenedLayer = pdb.gimp_image_flatten(newImage)

    fileNameNoExt = os.path.splitext(imageName)[0]
    pdb.file_jpeg_save(newImage, flattenedLayer, fileNameNoExt + '.jpg', fileNameNoExt + '.jpg', 1.0, 0.0, 1, 1, '', 0, 0, 0, 0)

register(
    "scale_logo",
    "Exports logo in jpeg format after custom scaling",
    "Exports  logo in jpeg format after custom scaling",
    "",
    "",
    "2017",
    "<Image>/Image/Scale  logo...",
    "RGB*, GRAY*",
    [
        (PF_STRING, 'file_name', 'file_name', 'logo.png')],
    [],
    scale)

main()

要执行此任务的预期行pdb.gimp_layer_scale(visibleLayer, 435, 100, True),但生成的文件在背景白色图层的左上角包含徽标图层。 如何正确对齐底层背景层?

1 个答案:

答案 0 :(得分:0)

以下说法对我有用。 感谢 xenoid 获取评论中的建议

import os    
from gimpfu import *
import gimpfu    
import logging
def scale(logo, tdrawable, imageName):
    logger = logging.getLogger()
    logger.info('got logo file {0}'.format(imageName))

    newImage = pdb.gimp_image_new(450, 300, RGB)

    newLayer = pdb.gimp_layer_new(newImage, 450, 300, 0, "background", 100.0, NORMAL_MODE)

    pdb.gimp_context_set_background((255, 255, 255))
    pdb.gimp_drawable_fill(newLayer, gimpfu.BACKGROUND_FILL)
    logger.info('Created new background image {0}')
    newImage.add_layer(newLayer, 0)

    logger.info('loading logo {0}'.format(imageName))
    logo = pdb.gimp_file_load(imageName, imageName)

    visibleLayer = pdb.gimp_layer_new_from_visible(logo, newImage, 'logo')
    pdb.gimp_image_add_layer(newImage, visibleLayer, 0)

    logger.info('Scaling logo to 435 width, 100 height')
    pdb.gimp_layer_scale(visibleLayer, 435, 100, True)

    logger.info('Align logo to background image')
    offx = (newLayer.width - visibleLayer.width) / 2
    offy = (newLayer.height - visibleLayer.height) / 2
    pdb.gimp_layer_set_offsets(visibleLayer, offx, offy)
    logger.info('Sharpening logo')
    pdb.plug_in_unsharp_mask(None, visibleLayer, 100.0, 0.5, 0)

    logger.info('Flatten all visible layers into a single layer')
    flattenedLayer = pdb.gimp_image_flatten(newImage)

    fileNameNoExt = os.path.splitext(imageName)[0]
    pdb.file_jpeg_save(newImage, flattenedLayer, fileNameNoExt + '.jpg', fileNameNoExt + '.jpg', 1.0, 0.0, 1, 1, '', 0, 0, 0, 0)

register(
    "scale_logo",
    "Exports logo in jpeg format after custom scaling",
    "Exports logo in jpeg format after custom scaling",
    "",
    "",
    "2017",
    "<Image>/Image/Scale logo...",
    "RGB*, GRAY*",
    [
        (PF_STRING, 'file_name', 'file_name', 'logo.png')],
    [],
    scale)

main()

要运行它,需要使用以下bash命令序列:

cp scaleAndSharpenLogo.py /usr/lib/gimp/2.0/plug-ins/scaleAndSharpenLogo.py; gimp --no-interface -b '(python-fu-scale-logo RUN-NONINTERACTIVE 0 0 "logo.png")' -b '(gimp-quit 0)'