Python 智能裁剪图像

Python 智能裁剪图像,python,image,python-imaging-library,crop,Python,Image,Python Imaging Library,Crop,我正在寻找一种方法,我可以使用自动裁剪几个地块。。如果没有,我必须手动设置裁剪的框大小 我需要裁剪一张像这样的光谱图列表 我只需要实际的情节,其他什么都不需要。只是情节 目前我正在这样裁剪它 print "Hstacked Image" images1 = Image.open(spectogram_path_train+"/"+name+"_plot_static_conv.png") images2 = Image.open(spectogram_path_train+"/"+name+"

我正在寻找一种方法,我可以使用自动裁剪几个地块。。如果没有,我必须手动设置裁剪的框大小

我需要裁剪一张像这样的光谱图列表

我只需要实际的情节,其他什么都不需要。只是情节

目前我正在这样裁剪它

print "Hstacked Image"
images1 = Image.open(spectogram_path_train+"/"+name+"_plot_static_conv.png")
images2 = Image.open(spectogram_path_train+"/"+name+"_plot_delta_conv.png")
images3 =      Image.open(spectogram_path_train+"/"+name+"_plot_delta_delta_conv.png")

box = (100,55,592,496)
cropped1  = images1.crop(box)
cropped2  = images2.crop(box)
cropped3  = images3.crop(box)

width1, height1 = cropped1.size
width2, height2 = cropped2.size
width3, height3 = cropped3.size

sum_width  = width1 + width2 + width3
max_height = max(height1,height2,height3)

new_im = Image.new('RGB',(sum_width,max_height))
x_offset = 0

for im in [cropped1,cropped2,cropped3]:
    new_im.paste(im,(x_offset,0))
    x_offset+=im.size[0]

new_im.save(spectogram_path_train+"/"+name+"_plot_hstacked.png")
这些框值设置为裁剪此图像。。对于每个绘图,框的左参数和下参数始终相同,但右参数可能不同,必须为每个绘图自动确定


我正在寻找一种智能作物,它能够去除除了彩色地块以外的所有东西

我不懂Python,但是您可以使用安装在大多数Linux发行版上的、适用于macOS和Windows的ImageMagick在终端上不使用任何高级语言

首先,请注意,由于某些原因,您的图像有一个多余的alpha通道,因此我将其关闭

然后,我注意到所有你感兴趣的东西都是饱和颜色,所有无关的文本都是黑色/灰色和不饱和的,所以我将把饱和作为判别标准。该命令输入到终端中,加载图像并将所有像素设置为黑色,即零,其中像素是非饱和的,并在其他地方保留其当前值。然后修剪边框并保存结果

convert spectrum.png -alpha off -fx "saturation<0.2?0:u" -trim z.png

convert spectrum.png-alpha off-fx“饱和度所以..我决定按照@martineau的建议做一个解决方案,利用它

images1 = Image.open(static)
images2 = Image.open(delta)
images3 = Image.open(delta_delta)

data_numpy = np.array(images1)
number = 0
right = 0

for i in data_numpy[55,:]:
#    print i
    number+=1
    if i[0] == 234 and i[1] == 234 and i[2] == 242 and i[3] == 255 and number > 100:
#        print "Found it!"
        right = number
        break
    if i[0] == 255 and i[1] == 255 and i[2] == 255 and i[3] == 255 and number > 100:
        right = number
        break
#print right

box = (100,55,right,496)

cropped1  = images1.crop(box)
cropped2  = images2.crop(box)
cropped3  = images3.crop(box)
如果不是的话,我希望代码能说明问题


for循环迭代(由于绘图的大小,只需检查一行)通过图像,找到与灰色图像相同的像素位置。找到该位置后,for循环将中断,并且将创建一个适合所需大小的框。

除非您可以找到专门的第三方模块来执行类似操作,否则您可以通过查看像素值来确定挖掘上边缘和右边缘所在的位置。如果绘图图像在白色背景范围内都是相似的,那么您应该能够通过从左侧位置向右搜索直到几个背景像素开始,然后向上搜索上边缘来找到边界。哦……对不起。我按照@martineau提出的方法解决了这个问题。解决方案是很好…我没有尝试你的解决方案..我需要在代码中裁剪它,因为我需要裁剪并将它们堆叠在一起..我想这需要更多的代码。。
convert spectrum.png -alpha off -fx "saturation<0.2?0:u" -trim +repage -crop 496x+0+0 z.png
x=$(convert spectrum.png -alpha off -fx "saturation<0.2?0:u" -trim +repage -crop x1! txt: | awk -F, '/black/{print $1;exit}')
convert spectrum.png -alpha off -fx "saturation<0.2?0:u" -trim +repage -crop ${x}x+0+0 y.png
images1 = Image.open(static)
images2 = Image.open(delta)
images3 = Image.open(delta_delta)

data_numpy = np.array(images1)
number = 0
right = 0

for i in data_numpy[55,:]:
#    print i
    number+=1
    if i[0] == 234 and i[1] == 234 and i[2] == 242 and i[3] == 255 and number > 100:
#        print "Found it!"
        right = number
        break
    if i[0] == 255 and i[1] == 255 and i[2] == 255 and i[3] == 255 and number > 100:
        right = number
        break
#print right

box = (100,55,right,496)

cropped1  = images1.crop(box)
cropped2  = images2.crop(box)
cropped3  = images3.crop(box)