import cv2
import os
import numpy as np
def imgchuli(file, filetype, num):
data = np.fromfile(file, dtype=np.uint8) # 先用numpy把图片文件存入内存:data,把图片数据看做是纯字节数据
img = cv2.imdecode(data, cv2.IMREAD_COLOR) # 从内存数据读入图片
h = int(img.shape[0])
w = int(img.shape[1])
img1 = img[0:h, int(w / 2):]
img2 = img[0:h, 0:int(w / 2)]
cv2.imwrite('%03d' % num + '_01' + filetype, img1)
cv2.imwrite('%03d' % num + '_02' + filetype, img2)
def fenge(path_name):
filetype_list = ['.jpg', '.png', '.JPG']
if path_name == '':
pass
else:
os.chdir(path_name) # 转换路径
path = os.getcwd() # 对该路径下所有文件重命名排序
num = 1
file_list = os.listdir() # 该文件夹下所有的文件(包括文件夹)
for file in file_list: # 遍历所有文件
print(file)
if os.path.isdir(file): # 如果是文件夹则跳过
continue
olddir = os.path.join(path, file) # 原来的文件路径
filename = os.path.splitext(file)[0] # 文件名
filetype = os.path.splitext(file)[1] # 文件扩展名
if filetype in filetype_list:
imgchuli(olddir, filetype, num)
num += 1
os.remove(olddir)
if __name__ == '__main__':
path_name = 'D:\XXXXX\XXX\XXXX'
fenge(path_name)
漫画图片切割
最新推荐文章于 2024-08-05 23:13:57 发布