fix split width height

This commit is contained in:
caiwx86 2022-12-06 20:10:06 +08:00
parent b2268dbb06
commit 7c8d546a25

View File

@ -62,28 +62,25 @@ class imageUtils:
def splitimage(cls,src,rownum,colnum,dstpath): def splitimage(cls,src,rownum,colnum,dstpath):
img=Image.open(src) img=Image.open(src)
w,h=img.size w,h=img.size
if rownum<= h and colnum<=w: s=os.path.split(src)
s=os.path.split(src) if dstpath=='':
if dstpath=='': dstpath = s[0]
dstpath = s[0] if not os.path.exists(dstpath):
if not os.path.exists(dstpath): os.makedirs(dstpath)
os.makedirs(dstpath) fn=s[1].split('.')
fn=s[1].split('.') basename=fn[0]
basename=fn[0] ext=fn[-1]
ext=fn[-1] num=0
num=0 rowheight=h//rownum
rowheight=h//rownum colwidth=w//colnum
colwidth=w//colnum for r in range(rownum):
for r in range(rownum): for c in range(colnum):
for c in range(colnum): box=(c*colwidth,r*rowheight,(c+1)*colwidth,(r+1)*rowheight)
box=(c*colwidth,r*rowheight,(c+1)*colwidth,(r+1)*rowheight) count_image = "{:0>3d}".format(num)
count_image = "{:0>3d}".format(num) file_path = os.path.join(dstpath,str(count_image)+'.'+ext)
file_path = os.path.join(dstpath,str(count_image)+'.'+ext) print("file_path=",file_path)
print("file_path=",file_path) img.crop(box).save(file_path)
img.crop(box).save(file_path) num=num+1
num=num+1
else:
print('不数!')
img.close img.close
return src return src