Search code examples
pythonimage-processingbackgroundimage-resizingforeground

How do I increase the size of the background without changing the size of the foreground in python?


I want to use the following picture: enter image description here

I want to increase the size of the background, so that the white shape doesn't lose its shape and size.

I want to have the resized background to be preferably twice the size of the biggest number in either width or length(depends which one is bigger). The output would therefore be a square.

I made an output that shows my goal (using paint):

enter image description here

Does anyone know how this can be done?


Solution

  • Using opencv you can do it with just a command:

    C++: copyMakeBorder(InputImage, OutputImage, int top, int bottom, int left, int right, BORDER_CONSTANT, Scalar(0,0,0) )
    Python: image = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=(0.0, 0.0, 0.0))
    

    Put whatever value you like for top, bottom , left and right. It keeps the original image unchanged but adds extra pixels to the borders as much as you want.

    More explanation at: https://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html?highlight=copymakeborder