我不知道基于 Python 的答案是否适合您,但这是我得到的,通过一些工作,您可以将此脚本复制并粘贴到您在 Maya 中的脚本编辑器或在您的 Python 解释器中执行它。
此脚本需要安装 PIL 库。
要在 Maya 上安装它,您可以点击此链接 Mistermatti's blog。解压缩 file 并将 PIL 文件夹粘贴到您的 Maya 安装中:Maya20XX\Python\Lib\site-packages
这适用于 Maya2014,尚未在其他版本上测试过。
from PIL import Image
import time
start_time = time.time()
#parses the image to find the first pixel on the x axis which is not blank
def parseXImage(pixelMap, resWidth, resHeight):
for x in range(0, int(resWidth/2)):
for y in range(0, int(resHeight/2)):
if (pixelMap[x,y] != (0, 0, 0, 0)) or \
(pixelMap[x,resHeight-y-1] != (0, 0, 0, 0)) or \
(pixelMap[resWidth-x-1,y] != (0, 0, 0, 0)) or \
(pixelMap[resWidth-x-1,resHeight-y-1] != (0, 0, 0, 0)):
return x
return None
#parses the image to find the first pixel on the y axis which is not blank
def parseYImage(pixelMap, resWidth, resHeight):
topFound = False
bottomFound = False
yTop = None
yBottom = None
for y in range(0, int(resHeight/2)):
for x in range(0, int(resWidth/2)):
if not topFound:
if (pixelMap[x,y] != (0, 0, 0, 0)) or \
(pixelMap[resWidth-x-1,y] != (0, 0, 0, 0)):
yTop = y
topFound = True
if not bottomFound:
if (pixelMap[x,resHeight-y-1] != (0, 0, 0, 0)) or \
(pixelMap[resWidth-x-1,resHeight-y-1] != (0, 0, 0, 0)):
yBottom = y
bottomFound = True
return yTop, yBottom
imageList = [r"Path\To\Image\Mod_Turn.001.png",
r"Path\To\Image\Mod_Turn.002.png",
r"Path\To\Image\Mod_Turn.003.png",
r"Path\To\Image\Mod_Turn.004.png",
r"Path\To\Image\Mod_Turn.005.png"]
#get images resolution
imagePil = Image.open(imageList[0])
resWidth, resHeight = imagePil.size #get the resolution of the first image in the list
imagePil.close()
valueWidthList = []
valueHeightTopList = []
valueHeightBottomList = []
for imageListIndex, imagePath in enumerate(imageList):
imagePil = Image.open(imagePath)
pixelMap = imagePil.load()
xValue = parseXImage(pixelMap, resWidth, resHeight) #now parse the image to find x
yTopValue, yBottomValue = parseYImage(pixelMap, resWidth, resHeight) #now parse the image to find y
valueWidthList.append(xValue) #Store it
valueHeightTopList.append(yTopValue) #Store it
valueHeightBottomList.append(yBottomValue) #Store it
imagePil.close()
xValueToCrop = min(valueWidthList) #Get the minimal value of X crop among all the entries
yTopValueToCrop = min(valueHeightTopList) #Get the minimal value of Y crop among all the entries
yBottomValueToCrop = min(valueHeightBottomList) #Get the minimal value of Y crop among all the entries
#This is the crop square we will use
cropSquare = (xValueToCrop, yTopValueToCrop, resWidth-xValueToCrop, resHeight-yBottomValueToCrop )
for imagePath in imageList:
imagePil = Image.open(imagePath)
#Replace Turn by something in the name of your image
imagePil.crop(cropSquare).save(imagePath.replace("Turn", "Turn_Cropped")) #Crop dans save the image
imagePil.close()
print("--- %s seconds ---" % (time.time() - start_time))
您需要在脚本的最后使用自己的图像路径和"Turn", "Turn_Cropped" 更改 imageList dict。
仅供参考,以 720p 处理 300 张图像大约需要 250 秒。这可以改进。