有谁能帮我弄清楚我的图像自动裁剪脚本中发生了什么?我有一个png图像,有一个很大的透明区域/空间。我想能够自动裁剪出空间,留下必需品。原始图像有一个正方形的画布,最理想的情况是它是矩形的,只封装了分子。
原图如下:
在google上搜索时,我发现了PIL/python代码,据报道这些代码在我的手中可以工作,但是在裁剪图像的时候运行下面的代码。
import Image
import sys
image=Image.open('L_2d.png')
image.load()
imageSize = image.size
imageBox = image.getbbox()
imageComponents = image.split()
rgbImage = Image.new("RGB", imageSize, (0,0,0))
rgbImage.paste(image, mask=imageComponents[3])
croppedBox = rgbImage.getbbox()
print imageBox
print croppedBox
if imageBox != croppedBox:
cropped=image.crop(croppedBox)
print 'L_2d.png:', "Size:", imageSize, "New Size:",croppedBox
cropped.save('L_2d_cropped.png')
结果是:
熟悉图像处理/PLI的人能帮我解决这个问题吗?
我测试了这篇文章中的大部分答案,但是,我最终得到了自己的答案。我用的是PythonPython。
对我来说,它的作用是:
当您通过
mask=imageComponents[3]
搜索边界时,只能通过蓝色通道搜索。可以使用numpy,将图像转换为数组,查找所有非空的列和行,然后从中创建图像:
结果看起来像
如果有什么不清楚的,尽管问。
相关问题 更多 >
编程相关推荐