欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python使用opencv切割图片白边

程序员文章站 2022-06-25 21:36:19
本文实例为大家分享了python使用opencv切割图片白边的具体代码,可以横切和竖切,供大家参考,具体内容如下废话不多说直接上码,分享使人进步:from pil import imagefrom i...

本文实例为大家分享了python使用opencv切割图片白边的具体代码,可以横切和竖切,供大家参考,具体内容如下

废话不多说直接上码,分享使人进步:

from pil import image
from itertools import groupby
import cv2
import datetime
import os
 
# from core.rabbitmq import messagequeue
 
threshold_value = 230  # 二值化时的阈值
pretreatment_file = 'hq'  # 横切时临时保存的文件夹
w = 540  # 最小宽度
h = 960  # 最小高度
 
 
class pretreatment(object):
    __doc__ = "图片横向切割"
 
    def __init__(self, path, save_path, min_size=960):
        self.x = 0
        self.y = 0
        self.img_section = []
        self.continuity_position = []
        self.path = path
        self.save_path = save_path
        self.img_obj = none
        self.min_size = min_size
        self.mkdir(self.save_path)
        self.file_name = self.path.split('/')[-1]
 
    def get_continuity_position_new(self):
        img = cv2.imread(self.path)
        gray_image = cv2.cvtcolor(img, cv2.color_bgr2gray)
        ret, thresh1 = cv2.threshold(gray_image, threshold_value, 255, cv2.thresh_binary)
 
        width = img.shape[1]
        height = img.shape[0]
        self.x = width
        self.y = height
        for i in range(0, height):
            if thresh1[i].sum() != 255 * width:
                self.continuity_position.append(i)
 
    def filter_rule(self):
        if self.y < self.min_size:
            return true
 
    def mkdir(self, path):
        if not os.path.exists(path):
            os.makedirs(path)
 
    def get_section(self):
        # 获取区间
        for k, g in groupby(enumerate(self.continuity_position), lambda x: x[1] - x[0]):
            l1 = [j for i, j in g]  # 连续数字的列表
            if len(l1) > 1:
                self.img_section.append([min(l1), max(l1)])
 
    def split_img(self):
        print(self.img_section)
        for k, s in enumerate(self.img_section):
            if s:
                if not self.img_obj:
                    self.img_obj = image.open(self.path)
 
                if self.x < w:
                    return
                if s[1] - s[0] < h:
                    return
                cropped = self.img_obj.crop((0, s[0], self.x, s[1]))  # (left, upper, right, lower)
                self.mkdir(os.path.join(self.save_path, pretreatment_file))
                cropped.save(os.path.join(self.save_path, pretreatment_file, f"hq_{k}_{self.file_name}"))
 
    def remove_raw_data(self):
        os.remove(self.path)
 
    def main(self):
        # v2
        try:
            self.get_continuity_position_new()
            self.filter_rule()
            self.get_section()
            self.split_img()
        except exception as e:
            print(self.file_name)
            print(e)
        finally:
            if self.img_obj:
                self.img_obj.close()
 
 
class longitudinal(pretreatment):
    def get_continuity_position_new(self):
        print(self.path)
        img = cv2.imread(self.path)
        gray_image = cv2.cvtcolor(img, cv2.color_bgr2gray)
        ret, thresh1 = cv2.threshold(gray_image, threshold_value, 255, cv2.thresh_binary)
 
        width = img.shape[1]
        height = img.shape[0]
        print(width, height)
        self.x = width
        self.y = height
        for i in range(0, width):
            if thresh1[:, i].sum() != 255 * height:
                self.continuity_position.append(i)
 
    def split_img(self):
        print(self.img_section)
        for k, s in enumerate(self.img_section):
            if s:
                if not self.img_obj:
                    self.img_obj = image.open(self.path)
                if self.y < h:
                    return
                if s[1] - s[0] < w:
                    return
                cropped = self.img_obj.crop((s[0], 0, s[1], self.y))  # (left, upper, right, lower)
                cropped.save(os.path.join(self.save_path, f"{k}_{self.file_name}"))
 
 
def main(path, save_path):
    starttime = datetime.datetime.now()
    a = pretreatment(path=path, save_path=save_path)
    a.main()
    for root, dirs, files in os.walk(os.path.join(save_path, pretreatment_file)):
        for i in files:
            b = longitudinal(path=os.path.join(save_path, pretreatment_file, i), save_path=save_path)
            b.main()
            os.remove(os.path.join(save_path, pretreatment_file, i))
    endtime = datetime.datetime.now()
    print(f'耗时:{(endtime - starttime)}')
 
 
if __name__ == '__main__':
    path = '你图片存放的路径'
    save_path = '要保存的路径'
    for _, _, files in os.walk(path):
        for i in files:
            main(path=os.path.join(path, i), save_path=save_path)
    os.rmdir(os.path.join(save_path, pretreatment_file))

原始图片:

python使用opencv切割图片白边

结果:

python使用opencv切割图片白边

python使用opencv切割图片白边

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: python 切割图片