numpy.pad() 用最大值操作补齐图象制作标签 标签大小为3000*3000像素

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值