Skip to content

Crop object into rectangular shape from image with auto detection

License

Notifications You must be signed in to change notification settings

leeshinyook/RectangleCropper

Repository files navigation

RectangleCropper

Hits Generic badge

Overview

RectangleCropper is a Python library that crops images into rectangles. The image to be cropped is detected through the rule base with pixel search algorithm. For example, the image below shows a bestseller in a bookstore, and you want to crop the image of the book. RectangleCropper automatically detects the object area and crop it! Just pass the image to the RectangleCropper and it will be cropped like magic.

1. BookStore

스크린샷 2021-09-11 오후 10 12 09

2. OpenMarket

스크린샷 2021-09-11 오후 10 13 07

3. NewPaper

스크린샷 2021-09-11 오후 10 13 33

4. WebSite

스크린샷 2021-09-11 오후 10 15 34

5. Google Image

스크린샷 2021-09-11 오후 10 29 15

There are many services that use square-based images. This library was created by combining a general rule-based algorithm and a pixel-based search algorithm, not advanced technologies such as computer vision. This library shows the best performance when the background color is one color or two or three. So it doesn't have enough ability to cover the edge case. However, by applying this rule-based algorithm, it became possible to apply rules according to the service. For example, you can input guidelines for the size of the image to be cropped, and finely adjust the critical point required when cropping the image.

Requirements

  • Python >= 3
  • Works on Linux, Windows, macOS

Getting it

The quick way:

pip install RectangleCropper

Getting Started

import rectanglecropper.crop as rc

cropper = rc.RectangleImageCrop()
cropper.open(image_path='images/image1.png')
cropper.crop()
cropper.save(saved_path='cropimages', filename='image1', saved_format='jpeg')

Done!

Performance

Unless you need a lot of image processing, I recommend using this library. However, performance cannot be guaranteed. Because this library is going through the pixel-based search process. In Python, the cost of this process is very high. It works by converting to NumPy inside, not a list, but it doesn't change the fact that it's expensive. If you process many images at once, the memory may not be able to support it or it will take a very long time. Also, the larger the image size, the longer the cropping process takes. To solve the above cost problem, I will make a library of the same function written in Golang which is at least 5-7 times faster and will distribute it soon. thank you.

Advanced Usage

Is the image not cropping well?

Then I recommend adjusting the options of the crop method.

  • min_crop
import rectanglecropper.crop as rc

cropper = rc.RectangleImageCrop()
cropper.open(image_path='images/image1.png')
cropper.crop(min_crop_width=1000, min_crop_height=1000)
cropper.save(saved_path='cropimages', filename='image1', saved_format='jpeg')

The min_crop option above specifies the minimum size when cropping an image. If the option is 1000, it is ignored if the cropped size is less than 1000.

  • occupancy_rate
import rectanglecropper.crop as rc

cropper = rc.RectangleImageCrop()
cropper.open(image_path='images/image1.png')
cropper.crop(occupancy_rate=30)
cropper.save(saved_path='cropimages', filename='image1', saved_format='jpeg')

스크린샷 2021-09-11 오후 11 01 30

Probably, the background in this photo is black, and the images to be cropped are blue and yellow boxes. If you want to crop only the blue box here, you can enter the above option greater than 18. That is, the occupancy_rate can be filtered by determining the ratio based on the RGB channel.

  • pixel_sensitivity
import rectanglecropper.crop as rc

cropper = rc.RectangleImageCrop()
cropper.open(image_path='images/image1.png')
cropper.crop(pixel_sensitivity=5)
cropper.save(saved_path='cropimages', filename='image1', saved_format='jpeg')

스크린샷 2021-09-11 오후 11 10 30

The image is composed of RGB channels, so if you look closely, continuous channels are connected. If pixel sensitivity is given, the adjacent pixels are masked to see the same value. If the pixel values between the images to be cropped are not the same and are blurry, it is recommended to try the above option.

  • candidate_threshold_group
import rectanglecropper.crop as rc

cropper = rc.RectangleImageCrop()
cropper.open(image_path='images/image1.png')
cropper.crop(candidate_threshold_group=5)
cropper.save(saved_path='cropimages', filename='image1', saved_format='jpeg')

An image that is approached in units of pixels divides the area according to a threshold value. If there are various RGB channels in the area to be divided, it is recommended to adjust the above options.

In fact, it is necessary to understand the option values related to this crop. So, I set the initial value in advance, and if you need to customize it, please set it not to deviate too much from this default value.

Finally

Many features will be added in the future.

If you have any problems, I'd appreciate it if you could leave them in the issue.

About

Crop object into rectangular shape from image with auto detection

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages