Pillow, or PIL, Python Imaging Library, is the Chinese word for Pillow. Pillow is the standard library for image processing in the Python platform. It is very powerful and has an easy-to-use API.

This article shares an image filling function, PAD_image, implemented by Pillow to preprocess image data sets. In the object detection algorithm, you convert the input image into an image of the size of the model, while keeping the scale constant and filling the rest with gray.

The specific implementation of the function is as follows:

  • Calculate the width and height of the image after scaling, and scale down or expand;
  • Call resize() to change the size of the image;
  • Create a new image for new() target size target_size;
  • Paste () is called to place the original scaled image into the target image.

The implementation is as follows:

def pad_image(image, target_size):
    iw, ih = image.size  # Size of the original image
    w, h = target_size  The size of the target image
    scale = min(float(w) / float(iw), float(h) / float(ih))  # Minimum conversion ratio

    Ensure that the length or width, at least one match the size of the target image
    nw = int(iw * scale)
    nh = int(ih * scale)

    image = image.resize((nw, nh), Image.BICUBIC)  # Zoom out
    # image.show()
    new_image = Image.new('RGB', target_size, (128.128.128))  # Generate a gray image
    # // Is an integer division method to calculate the position of the image
    new_image.paste(image, ((w - nw) // 2, (h - nh) // 2))  # Fill the image with a middle image with a gray style on both sides
    # new_image.show()

    return new_image
Copy the code

Testing:

def main(a):
    img_path = 'xxxx.jpg'
    image = Image.open(img_path)
    size = (416.416)
    pad_image(image, size)  # Fill image

if __name__ == '__main__':
    main()
Copy the code

The original:

Modification:


OpenCV version

def preprocess_image(fn_img):
    img = cv2.imread(fn_img)
    h, w, _ = img.shape
    m = h if h > w else w
    r = m / 256
    h_ = int(h / r)
    w_ = int(w / r)
    img = cv2.resize(img, (w_, h_))

    offset_w = int((256 - w_) / 2)
    offset_h = int((256 - h_) / 2)
    img_bkg = np.ones((256.256.3)) * 255
    img_bkg = img_bkg.astype(int)
    img_bkg[offset_h:256 - offset_h, offset_w:256 - offset_w] = img

    return img_bkg
Copy the code

OK, that’s all! Enjoy it!

For more algorithm tips, follow wechat public ID: DeepAlgorithm