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. String StringBuffer StringBuilder对比

    1.相同点 三者都可以用来存储字符串类型数据. 2.不同点 String类型对象内容不可变,每变化一次都会创建一个新的对象. StringBuiler与StringBuffer的内容与长度均可以发生变 ...

  2. Java设计模式—门面模式(带案例分析)

    1.门面模式的定义: 门面模式(Facade Pattern)也叫做外观模式,是一种比较常用的封装模式,其定义如下:       要求一个子系统的外部与其内部的通信必须通过一个统一的对象进行.门面模式 ...

  3. Android StickHeaderRecyclerView - 让recyclerview头部固定

    介绍在项目中有时会需要recyclerview滑动式时某个view滑出后会固定在头部显示,比较常用的比如手机联系人界面.地区选择界面等. StickHeaderRecyclerView就是实现这个功能 ...

  4. 《ArcGIS Runtime SDK for Android开发笔记》——问题集:如何解决ArcGIS Runtime SDK for Android中文标注无法显示的问题(转载)

    Geodatabase中中文标注编码乱码一直是一个比较头疼的问题之前也不知道问题出在哪里?在百度后发现园子里的zssai已经对这个问题原因做了一个详细说明.这里将原文引用如下: 说明:此文转载自htt ...

  5. 安全隐患,你对X-XSS-Protection头部字段理解可能有误

    0×00. 引言 我曾做过一个调查,看看网友们对关于X-XSS-Protection 字段的设置中,哪一个设置是最差的,调查结果令我非常吃惊,故有此文. 网友们认为 最差的配置是X-XSS-Prote ...

  6. 学习和运用scrum

    作为长大的大三老腊肉,我们已经在长大生活了两年多,对于什么是长大人最想完善的校园需求.最想拥有的校园服务媒介也有了更加深切的体会. 于是,GoodJob小团队blingbling闪现啦!! GoodJ ...

  7. NO.013-2018.02.18《鹊桥仙·纤云弄巧》宋代:秦观

    鹊桥仙·纤云弄巧_古诗文网 鹊桥仙·纤云弄巧 宋代:秦观 纤云弄巧,飞星传恨,银汉迢迢暗度.金风玉露一相逢,便胜却人间无数.(度 通:渡)纤薄的云彩在天空中变幻多端,天上的流星传递着相思的愁怨,遥远无 ...

  8. LeetCodeOJ刷题之12【Integer to Roman】

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  9. bootstrap table通过ajax获取后台数据展示在table

    1. 背景 bootstrap table 默认向后台发送语法的dataType为 json,但是为了解决跨域问题我们需要将dataType改为jsonp,这时就需要修改bootstrap table ...

  10. datatable Left and right fixed columns

    $(document).ready(function() { var table = $('#example').DataTable( { scrollY: "300px", sc ...