Adding Borders to an Image Using Python

Adding borders to an image using python

You can create a new image with the desired new size, and paste the old image in the center, then saving it. If you want, you can overwrite the original image (are you sure? ;o)

import Image

old_im = Image.open('someimage.jpg')
old_size = old_im.size

new_size = (800, 800)
new_im = Image.new("RGB", new_size) ## luckily, this is already black!
box = tuple((n - o) // 2 for n, o in zip(new_size, old_size))
new_im.paste(old_im, box)

new_im.show()
# new_im.save('someimage.jpg')

You can also set the color of the new border with a third argument of Image.new() (for example: Image.new("RGB", new_size, "White"))

create an image with border of certain width in python

This is what you need to change to make the border any number of px wide:

for x in range(w):
for y in range(h):
if (x<border_width
or y<border_width
or x>w-border_width-1
or y>h-border_width-1):
pixels[x,y] = (0,0,0)

#other 3 boxes and #primary box Doesn't make boxes but instead 3 points and 1 point respectively.

how to add border around an image in opencv python

The following code adds a constant border of size 10 pixels to all four sides of your original image.

For the colour, I have assumed that you want to use the average gray value of the background, which I have calculated from the mean value of bottom two lines of your image. Sorry, somewhat hard coded, but shows the general how-to and can be adapted to your needs.

If you leave bordersize values for bottom and right at 0, you even get a symmetric border.

Other values for BORDER_TYPE are possible, such as BORDER_DEFAULT, BORDER_REPLICATE, BORDER_WRAP.

For more details cf: http://docs.opencv.org/trunk/d3/df2/tutorial_py_basic_ops.html#gsc.tab=0

import numpy as np
import cv2

im = cv2.imread('image.jpg')
row, col = im.shape[:2]
bottom = im[row-2:row, 0:col]
mean = cv2.mean(bottom)[0]

bordersize = 10
border = cv2.copyMakeBorder(
im,
top=bordersize,
bottom=bordersize,
left=bordersize,
right=bordersize,
borderType=cv2.BORDER_CONSTANT,
value=[mean, mean, mean]
)

cv2.imshow('image', im)
cv2.imshow('bottom', bottom)
cv2.imshow('border', border)
cv2.waitKey(0)
cv2.destroyAllWindows()

How to add border to an image, choosing color dynamically based on image edge color(s)?

Overwrite the center part of the image with some fixed color, that – most likely – won't be present within the edge. For that, maybe use a color with a certain alpha value. Then, there's a function getcolors, which exactly does, what you're looking for. Sort the resulting list, and get the color with the highest count. (That, often, will be the color we used to overwrite the center part. So check for that, and take the second entry, if needed.) Finally, use ImageOps.expand to add the actual border.

That'd be the whole code:

from PIL import Image, ImageDraw, ImageOps

# Open image, enforce RGB with alpha channel
img = Image.open('path/to/your/image.png').convert('RGBA')
w, h = img.size

# Set up edge margin to look for dominant color
me = 3

# Set up border margin to be added in dominant color
mb = 30

# On an image copy, set non edge pixels to (0, 0, 0, 0)
img_copy = img.copy()
draw = ImageDraw.Draw(img_copy)
draw.rectangle((me, me, w - (me + 1), h - (me + 1)), (0, 0, 0, 0))

# Count colors, first entry most likely is color used to overwrite pixels
n_colors = sorted(img_copy.getcolors(2 * me * (w + h) + 1), reverse=True)
dom_color = n_colors[0][1] if n_colors[0][1] != (0, 0, 0, 0) else n_colors[1][1]

# Add border
img = ImageOps.expand(img, mb, dom_color).convert('RGB')

# Save image
img.save('with_border.png')

That'd be the result for your example:

Output

And, that's some output for another image:

Other output

It's up to you to decide, whether there are several dominant colors, which you want to mix or average. You'd need to inspect the n_colors appropriately on the several counts for that. That's quite a lot of work, which is left out here.

----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.9.1
PyCharm: 2021.1
Pillow: 8.2.0
----------------------------------------

Transparent border of the image Pillow

Create a copy of image you need to keep for 100% opacity. Put the opacity to the main image, and at last, paste the copied image to the original image. Opacity at the border done.

from PIL import Image

padding = 32
opacity = 127
img = Image.open("image.png").convert('RGBA')

x, y, w, h = padding, padding, img.width - padding, img.height - padding
img_cropped = img.crop((x, y, w, h))

img.putalpha(127)

img.paste(img_cropped, (x, y))
img.save('image_new.png')

applying image decoration (border) in Python (programmatically)

Look at the ImageOps module within the PIL.

import Image
import ImageOps

x = Image.open('test.png')
y = ImageOps.expand(x,border=5,fill='red')
y.save('test2.png')


Related Topics



Leave a reply



Submit