from numbers import Integral
import cv2
import math
import numpy
import numpy as np
from PIL import Image
import os
#####################################################################
### numpy 补0 操作
###
#####################################################################
def paddingImage (image,path_save,imagename):
image_arr = numpy.array(image)
print(image_arr.shape)
if(image_arr.ndim==3):
row_actual,col_actual,channel=image_arr.shape
row_expect = 3000 - row_actual
col_expect = 3000 - col_actual
if (row_actual > col_actual):
row_expect = 0
else:
col_expect = 0
if (row_expect % 2 and col_expect % 2):
image_arr = np.pad(image_arr,
((row_expect // 2, row_expect // 2 + 1), (col_expect // 2, col_expect // 2 + 1), (0, 0)),
'maximum')
elif (row_expect % 2):
image_arr = np.pad(image_arr,
((row_expect // 2, row_expect // 2 + 1), (col_expect // 2, col_expect // 2), (0, 0)),
'maximum')
elif (col_expect % 2):
image_arr = np.pad(image_arr,
((row_expect // 2, row_expect // 2), (col_expect // 2, col_expect // 2 + 1), (0, 0)),
'maximum')
elif (col_expect % 2 == 0 or row_expect % 2 == 0):
image_arr = np.pad(image_arr,
((row_expect // 2, row_expect // 2), (col_expect // 2, col_expect // 2), (0, 0)),
'maximum')
image = Image.fromarray(image_arr)
# image.show()
print(image)
if (image.size != (3000, 3000)):
exit(1)
image.save(path_save + imagename + ".jpg")
return
else:
row_actual, col_actual = image_arr.shape
row_expect = 3000-row_actual
col_expect = 3000-col_actual
if (row_actual>col_actual):
row_expect = 0
else:
col_expect = 0
if(row_expect%2 and col_expect%2):
image_arr = np.pad(image_arr,((row_expect//2, row_expect//2+1), (col_expect//2,col_expect//2+1)),'maximum')
elif(row_expect%2):
image_arr=np.pad(image_arr,((row_expect//2,row_expect//2+1),(col_expect//2,col_expect//2)),'maximum')
elif(col_expect%2):
image_arr=np.pad(image_arr,((row_expect//2,row_expect//2),(col_expect//2,col_expect//2+1)),'maximum')
elif(col_expect%2 ==0 or row_expect%2==0 ):
image_arr = np.pad(image_arr, ((row_expect//2,row_expect//2),(col_expect//2,col_expect//2)),'maximum')
image = Image.fromarray(image_arr)
#image.show()
print(image)
if(image.size!=(3000,3000)):
exit(1)
image.save(path_save + imagename + ".jpg")
return
#######################################################################
#######################################################################
image_file_path = "C:/Users/Yuki/Desktop/workstation/humanparsing/segmentation/"
path_save = "C:/Users/Yuki/Desktop/workstation/humanparsing/Convert_images/"
images=os.listdir(image_file_path)
for image in images:
imagename = image.split('.')[0]
im=Image.open(image_file_path+image)
if(im.size[0]<=3000 and im.size[2]<=3000):
if (im.size[1] >= im.size[0]):
s = 3000 // im.size[1]
im = im.resize((im.size[0] * s, 3000))
else:
s = 3000 // im.size[0]
im = im.resize((3000, im.size[1] * s))
if (im.size[1] != 3000 or im.size[0] != 3000):
print(imagename)
paddingImage(im, path_save=path_save, imagename=imagename)
else:
paddingImage(im, path_save=path_save, imagename=imagename)
else:
if (im.size[1] >= im.size[0]):
s = im.size[1]//3000
s=math.floor(s)
im = im.resize((im.size[0] * s, 3000))
else:
s = im.size[0] // 3000
s = math.floor(s)
im = im.resize((3000, im.size[1] * s))
if (im.size[1] != 3000 or im.size[0] != 3000):
print(imagename)
paddingImage(im, path_save=path_save, imagename=imagename)
else:
paddingImage(im, path_save=path_save, imagename=imagename)