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 cut the outer blank area of graphics in batch by Python

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces Python how to batch cut graphics outside the blank area of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this Python how to batch cut graphics peripheral blank areas of the article will have a harvest, let's take a look.

1. Basic description

The background area of the picture is cut out in batches, usually with a white background, so as to reduce the interference of the background value and reduce the storage space.

By retrieving the minimum crop region coordinate values of all pictures, and then cropping the picture. The pictures in this paper are all standardized, and the core pictures have the same content scale, so using this method, if there are many pictures of different sizes, that is, the size and shape of the content area in the picture are different, then retrieve the background area of the picture one by one, and then cut it. That is, the clipping area is retrieved and cropped one by one.

Second, implementation code

Modify the code in the original text, retrieve the crop region coordinates of each picture one by one, and then cut it.

The code is as follows:

From PIL import Imageimport numpy as npimport os imagesDirectory = r "C:\ Users\ Administrator\ Desktop\ out" # tiff picture folder path I = 0for imageName in os.listdir (imagesDirectory): imagePath = os.path.join (imagesDirectory, imageName) image = Image.open (imagePath) # Open tiff image ImageArray = np.array (image) row = ImageArray.shape [0] col = ImageArray.shape [1] print (row Col) # calculate the cropping range of all pictures first Then uniformly crop and output the picture x_left = row x_top = col x_right = 0 x_bottom = 0 # upper and lower left and right range "" Image.crop (left, up, right, below) left: the distance from the left boundary up: the distance from the upper boundary right: the distance from the left boundary below: or the distance from the upper boundary, in short, the upper left, upper right and lower right. "" I + = 1 for r in range (row): for c in range (col): # if ImageArray [row] [col] [0]

< 255 or ImageArray[row][col][0] ==0: if ImageArray[r][c][0] < 255 and ImageArray[r][c][0] !=0: #外框有个黑色边框,增加条件判断 if x_top >

R: x_top = r # get the minimum x_top if x_bottom

< r: x_bottom = r # 获取最大x_bottom if x_left >

C: x_left = c # get the minimum x_left if x_right

< c: x_right = c # 获取最大x_right print(x_left, x_top, x_right, x_bottom) # image = Image.open(imagePath) # 打开tiff图像 cropped = image.crop((x_left-5, x_top-5, x_right+5, x_bottom+5)) # (left, upper, right, lower) cropped.save(r"C:\Users\Administrator\Desktop\out_cut_bg\{}.png".format(imageName[:-4], i)) print("imageName completed!")三、效果 原图显示: 裁剪结果显示:

Original effect:

This is the end of the article on "how to cut the white space around the graphics in batch by Python". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to cut the outer blank area of graphics in batch by Python". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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