调整图像大小以适合画布-Gimp [英] Resize image to fit canvas - Gimp

查看:308
本文介绍了调整图像大小以适合画布-Gimp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Gimp调整某些图像的大小.我是一名Web开发人员,但是我并没有真正使用图像处理软件,因为大多数图像都是由设计师提供的,因此Gimp工具对我来说是非常陌生的.我已经浏览了Gimp网站上的所有教程和帮助指南,但找不到最简单的问题的答案:

如何在保持纵横比的同时调整图层大小以适合当前画布?

我实际上是在Canvas上设置固定大小,然后将图像作为图层导入到我的项目中.然后,我希望做的是将这个大得多的图像按比例缩小,以便可以在保留画布的宽高比的情况下放入Canvas中.我找到了一种缩放画布以适合图层的方法,但这不是我想要的.

任何帮助将不胜感激.

解决方案

这是很容易做到,但动辄上可能的选项上的程序界面,它是不是选举"被投入之间在那里.

解决方法是使用程序的脚本功能来执行操作: 必须通过程序确定的是图像/图层的比例是否较大 在宽度或高度上,并使用该比例缩放图层,然后将图层居中.

为方便起见,我在一行中为此编写了一些Python代码,您可以将其复制并粘贴到python控制台(filters-> python-> console)上以应用效果 在最近打开的图像的顶层.

img = gimp.image_list()[0]; layer = img.layers[0]; factor = min (float(img.width) / layer.width, float(img.height) / layer.height); layer.scale(int(layer.width * factor), int(layer.height * factor)); layer.set_offsets((img.width - layer.width) / 2, (img.height - layer.height) / 2)

因为可以这样做,但不切实际,甚至更多,因为它不允许您执行以下操作: 选择要调整大小的图像或图层,我也将其格式化为GIMP的python脚本. 只需检查您的插件目录的编辑"->首选项"->文件夹"->插件", 将下面的内容粘贴为文件(如果在Windows上,文件必须具有".py"扩展名.在Linux和Mac OS上,任何扩展名都可以使用,但是您必须提供该文件 可执行"属性").

重新启动GIMP后,您将在图层"菜单上方便地找到新命令:

#! /usr/bin/env python
# coding: utf-8

from gimpfu import *

def scale_layer_to_canvas_size(img, layer):
    pdb.gimp_image_undo_group_start(img)
    factor = min (float(img.width) / layer.width,
                 float(img.height) / layer.height)

    layer.scale(int(layer.width * factor), int(layer.height * factor))
    layer.set_offsets((img.width - layer.width) / 2,
        (img.height - layer.height) / 2)
    pdb.gimp_image_undo_group_end(img)

register("scale-layer-to-canvas-size",
    "Scale layer to canvas size",
    "Scales the layer to canvas size, keeping the aspect ratio",
    "João S. O. Bueno", "Public domain", "2014",
    N_("Scale layer to canvas size..."),
    "*",
    [(PF_IMAGE, "image",       "Input image", None),
     (PF_DRAWABLE, "layer", "Input drawable", None), ], [],
    scale_layer_to_canvas_size,  menu="<Image>/Layer/",
    )

main()

请注意,它与上面的代码相同,但是GIMP现在支持"img"和"layer" 从菜单中选择操作时,有两个额外的调用,以便 将scalign和居中都作为一个动作撤消"- 剩下的代码就是需要注册的样板 GIMP的功能

I'm currently using Gimp to resize some images. I'm a web developer but I don't really use image manipulation software much as most of the images are provided by designers so the Gimp tool is very unfamiliar to me. I've looked through all of the tutorials and help guides on the Gimp site but I cannot find the answer to the simplest of questions:

How do you resize a layer to fit within the current canvas whilst maintaining the aspect ratio?

I'm essentially setting a fixed size on my Canvas and importing an image as a layer into my project. What I then wish to do is scale this much larger image down so that is can fit within the Canvas with the aspect ratio preserved. I have found a way of scaling the Canvas to fit a layer but this is not what I am looking for.

Any help would be greatly appreciated.

解决方案

This is easy to do, but among the hundreds of possible options to be put on the program UI, it was not "elected" to be there.

The way out is to use the program's scripting capabilities to perform the action: what have to be determined programatcially is whether the ratio of the image/layer is larger on the width or height, and use this ratio to scale tha layer, and then center the layer.

For your convenience, I wrote some Python code for this in a single line, in a way you can just copy and paste on the python console (filters->python->console) to apply the effect on the top layer of the most recent open image.

img = gimp.image_list()[0]; layer = img.layers[0]; factor = min (float(img.width) / layer.width, float(img.height) / layer.height); layer.scale(int(layer.width * factor), int(layer.height * factor)); layer.set_offsets((img.width - layer.width) / 2, (img.height - layer.height) / 2)

Since this can be done, but is not practical, even more because it does not allow you to pick the image or layer to resize, I formated it as a python-script for GIMP as well. Just check your edit->preferences->folders->plug-ins for your plug-in directory, paste the contents bellow as a file there (if on Windows, the file must have the ".py" extension. On Linux and Mac OS, any extension would work, but you have to give the file the "exectuable" property" ).

After restarting GIMP, you will have the new command conveniently located on your Layer menu:

#! /usr/bin/env python
# coding: utf-8

from gimpfu import *

def scale_layer_to_canvas_size(img, layer):
    pdb.gimp_image_undo_group_start(img)
    factor = min (float(img.width) / layer.width,
                 float(img.height) / layer.height)

    layer.scale(int(layer.width * factor), int(layer.height * factor))
    layer.set_offsets((img.width - layer.width) / 2,
        (img.height - layer.height) / 2)
    pdb.gimp_image_undo_group_end(img)

register("scale-layer-to-canvas-size",
    "Scale layer to canvas size",
    "Scales the layer to canvas size, keeping the aspect ratio",
    "João S. O. Bueno", "Public domain", "2014",
    N_("Scale layer to canvas size..."),
    "*",
    [(PF_IMAGE, "image",       "Input image", None),
     (PF_DRAWABLE, "layer", "Input drawable", None), ], [],
    scale_layer_to_canvas_size,  menu="<Image>/Layer/",
    )

main()

Note it is the same code than above, but "img" and "layer" are now suplied by GIMP when picking the action from the menu, and there are two extra calls so that both scalignand centering are "undone" as a single action - the remaining code is justtheneeded boiler plate to register the function with GIMP

这篇关于调整图像大小以适合画布-Gimp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆