在大图像周围裁剪空白

时间:2018-11-06 20:54:41

标签: image whitespace crop

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

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

2 个答案:

答案 0 :(得分:0)

我在R中找到了解决问题的方法。我在plot / image函数之前添加了par(mar = c(0,0,0,0))这行代码,它起到了作用,并生成了没有任何边距的图像为了我。 这可能不是我在这里要问的,但比我在完成图像后裁剪图像周围的空白时要好。这是一个简单的解决方案,在绘制绘图之前消除了白色边距的问题。 谢谢大家的建议和帮助。如果您不同意此解决方案,请告诉我,我们将很乐意为您提供帮助。

答案 1 :(得分:0)

有点晚了,但我只是写了这张图片裁剪器:

# 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)