Using the environment

In deep learning, the prediction result of each slice of the image to be predicted is obtained through the prediction model, and all small slices need to be inlaid subsequently. However, because these slices do not have geographic coordinates, arcGIS and other tools cannot be used.

code

Parameter setting before Mosaic

import PIL.Image as Image
import os

IMAGES_PATH = R 'path of small slices'  # Gallery address
# IMAGES_FORMAT = ['.jpg', '.jpg'] # image format
IMAGES_FORMAT = ['.png']  # Image format
IMAGE_SIZE = 256  # Size of each small picture
IMAGE_ROW = 7  # image spacing, that is, how many lines are combined into one image
IMAGE_COLUMN = 6  # image spacing, that is, how many columns are combined into a single image
IMAGE_SAVE_PATH = 'predict.jpg'  # Address after image conversion
Copy the code

Slice the sorting

Sort by numeric size
files = os.listdir(IMAGES_PATH)
files.sort(key=lambda x: int(x.split('. ') [0]))
Copy the code

Splicing function

# Define image Mosaic function
def image_compose() :
    to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE, IMAGE_ROW * IMAGE_SIZE))  Create a new image
    # Loop through, paste each picture to the corresponding position in order
    for y in range(1, IMAGE_ROW + 1) :for x in range(1, IMAGE_COLUMN + 1):
            from_image = Image.open(IMAGES_PATH + image_names[IMAGE_COLUMN * (y - 1) + x - 1]).resize(
                (IMAGE_SIZE, IMAGE_SIZE), Image.ANTIALIAS)
            to_image.paste(from_image, ((x - 1) * IMAGE_SIZE, (y - 1) * IMAGE_SIZE))
    return to_image.save(IMAGE_SAVE_PATH)  # Save the new image
Copy the code

Complete function

# Mosaic the image
import PIL.Image as Image
import os

IMAGES_PATH = R 'path of small slices'  # Gallery address
# IMAGES_FORMAT = ['.jpg', '.jpg'] # image format
IMAGES_FORMAT = ['.png']  # Image format
IMAGE_SIZE = 256  # Size of each small picture
IMAGE_ROW = 7  # image spacing, that is, how many lines are combined into one image
IMAGE_COLUMN = 6  # image spacing, that is, how many columns are combined into a single image
IMAGE_SAVE_PATH = 'predict.jpg'  # Address after image conversion

Sort by numeric size
files = os.listdir(IMAGES_PATH)
files.sort(key=lambda x: int(x.split('. ') [0]))

Get all the image names under the gallery address
image_names = [name for name in files for item in IMAGES_FORMAT if
               os.path.splitext(name)[1] == item]


# Simple quantitative judgment for parameter setting and actual size of picture set
if len(image_names) ! = IMAGE_ROW * IMAGE_COLUMN:raise ValueError("The parameters of the composite image do not match the required number!")
print(image_names)

# Define image Mosaic function
def image_compose() :
    to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE, IMAGE_ROW * IMAGE_SIZE))  Create a new image
    # Loop through, paste each picture to the corresponding position in order
    for y in range(1, IMAGE_ROW + 1) :for x in range(1, IMAGE_COLUMN + 1):
            from_image = Image.open(IMAGES_PATH + image_names[IMAGE_COLUMN * (y - 1) + x - 1]).resize(
                (IMAGE_SIZE, IMAGE_SIZE), Image.ANTIALIAS)
            to_image.paste(from_image, ((x - 1) * IMAGE_SIZE, (y - 1) * IMAGE_SIZE))
    return to_image.save(IMAGE_SAVE_PATH)  # Save the new image

image_compose()  # call function

Copy the code

post-processing

According to the remote sensing image before clipping, the embedded prediction results are registered geographically.