Use Python to stitch the segmented image of Market1501 and the original image into a left and right image, and adjust the pixel value of the image to 256*128.
All folders:
All original images in the folder:
All split pictures in the folder:
code show as below:
import PIL.Image as Image
import os
IMAGES_PATH ='E:/gyx/Learning/Practice/4/data/market1501_seg_1/test/ori_img/' #Original photo gallery address
IMAGES_PATH_1 ='E:/gyx/Learning/Practice/4/data/market1501_seg_1/test/seg_img/' #Split picture set address
IMAGE_SAVE_PATH ='E:/gyx/Learning/Practice/4/data/market1501_seg_1/test/new/' #Save new picture address
IMAGES_FORMAT =['.jpg','.JPG'] #Image Format
list_n =[]
w =256 #The size of each small picture
h =128
# Get all the picture names under the picture collection address
image_names =[name for name in os.listdir(IMAGES_PATH)for item in IMAGES_FORMAT if
os.path.splitext(name)[1]== item]
image_names_1 =[name for name in os.listdir(IMAGES_PATH_1)for item in IMAGES_FORMAT if
os.path.splitext(name)[1]== item]
# Define image stitching function
def image_compose(imag,imag_1):
src = os.path.join(os.path.abspath(IMAGE_SAVE_PATH), img)
to_image = Image.new('RGB',(2* h,1* w)) #Create a new graph
# Paste the two pictures to the corresponding positions in order
rom_image = Image.open(IMAGES_PATH + imag).resize((h,w), Image.ANTIALIAS)
rom_image_1 = Image.open(IMAGES_PATH_1 + imag_1).resize((h, w), Image.ANTIALIAS)
to_image.paste(rom_image,(0,0))
to_image.paste(rom_image_1,(h,0))
to_image.save(src) #Save the new image with the original name
# Call the splicing function if the file name is the same
for img in image_names:for img_1 in image_names_1:if img == img_1:image_compose(img,img_1)
Stitching effect of separate pictures:
The effect of the picture in the new folder new:
This article has been included in the topic "Python Image Processing Operations", welcome to click to learn more exciting content.
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts