1.输入一张图片转化成一张新图(只适用单通道图片)
def convert_image_size1 ( img, size) :
img = np. array( img)
height, width = img. shape[ 0 ] , img. shape[ 1 ]
ratio = width / height
mask_image = [ ]
channel = 1
if len ( img. shape) == 3 :
channel = 3
if width > height:
w = size[ 0 ]
h = int ( w / ratio)
img1 = cv2. resize( img, ( w, h) )
h1, w1 = img1. shape[ 0 ] , img1. shape[ 1 ]
if h1 % 2 == 0 :
c1 = int ( ( size[ 1 ] - h1) / 2 )
c2 = int ( ( size[ 1 ] - h1) / 2 )
mask1 = np. zeros( [ c1, w, channel] , dtype= np. uint8)
mask2 = np. zeros( [ c2, w, channel] , dtype= np. uint8)
img2 = np. concatenate( [ mask1, img1, mask2] , axis= 0 )
mask_image. append( img2)
elif h1 % 2 == 1 :
c1 = int ( ( ( size[ 1 ] - h1) / 2 ) + 1 )
c2 = int ( ( size[ 1 ] - h1) / 2 )
mask1 = np. zeros( [ c1, w, channel] , dtype= np. uint8)
mask2 = np. zeros( [ c2, w, channel] , dtype= np. uint8)
img2 = np. concatenate( [ mask1, img1, mask2] , axis= 0 )
mask_image. append( img2)
elif width < height:
h = size[ 1 ]
w = int ( h * ratio)
img1 = cv2. resize( img, ( w, h) )
h1, w1 = img1. shape
if w1 % 2 == 0 :
c1 = int ( ( size[ 0 ] - w1) / 2 )
c2 = int ( ( size[ 0 ] - w1) / 2 )
mask1 = np. zeros( [ h, c1, channel] , dtype= np. uint8)
mask2 = np. zeros( [ h, c2, channel] , dtype= np. uint8)
img2 = np. concatenate( [ mask1, img1, mask2] , axis= 1 )
mask_image. append( img2)
elif w1 % 2 == 1 :
c1 = int ( ( size[ 0 ] - w1) / 2 ) + 1
c2 = int ( ( size[ 0 ] - w1) / 2 )
print ( "c1c2:" , c1, c2)
mask1 = np. zeros( [ h, c1, channel] , dtype= np. uint8)
mask2 = np. zeros( [ h, c2, channel] , dtype= np. uint8)
img2 = np. concatenate( [ mask1, img1, mask2] , axis= 1 )
print ( ">>>>>>>>>" , img2. shape)
mask_image. append( img2)
else :
img1 = cv2. resize( img, ( size[ 0 ] , size[ 1 ] , channel) )
mask_image. append( img1)
return mask_image[ 0 ]
2.输入一张图片转化成一张新图(适用单通道和3通道图片)
def convert_image_size ( img, size) :
img= np. array( img)
new_shape = list ( img. shape)
new_shape[ 0 ] = size[ 0 ]
new_shape[ 1 ] = size[ 1 ]
mask_image = np. zeros( new_shape, dtype= np. uint8)
height, width= img. shape[ 0 ] , img. shape[ 1 ]
ratio = width / height
if width > height:
img2 = cv2. resize( img, ( size[ 0 ] , int ( size[ 0 ] / ratio) ) )
start= int ( ( size[ 1 ] - img2. shape[ 0 ] ) / 2 )
mask_image[ start: start+ img2. shape[ 0 ] , 0 : size[ 0 ] ] = img2
elif width < height:
img2 = cv2. resize( img, ( int ( size[ 1 ] * ratio) , size[ 1 ] ) )
start = int ( ( size[ 0 ] - img2. shape[ 1 ] ) / 2 )
mask_image[ 0 : size[ 1 ] , start: start+ img2. shape[ 1 ] ] = img2
else :
mask_image = cv2. resize( img, ( size[ 0 ] , size[ 1 ] ) )
return mask_image
3.案例
import os
from PIL import Image
import numpy as np
import cv2
def convert_image_size1 ( img, size) :
img = np. array( img)
height, width = img. shape
ratio = width / height
mask_image = [ ]
if width > height:
w = size[ 0 ]
h = int ( w / ratio)
img1 = cv2. resize( img, ( w, h) )
h1, w1 = img1. shape
c1 = int ( ( ( size[ 1 ] - h1) / 2 ) + 0.5 )
c2 = int ( ( size[ 1 ] - h1) / 2 )
mask1 = np. zeros( [ c1, w] , dtype= np. uint8)
mask2 = np. zeros( [ c2, w] , dtype= np. uint8)
img2 = np. concatenate( [ mask1, img1, mask2] , axis= 0 )
mask_image. append( img2)
print ( "*****" , img2. shape)
elif width < height:
h = size[ 1 ]
w = int ( h * ratio)
img1 = cv2. resize( img, ( w, h) )
h1, w1 = img1. shape
c1 = int ( ( ( size[ 0 ] - w1) / 2 ) + 0.5 )
c2 = int ( ( size[ 0 ] - w1) / 2 )
mask1 = np. zeros( [ h, c1] , dtype= np. uint8)
mask2 = np. zeros( [ h, c2] , dtype= np. uint8)
img2 = np. concatenate( [ mask1, img1, mask2] , axis= 1 )
mask_image. append( img2)
print ( ">>>>>>>>>" , img2. shape)
else :
img1 = cv2. resize( img, ( size[ 0 ] , size[ 1 ] ) )
mask_image. append( img1)
return mask_image[ 0 ]
def convert_image_size ( img, size) :
img= np. array( img)
new_shape = list ( img. shape)
new_shape[ 0 ] = size[ 0 ]
new_shape[ 1 ] = size[ 1 ]
mask_image = np. zeros( new_shape, dtype= np. uint8)
height, width= img. shape[ 0 ] , img. shape[ 1 ]
ratio = width / height
if width > height:
img2 = cv2. resize( img, ( size[ 0 ] , int ( size[ 0 ] / ratio) ) )
start= int ( ( size[ 1 ] - img2. shape[ 0 ] ) / 2 )
mask_image[ start: start+ img2. shape[ 0 ] , 0 : size[ 0 ] ] = img2
elif width < height:
img2 = cv2. resize( img, ( int ( size[ 1 ] * ratio) , size[ 1 ] ) )
start = int ( ( size[ 0 ] - img2. shape[ 1 ] ) / 2 )
mask_image[ 0 : size[ 1 ] , start: start+ img2. shape[ 1 ] ] = img2
else :
mask_image = cv2. resize( img, ( size[ 0 ] , size[ 1 ] ) )
return mask_image
if __name__ == '__main__' :
dir = "/media/tianhailong/新加卷/coco_seg/image1"
out_dir= "/media/tianhailong/新加卷/coco_seg/out"
for path in os. listdir( dir ) :
path1= os. path. join( dir , path)
print ( path1)
img = Image. open ( path1)
mask_image= convert_image_size( img, ( 640 , 640 ) )
cv2. imwrite( os. path. join( out_dir, path) , mask_image)