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. ASTreeView Demo:Add, Edit & Delete nodes

    http://www.jinweijie.com/ http://www.astreeview.com/astreeviewdemo/astreeviewdemo1.aspx 選擇節點: <sc ...

  2. Swiper双向轮播

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  3. 一個新的面試題目,leetcode上面可以找到shortest palindrome

    記錄一下新的面試題目,其實題目是舊的,只是我才見到.以前研究過,只不過以前的解法不容易理解,現在有了新的遞歸解法.記錄一下. public String shortestPalindrome(Stri ...

  4. web开发路径问题解决

     使用监听器解决路径问题 监听器:

  5. May 27th 2017 Week 21st Saturday

    I learned the value of hard work by working hard. 只有真的努力了,才会知道努力的价值. I remember in the movie, The Da ...

  6. Docker镜像提交命令commit的工作原理和使用方法

    在本地创建一个容器后,可以依据这个容器创建本地镜像,并可把这个镜像推送到Docker hub中,以便在网络上下载使用. 下面我们来动手实践. docker pull nginx:1.15.3 用命令行 ...

  7. PHP设计模式——工厂模式

    <?php /** * 工厂模式 * 提供获取某个对象的新实例的一个接口,同时使调用代码避免确定实际实例化基类的步骤. * * 工厂类用于创建不同类的实例,并将其返回. */ /** * 服务端 ...

  8. listBox获取项的方法

    获取所有项 ; i < LB.Items.Count;i++ )2 {3 str_arr.Add(LB.Items[i].ToString()); 4 } 获取指定项 string str=LB ...

  9. 一步一步部署SSIS包图解教程

    本文就SQL统计分析SSIS包的部署进行一次详细的部署图解教程,Sql Server Integration Services 提供了非常简单的部署工具,利用这些工具可以方便地将包文件(*.dtsx) ...

  10. tracking

    https://reid-mct.github.io/   1st Workshop on Target Re-Identification and Multi-Target Multi-Camera ...