如何在带有python3的文件夹中在白色背景上裁剪所有TIFF图像



我正在扫描一本书,现在我已进入最后一步。我需要在没有保存比例的情况下,通过白色背景裁剪所有内容居中的方形图像。有些库不支持TIFF格式。

我该怎么做?

我结合了《如何用python在白色背景上裁剪图像?》中的一些例子?,如何用python在白色背景上裁剪图像?以及其他问题。它工作

from PIL import Image
from skimage import io, img_as_float
import sys
import numpy as np
import glob
import matplotlib.pyplot as plt
filePaths = glob.glob("*.tif")
for filePath in filePaths:
image = img_as_float(io.imread(filePath))

white = np.array([1, 1, 1, 1])
mask = np.abs(image - white).sum(axis=2) < 0.05
coords = np.array(np.nonzero(~mask))
top_left = np.min(coords, axis=1)
bottom_right = np.max(coords, axis=1)
out = image[top_left[0]:bottom_right[0],
top_left[1]:bottom_right[1]]
plt.imshow(out)
# plt.show()

fig = plt.gcf()
for ax in fig.axes:
ax.axis('off')
ax.margins(0,0)
ax.xaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_major_locator(plt.NullLocator())
fig.savefig(filePath + ".png", dpi=1200, bbox_inches="tight", pad_inches=0)

最新更新