Help, I need to put a border on the image displayed from the code below. How do I do this? The code below pulls up the image, and now i need a border. Help.. All of this code is Python
JavaScript
x
24
24
1
# -*- coding: utf-8 -*-
2
'''
3
LoganCaroline_1_4_7: Merge images
4
'''
5
import matplotlib.pyplot as plt
6
import os.path
7
import numpy as np # “as” lets us use standard abbreviations
8
9
'''Read the image data'''
10
# Get the directory of this python script
11
directory = os.path.dirname(os.path.abspath(__file__))
12
# Build an absolute filename from directory + filename
13
filename = os.path.join(directory, 'family1.jpg')
14
# Read the image data into an array
15
img = plt.imread(filename)
16
17
'''Show the image data'''
18
# Create figure with 1 subplot
19
fig, ax = plt.subplots(1, 1)
20
# Show the image data in a subplot
21
ax.imshow(img, interpolation='none')
22
# Show the figure on the screen
23
fig.show()
24
Advertisement
Answer
Just create a slightly larger array that is black and fill the central pixels with your image.
JavaScript
1
19
19
1
import numpy as np
2
import matplotlib.pyplot as plt
3
4
def frame_image(img, frame_width):
5
b = frame_width # border size in pixel
6
ny, nx = img.shape[0], img.shape[1] # resolution / number of pixels in x and y
7
if img.ndim == 3: # rgb or rgba array
8
framed_img = np.zeros((b+ny+b, b+nx+b, img.shape[2]))
9
elif img.ndim == 2: # grayscale image
10
framed_img = np.zeros((b+ny+b, b+nx+b))
11
framed_img[b:-b, b:-b] = img
12
return framed_img
13
14
img = np.random.rand(456, 333, 3)
15
fig, (ax1, ax2) = plt.subplots(1,2)
16
ax1.imshow(img, interpolation='none')
17
ax2.imshow(frame_image(img, 20), interpolation='none')
18
plt.show()
19