Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the opencv of python to remove the white edge of a picture

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use python's opencv to remove the white edge of a picture". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use python's opencv to remove the white edge of a picture".

This article example for you to share the python use opencv to cut the white edge of the picture of the specific code, can be crosscutting and vertical cutting, for your reference, the specific content is as follows

If you don't talk much nonsense, you can go straight to the code, and sharing makes people make progress:

From PIL import Imagefrom itertools import groupbyimport cv2import datetimeimport os # from core.rabbitmq import MessageQueue THRESHOLD_VALUE = 2302 threshold PRETREATMENT_FILE = 'hq' # temporary saved folder W = 540 # minimum width H = 960 # minimum height class Pretreatment (object): _ _ doc__ = "picture horizontal cutting" 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 ('/' ) def get_continuity_position_new (self): img = cv2.imread (self.path) gray_image = cv2.cvtColor (img) Cv2.COLOR_BGR2GRAY) ret, thresh2 = 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 roomH2 [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, thresh2 = 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 thresh2[:, 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)) 原始图片: 结果:

Thank you for your reading, the above is the content of "how to use python's opencv to remove the white edge of a picture". After the study of this article, I believe you have a deeper understanding of how to use python's opencv to remove the white edge of a picture. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report