Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by InterstellarSatellite564

How do I center a cropped image on a 700x700 black canvas using padding in Python?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I am processing images of varying sizes and want to crop them to a fixed 700x700 size. However, some images are slightly smaller than 700x700, so I need to add padding that centers the image and fills the extra space with black pixels.

I've tried using cv2.resize(center_contour, (700, 700), interpolation=cv2.INTER_NEAREST), but that approach distorts the image, as it resizes the object rather than just adding padding.

Here is the current code:

PYTHON
# Cropping image if np.shape(c_contour)[0] >= 700 and np.shape(c_contour)[1] >= 700: crop_img = center_contour[center_y - 350:center_y + 350, center_x - 350 :center_x + 350] else: padded_img = np.zeros((700, 700, 3), dtype=np.uint8) if np.shape(c_contour)[0] < 700: crop_img = center_contour[:, center_x - 350 :center_x + 350] else: crop_img = center_contour[center_y - 350:center_y + 350, :] #####Make the photo center while padding #####.....

How can I add padding so that the image remains centered without distorting the original object size?

0

Comments 0

Answers 2

Views 76

2 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

Below is one way to achieve this without distorting your object. You can compute how much padding is needed on each side so that your cropped (or resized) image remains at its original size, and then place it centered onto a new 700×700 black canvas. For example, suppose you already have your crop (or subimage) extracted as crop_img; you can do this as follows:

PYTHON
import cv2 import numpy as np # Assume crop_img is your extracted image patch (with no distortion) h, w = crop_img.shape[:2] # Calculate padding for top, bottom, left, and right to center the image pad_top = (700 - h) // 2 pad_bottom = 700 - h - pad_top pad_left = (700 - w) // 2 pad_right = 700 - w - pad_left # Use cv2.copyMakeBorder to pad the image with black pixels (value=[0,0,0]) padded_img = cv2.copyMakeBorder(crop_img, pad_top, pad_bottom, pad_left, pad_right, cv2.BORDER_CONSTANT, value=[0,0,0]) # padded_img is now 700x700 with crop_img centered cv2.imshow("Padded", padded_img) cv2.waitKey(0) cv2.destroyAllWindows()

Explanation

  • First, determine the height (h) and width (w) of the cropped image.
  • Compute the padding needed on each side to center the image in a 700×700 canvas.
  • Use the computed values with cv2.copyMakeBorder, which adds constant padding (black in this case) to the image borders.
  • This method preserves the object’s original size while embedding it within a fixed-size canvas.

This avoids the distortion issues you experienced with cv2.resize, as the resized image will keep the original feature scale intact.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by CelestialSurveyor250 1 month ago

0

If you know how much to pad before/after the height/width, you can just use np.pad.

PYTHON
def get_pad(curr_size: int, target_size: int): d = target_size - curr_size if d <= 0: return (0, 0) # no need to pad p1 = d // 2 p2 = d - p1 return (p1, p2) H, W = 600, 650 # make dummy image image = np.random.randint(0, 255, (H, W, 3), np.uint8) DH, DW = 700, 700 # desired height / width padding = (get_pad(H, DH), get_pad(W, DW), (0, 0)) image_padded = np.pad(image, padding, constant_values=0)

No comments yet.

Discussion

No comments yet.