python使用opencv切割圖片白邊
本文實(shí)例為大家分享了python使用opencv切割圖片白邊的具體代碼,可以橫切和豎切,供大家參考,具體內(nèi)容如下
廢話(huà)不多說(shuō)直接上碼,分享使人進(jìn)步:
from PIL import Image from itertools import groupby import cv2 import datetime import os # from core.rabbitmq import MessageQueue THRESHOLD_VALUE = 230 # 二值化時(shí)的閾值 PRETREATMENT_FILE = 'hq' # 橫切時(shí)臨時(shí)保存的文件夾 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): # 獲取區(qū)間 for k, g in groupby(enumerate(self.continuity_position), lambda x: x[1] - x[0]): l1 = [j for i, j in g] # 連續(xù)數(shù)字的列表 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'耗時(shí):{(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))
原始圖片:
結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一文講解如何查看python腳本所依賴(lài)三方包及其版本
Python因?yàn)榫哂谐嗟牡谌綆?kù)而被大家喜歡,下面這篇文章主要給大家介紹了關(guān)于如何查看python腳本所依賴(lài)三方包及其版本的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03Python中不同類(lèi)之間調(diào)用方法的四種方式小結(jié)
類(lèi)是一種面向?qū)ο蟮木幊谭妒?它允許我們將數(shù)據(jù)和功能封裝在一個(gè)實(shí)體中,本文主要介紹了Python中不同類(lèi)之間調(diào)用方法的四種方式小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02利用python在Word文檔中創(chuàng)建和執(zhí)行條件郵件合并
郵件合并域和IF域是Word文檔中兩種非常實(shí)用的域,前者可以用來(lái)進(jìn)行郵件合并,根據(jù)數(shù)據(jù)批量創(chuàng)建定制的Word文檔,本文講介紹如何使用Python在Word文檔中創(chuàng)建條件郵件合并域以及執(zhí)行條件郵件合并,需要的朋友可以參考下2024-08-08tensorflow2.0如何實(shí)現(xiàn)cnn的圖像識(shí)別
這篇文章主要介紹了tensorflow2.0如何實(shí)現(xiàn)cnn的圖像識(shí)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12python3使用迭代生成器實(shí)現(xiàn)減少內(nèi)存占用
這篇文章主要介紹了python3使用迭代生成器實(shí)現(xiàn)減少內(nèi)存占用的相關(guān)資料,需要的朋友可以參考下2021-05-05對(duì)python3中的RE(正則表達(dá)式)-詳細(xì)總結(jié)
今天小編就為大家分享一篇對(duì)python3中的RE(正則表達(dá)式)-詳細(xì)總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07Python MongoDB 插入數(shù)據(jù)時(shí)已存在則不執(zhí)行,不存在則插入的解決方法
這篇文章主要介紹了Python MongoDB 插入數(shù)據(jù)時(shí)已存在則不執(zhí)行,不存在則插入的解決方法,結(jié)合實(shí)例形式分析了Python基于日志判斷數(shù)據(jù)是否已經(jīng)插入的相關(guān)操作技巧,需要的朋友可以參考下2019-09-09Python Flask全棧項(xiàng)目實(shí)戰(zhàn)構(gòu)建在線(xiàn)書(shū)店流程
這篇文章主要為大家介紹了Python Flask全流程全棧項(xiàng)目實(shí)戰(zhàn)之在線(xiàn)書(shū)店構(gòu)建實(shí)現(xiàn)過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11