万维建设网站,网站维护升级,wordpress百家,定制开发一般多少钱前言从浏览器保存的图片有透明度#xff0c;但是python打开其透明通道是黑色的#xff0c;因此我利用python的OpenCV模块去除了其上下左右的黑边。效果展示计算机中效果python打开效果python裁剪后效果代码import cv2def change_size(read_file):image cv2.imread(read_file…前言从浏览器保存的图片有透明度但是python打开其透明通道是黑色的因此我利用python的OpenCV模块去除了其上下左右的黑边。效果展示计算机中效果python打开效果python裁剪后效果代码import cv2def change_size(read_file):image cv2.imread(read_file, 1) # 读取图片 image_name应该是变量img cv2.medianBlur(image, 5) # 中值滤波去除黑色边际中可能含有的噪声干扰b cv2.threshold(img, 15, 255, cv2.THRESH_BINARY) # 调整裁剪效果binary_image b[1] # 二值图--具有三通道binary_image cv2.cvtColor(binary_image, cv2.COLOR_BGR2GRAY)x binary_image.shape[0]print(高度x, x)y binary_image.shape[1]print(宽度y, y)edges_x []edges_y []for i in range(x):for j in range(y):if binary_image[i][j] 255:edges_x.append(i)edges_y.append(j)left min(edges_x) # 左边界right max(edges_x) # 右边界width right - left # 宽度bottom min(edges_y) # 底部top max(edges_y) # 顶部height top - bottom # 高度pre1_picture image[left:left width, bottom:bottom height] # 图片截取return pre1_picture # 返回图片数据if __name__ __main__:file_names template.pngtemplate change_size(file_names)cv2.imshow(template, template)cv2.waitKey(0)