import os
import cv2
import numpy as np
from scipy.stats import mode
import time
import concurrent.futures '''
multi-process to crop pictures.
''' def crop(file_path_list):
origin_path, save_path = file_path_list
img = cv2.imread(origin_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) closed_1 = cv2.erode(gray, None, iterations=4)
closed_1 = cv2.dilate(closed_1, None, iterations=4)
blurred = cv2.blur(closed_1, (9, 9))
# get the most frequent pixel
num = mode(blurred.flat)[0][0] + 1
# the threshold depends on the mode of your images' pixels
num = num if num <= 30 else 1 _, thresh = cv2.threshold(blurred, num, 255, cv2.THRESH_BINARY) # you can control the size of kernel according your need.
kernel = np.ones((13, 13), np.uint8)
closed_2 = cv2.erode(thresh, kernel, iterations=4)
closed_2 = cv2.dilate(closed_2, kernel, iterations=4) _, cnts, _ = cv2.findContours(closed_2.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
c = sorted(cnts, key=cv2.contourArea, reverse=True)[0] # compute the rotated bounding box of the largest contour
rect = cv2.minAreaRect(c)
box = np.int0(cv2.boxPoints(rect)) # draw a bounding box arounded the detected barcode and display the image
# cv2.drawContours(img, [box], -1, (0, 255, 0), 3)
# cv2.imshow("Image", img)
# cv2.imwrite("pic.jpg", img)
# cv2.waitKey(0) xs = [i[0] for i in box]
ys = [i[1] for i in box]
x1 = min(xs)
x2 = max(xs)
y1 = min(ys)
y2 = max(ys)
height = y2 - y1
width = x2 - x1
crop_img = img[y1:y1 + height, x1:x1 + width]
cv2.imwrite(save_path, crop_img)
# cv2.imshow("Image", crop_img)
# cv2.waitKey(0)
print(f'the {origin_path} finish crop, most frequent pixel is {num}') def multi_process_crop(input_dir):
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(crop, input_dir) if __name__ == "__main__":
data_dir = ''
save_dir = ''
path_list = [(os.path.join(data_dir, o), os.path.join(save_dir, o)) for o in os.listdir(data_dir)]
start = time.time()
multi_process_crop(path_list)
print(f'Total cost {time.time()-start} seconds')

利用python和opencv批量去掉图片黑边的更多相关文章

  1. Python cv2 OpenCV 中传统图片格式与 base64 转换

    Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,是一种基于64个可打印字符来表示二进制数据的方法.通过http传输图片常常将图片数据转换成base64之后再进行传输. Base64简 ...

  2. 10 行 Python 代码,批量压缩图片 500 张,简直太强大了

    本文原创并首发于公众号[Python猫],未经授权,请勿转载. 原文地址:https://mp.weixin.qq.com/s/5hpFDgjCpfb0O1Jg-ycACw 熟悉 "Pyth ...

  3. 【python】PIL 批量绘制图片矩形框工具

    工具采用PIL:Python Imaging Library,图像处理标准库.PIL功能非常强大,但API却非常简单易用. 安装PIL 在Debian/Ubuntu Linux下直接通过apt安装 $ ...

  4. 利用Python实现简单的相似图片搜索的教程

    大概五年前吧,我那时还在为一家约会网站做开发工作.他们是早期创业公司,但他们也开始拥有了一些稳定用户量.不像其他约会网站,这家公司向来以洁身自好为主要市场形象.它不是一个供你鬼混的网站——是让你能找到 ...

  5. 利用python爬虫关键词批量下载高清大图

    前言 在上一篇写文章没高质量配图?python爬虫绕过限制一键搜索下载图虫创意图片!中,我们在未登录的情况下实现了图虫创意无水印高清小图的批量下载.虽然小图能够在一些移动端可能展示的还行,但是放到pc ...

  6. 手把手教你利用Python自动下载CL社区图片

    需求描述:     最近发现CL社区上好多精华的帖子分享的图片非常棒,好想好想保存下来,但是一张一张地保存太费时间了,因此,造物者思想主义的我就想动手写个工具,实现只要输入帖子的链接,就能把所有的精华 ...

  7. opencv批量读取图片

    #include<opencv2/opencv.hpp>using namespace cv;using namespace std;int main(){    int num=4;// ...

  8. 利用Python对文件批量重命名

    由于要用到对无序的文件进行重命名,以下写了一个脚本,进行批量进行重命名. 基本格式是  i.后缀名 ( i  循环条件下的数 ) 1 #coding:utf8 2 import os; 3 4 def ...

  9. opencv批量修改图片尺寸

    #include"opencv2/opencv.hpp" using namespace std; using namespace cv; #include<opencv2/ ...

随机推荐

  1. File GDB API

    bbs一问一答 https://blogs.esri.com/esri/arcgis/2010/12/13/file-geodatabase-api-details/ 下载页面 http://www. ...

  2. FragmentActivity的简单使用

    如图是效果图 当  点击下面 不同 的按钮 进入 不同的界面 其中 要一个 主布局当做容器 , 和3个不同的 布局来对应下面的3个按钮界面 主程序的 代码和布局如下 import android.su ...

  3. python3的学习经验

    网上资料非常多,颇有些“乱花渐欲迷人眼”的意味,个人看了不少,拖之前从事前端的福,发现廖雪峰大神的网站里有.学了2天之后发觉获益良多,网址:https://www.liaoxuefeng.com/wi ...

  4. ios端微信浏览器禁止上下滑动

    在body里的第一个div容器中添加 position:fixed

  5. sql developer中英文切换

    今天使用oracle sql developer时做调优建议时找到的建议显示为?的乱码,本人sql developer为中文版,修改为英文版后问题解决. 查看帮助菜单中的属性选项卡,user.lang ...

  6. 插上翅膀,让Excel飞起来——xlwings(二)

    在上一篇插上翅膀,让Excel飞起来——xlwings(一)中提到利用xlwings模块,用python操作Excel有如下的优点: xlwings能够非常方便的读写Excel文件中的数据,并且能够进 ...

  7. SQA和测试规程

    SQA *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; ...

  8. 找出OData service出错根源的小技巧

    SAP的Fiori应用是通过OData和后台交互的.在使用Fiori应用时您可能会遇到这样的错误消息: 这个错误消息没有包含有助于partner或者客户定位问题根源的线索. 下面是如何在后台找出问题根 ...

  9. 面条代码 vs. 馄沌代码

    转载自:https://blog.csdn.net/godsme_yuan/article/details/6594013

  10. 【Unity3D】【NGUI】UIRect的Anchor的使用

    NGUI版本号:3.6.5 以以下的图,解释下主要的Anchors的使用(能够通过官方的Anchor和Chat样例进行深入学习) Target不是一定要是Sprite.能够是随意的UIRect(UIS ...