padding_height
=
math
.
ceil
(
height
/
DES_HEIGHT
)
*
DES_HEIGHT
padding_width
=
math
.
ceil
(
width
/
DES_WIDTH
)
*
DES_WIDTH
padding_img
=
np
.
random
.
randint
(
0
,
255
,
size
=
(
padding_height
,
padding_width
,
3
)
)
.
astype
(
np
.
uint8
)
padding_img
[
0
:
height
+
0
,
0
:
width
+
0
]
=
src
img
=
padding_img
sum_rows
=
img
.
shape
[
0
]
sum_cols
=
img
.
shape
[
1
]
cols
=
DES_WIDTH
rows
=
DES_HEIGHT
save_path
=
"crop{0}_{1}\\"
.
format
(
cols
,
rows
)
if
not
os
.
path
.
exists
(
save_path
)
:
os
.
makedirs
(
save_path
)
setDir
(
save_path
)
print
(
"裁剪所得{0}列图片,{1}行图片."
.
format
(
int
(
sum_cols
/
cols
)
,
int
(
sum_rows
/
rows
)
)
)
filename
=
os
.
path
.
split
(
path_img
)
[
1
]
for
i
in
range
(
int
(
sum_cols
/
cols
)
)
:
for
j
in
range
(
int
(
sum_rows
/
rows
)
)
:
cv2
.
imwrite
(
save_path
+
os
.
path
.
splitext
(
filename
)
[
0
]
+
'_'
+
str
(
j
)
+
'_'
+
str
(
i
)
+
os
.
path
.
splitext
(
filename
)
[
1
]
,
img
[
j
*
rows
:
(
j
+
1
)
*
rows
,
i
*
cols
:
(
i
+
1
)
*
cols
,
:
]
)
print
(
"裁剪完成,得到{0}张图片."
.
format
(
int
(
sum_cols
/
cols
)
*
int
(
sum_rows
/
rows
)
)
)
print
(
"文件保存在{0}"
.
format
(
save_path
)
)
def merge_picture(merge_path,num_of_cols,num_of_rows):
filename=os.listdir(merge_path)
full_path=os.path.join(merge_path,filename[0])
shape=cv2.imread(full_path).shape
cols=shape[1]
rows=shape[0]
channels=shape[2]
dst=np.zeros((rows*num_of_rows,cols*num_of_cols,channels),np.uint8)
for i in range(len(filename)):
full_path=os.path.join(merge_path,filename[i])
img=cv2.imread(full_path,-1)
cols_th=int(full_path.split("_")[-1].split('.')[0])
rows_th=int(full_path.split("_")[-2])
roi=img[0:rows,0:cols,:]
dst[rows_th*rows:(rows_th+1)*rows,cols_th*cols:(cols_th+1)*cols,:]=roi
cv2.imwrite(merge_path+"merge.jpg",dst)