酷代码 AI
菜单
服务商

帮我写一个Photoshop 版本: 24.0.1脚本,用于截切图片上下多余白色空白,主图居中

以下是一个可以在Photoshop 24.0.1中使用的脚本,用于截切图片上下多余白色空白并将主图居中。 ```javascript // 获取当前活动文档 var doc = app.activeDocument; // 定义一个函数来查找图像的边界 function findContentBounds() { // 获取文档的宽度和高度 var width = doc.width.value; var height = doc.height.value; // 初始化顶部、底部边界 var top = 0; var bottom = height - 1; // 查找顶部边界 for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { var pixelColor = doc.channels[2].getPixel(x, y); if (pixelColor < 255) { top = y; break; } } if (top > 0) { break; } } // 查找底部边界 for (var y = height - 1; y >= 0; y--) { for (var x = 0; x < width; x++) { var pixelColor = doc.channels[2].getPixel(x, y); if (pixelColor < 255) { bottom = y; break; } } if (bottom < height - 1) { break; } } return { top: top, bottom: bottom }; } // 查找图像的边界 var bounds = findContentBounds(); // 裁剪图像 var newHeight = bounds.bottom - bounds.top + 1; var newTop = (doc.height.value - newHeight) / 2; var cropRect = [0, bounds.top, doc.width.value, bounds.bottom + 1]; doc.crop(cropRect); // 移动图像到居中位置 var moveY = newTop - bounds.top; if (moveY !== 0) { var idmove = charIDToTypeID("move"); var desc15 = new ActionDescriptor(); var idnull = charIDToTypeID("null"); desc15.putClass(idnull, charIDToTypeID("Lyr ")); var idT = charIDToTypeID("T "); var desc16 = new ActionDescriptor(); var idHrzn = charIDToTypeID("Hrzn"); var idAdjt = charIDToTypeID("Adjt"); desc16.putUnitDouble(idHrzn, idAdjt, 0); var idVrtc = charIDToTypeID("Vrtc"); desc16.putUnitDouble(idVrtc, idAdjt, moveY); var idOfst = charIDToTypeID("Ofst"); desc15.putObject(idOfst, idT, desc16); executeAction(idmove, desc15, DialogModes.NO); } ``` 额外需要注意的逻辑点 1. 该脚本假设图像是灰度图或者只通过红色通道(`doc.channels[2]`)来判断像素是否为白色。如果是彩像,可能需要调整判断逻辑,以更准确地识别白色像素。 2. 脚本在裁剪和移动图像时,可能会受到图像分辨率等因素的影响。如果图像分辨率非常高,可能会导致性能问题。 3. 脚本没有处理图像本身没有多余白色空白的情况,在这种情况下,裁剪和移动操作可能是不必要的,可以添加相应的判断逻辑来避免这种情况。 [2025-06-21 11:08:01 | AI写代码神器 | 833点数解答]

相关提问
请收藏新域名:pdcd.cn
发财导航,免费问AI
实用工具查看更多
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]
数独游戏 [娱乐类]
经典推箱子 [娱乐类]
AI摸鱼五子棋 [娱乐类]