繁体   English   中英

调整大小和裁剪图像(photoshop脚本)

[英]Resize and crop image (photoshop script)

我正在尝试调整大小和裁剪文件夹中的多个图像。 首先,让我们看一下脚本的某些部分:

    // DOCUMENT SETTINGS
    app.preferences.rulerUnits = Units.MM;
    app.displayDialogs = DialogModes.NO;

    // FUNCTIONS
    function processing_f_alta(folder, files, w, h) {
        var f_alta = new Folder(folder + "/ALTA");
        if ( ! f_alta.exists ) { f_alta.create(); }

        for ( var cont = 0; cont < files.length; cont++ ) {
            files[cont].copy(decodeURI(f_alta) + "/" + files[cont].displayName);
        }

        var files = f_alta.getFiles("*.tif");
        for ( var cont = 0; cont < files.length; cont++ ) {
            var img_file = app.open(files[cont]);

            img_file.resizeImage(UnitValue(w, "cm"), UnitValue(h, "cm"), null, ResampleMethod.BICUBIC);
            img_file.resizeCanvas(UnitValue(w, "cm"), UnitValue(h, "cm"), AnchorPosition.MIDDLECENTER);

            img_file.close(SaveOptions.SAVECHANGES);
        }
    }

    var w =prompt("Width (cm)","","Introduzca valor");
    var h =prompt("Height (cm)","","Introduzca valor");

    var f_origin_folder = Folder.selectDialog("Select the containing folder of the images");
    var folder = new Folder(f_origin_folder);
    var files = folder.getFiles("*.tif");

    processing_f_alta(folder, files, w/2, h/2);

该脚本具有更多代码,但无关紧要。

这个想法是从键盘上获得一个平滑的宽度(“ w”)和高度(“ h”),并在图像被获取时获得文件夹(“文件夹”)。 因此,脚本将获取此文件夹的所有“ .tif”文件,并将其保存到变量“ files”中。

使用几个参数( 文件夹,文件,w / 2,h / 2 )调用函数processing_f_alta( )。 最后一个参数为何除以2是无关紧要的。

进入函数后,脚本会在名为“ ALTA”的“文件夹”中创建一个新文件夹,并将所有“ .tif”文件复制到其中。 然后,脚本将获取所有这些最后的“ .tif”文件,并将它们调整为具有( w / 2 )和高度( h / 2 )的新值。

直到这里一切都还好。

现在出现了问题。 我想在不失真的情况下裁剪文件,但是我不知道该怎么做。

让我们看一个真实的示例(我正在测试的示例)。

我在名为“测试”的文件夹中得到了40x40cm的图像。 我使用以下值执行脚本:w = 30,h = 15,folder =“ test”。 当我运行脚本时,将进入名为“ ALTA”的“ test”文件夹,该文件夹的大小调整为15x7,5cm。 正确的。 但是当我打开文件时,它没有被裁剪。 它已垂直变形。 我想要得到的是这个结果,但是图像被垂直裁剪,并且图像变形了。

尝试crop(),resizeCanvas()函数,但无法获得预期的结果。

你能帮我解决我的问题吗?

在此先感谢您的时间。

尝试:

img_file.resizeImage(null, UnitValue(h, "cm"), null, ResampleMethod.BICUBIC);
if(img_file.width < w){
    img_file.resizeImage(UnitValue(w, "cm"), null, null, ResampleMethod.BICUBIC);
}

(另外:我在这台机器上没有Photoshop,所以这是未经测试的代码,但是基本上,在裁剪图像之前,您需要先调整高度,然后如果宽度小于您想要的宽度,则将其放大)

现在可行:

// resizeImage([width] [, height] [, resolution] [, sampleMethod] [, amount]);
        img_file.resizeImage(UnitValue(w, "cm"), null, null, ResampleMethod.BICUBIC);
        // crop(bounds [, angle] [, width] [, height]);
        //      bounds = array[left, top, right, bottom]
        bounds = [
            0,
            0,
            w*10,
            h*10
        ];
        img_file.crop(bounds);

下一步将是从图像的中心进行裁切(现在是从左上角开始裁切)。

PD:我做了w * 10和h * 10,因为例如,如果原始w = 30和h = 30,它将图像裁剪为w-> 3和h-> 3。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM