首页 > 解决方案 > 裁剪大图像周围的空白区域

问题描述

我正在尝试裁剪大图像周围的空白区域。2000 像素 X 32000 像素。我试过 convert trim 但失败了,也试过 mogrify 也失败了。我尝试安装带有其他错误和警告的 R magick 库。此外,我尝试了 PIL,但它没有安装在我的 Python 2.7 上。无论如何,有一个简单的解决方案可以裁剪我的图像周围的空白区域。任何帮助都深表感谢。我有 100 个这样的图像要修剪。这是我的转换命令:

convert A1.png -fuzz 0% -trim +repage A_N.png'

标签: imagewhitespacecrop

解决方案


有点晚了,但我刚刚写了这个图像裁剪器:

# Load packages
import matplotlib.pyplot as plt
import numpy as np
import glob

# List all logo files
path = r"K:\Folder/"
files = [f for f in glob.glob(path + "*logo.png", recursive=True)]

# Loop!
for file in files:
    # Load image
    img=plt.imread(file,0)

    # Start from top down
    found_white_row = True
    row = 0
    while found_white_row:
        check = True
        for layer in range(img.shape[2]):
            check = ((img[row,:,layer] == 255).all()) & check
        if check:
            row += 1
            if row > img.shape[0]:
                found_white_row = False
        else: 
            found_white_row = False

    img = img[row:,:,:]

    # Start from bottom up
    found_white_row = True
    row = img.shape[0]-1
    while found_white_row:
        check = True
        for layer in range(img.shape[2]):
            check = ((img[row,:,layer] == 255).all()) & check
        if check:
            row -= 1
            if row > img.shape[0]:
                found_white_row = False
        else: 
            found_white_row = False
            row -= 1

    img = img[:row,:,:]

    # Start from left to right
    found_white_row = True
    col = 0
    while found_white_row:
        check = True
        for layer in range(img.shape[2]):
            check = ((img[:,col,layer] == 255).all()) & check
        if check:
            col += 1
            if col > img.shape[1]:
                found_white_row = False
        else: 
            found_white_row = False

    img = img[:,col:,:]

    # Start from right to left
    found_white_row = True
    col = img.shape[1]-1
    while found_white_row:
        check = True
        for layer in range(img.shape[2]):
            check = ((img[:,col,layer] == 255).all()) & check
        if check:
            col -= 1
            if col > img.shape[1]:
                found_white_row = False
        else: 
            found_white_row = False
            col -= 1

    img = img[:,:col,:]

    # Save image
    plt.imsave(file.replace('logo.png','logo_cropped.png'),img)

推荐阅读