image - 裁剪大图像周围的空白区域
问题描述
我正在尝试裁剪大图像周围的空白区域。2000 像素 X 32000 像素。我试过 convert trim 但失败了,也试过 mogrify 也失败了。我尝试安装带有其他错误和警告的 R magick 库。此外,我尝试了 PIL,但它没有安装在我的 Python 2.7 上。无论如何,有一个简单的解决方案可以裁剪我的图像周围的空白区域。任何帮助都深表感谢。我有 100 个这样的图像要修剪。这是我的转换命令:
convert A1.png -fuzz 0% -trim +repage A_N.png'
解决方案
有点晚了,但我刚刚写了这个图像裁剪器:
# 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)
推荐阅读
- android - Exoplayer 清除所有媒体项,除了正在播放的媒体项
- vulkan - Vulkan:如何将图像从 GPU 下载到 RAM
- javascript - 如何通过 ajax 正确地将数据通过 php 发送到电报?
- azure-web-app-service - 如何通过 Terraform 代码向 Microsoft 标识平台注册应用程序
- google-chrome - Web 应用程序 302 从嵌入式 iframe 重定向 Chrome 90 中的 HTTP 请求
- reactjs - AWS IVS 流式传输 流式传输关闭时脚本标签的错误处理
- loadrunner - 在使用 citrix 协议的 loadRunner 多用户期间出现错误
- python - 如何摆脱 phycharm 软件中无法访问的问题
- vue.js - 仅在Vuejs中满足条件时如何在选择上调用事件
- android - 获取变换纹理视图的位图