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. 洛谷P1966 火柴排队(逆序对)

    题意 题目链接 Sol 不算很难的一道题 首先要保证权值最小,不难想到一种贪心策略,即把两个序列中rank相同的数放到同一个位置 证明也比较trivial.假设\(A\)中有两个元素\(a, b\), ...

  2. JDBC实现动态查询

    一 概述 1.什么是动态查询? 从多个查询条件中随机选择若干个组合成一个DQL语句进行查询,这一过程叫做动态查询. 2.动态查询的难点 可供选择的查询条件多,组合情况多,难以一一列举. 3.最终查询语 ...

  3. MySQL远程连接:Host 'x' is not allowed to connect to this MySQL server

    远程连接MySQL时发现如下错误: java.sql.SQLException: null, message from server: "Host '192.168.30.23' is no ...

  4. Linux漏洞分析入门笔记-CVE-2015-0235

    Ubuntu 12.04 32位 ida 7.0 0x00:漏洞描述 1.glibc的__nss_hostname_digits_dots存在缓冲区溢出漏洞,导致使用gethostbyname系列函数 ...

  5. linux C之判断文件或目录是否存在 access函数

    http://blog.sina.com.cn/s/blog_6a1837e90100uh5d.html access():判断是否具有存取文件的权限 相关函数    stat,open,chmod, ...

  6. Matlab GUI保存图片

    % --- Executes on button press in pushbutton5. function pushbutton5_Callback(hObject, eventdata, han ...

  7. 单例模式(C++)

    #include <iostream> #include <string> using namespace std; class singleton { private: si ...

  8. [转载]AMOLED结构详解,BOE专家给你分析驱动补偿

    关键词: AMOLED, 驱动补偿 有机发光显示二极管(OLED)作为一种电流型发光器件已越来越多地被应用于高性能显示中.由于它自发光的特性,与LCD相比,AMOLED具有高对比度.超轻薄.可弯曲等诸 ...

  9. MySQL Database on Azure 支持 5.7 版本啦!

    MySQL Database on Azure 目前已经全面开放对 5.7 的支持.您可以通过管理门户,在 MySQL 数据库服务器创建时选择 5.7 版本进行体验.MySQL 5.7 版本目前是 M ...

  10. SpringBoot热部署插件

    1.配置在 maven工程中的pom.xml文件中 2.SpringBoot框架中提供的一个热部署插件,利用该热部署插件,我们可以在修改代码后不用重启应用,大大提高开发效率: