python - Adding a border to and Image in my code -
help, need put border on image displayed code below. how do this? code below pulls image, , need border. help.. all of code python
# -*- coding: utf-8 -*- ''' logancaroline_1_4_7: merge images ''' import matplotlib.pyplot plt import os.path import numpy np # “as” lets use standard abbreviations '''read image data''' # directory of python script directory = os.path.dirname(os.path.abspath(__file__)) # build absolute filename directory + filename filename = os.path.join(directory, 'family1.jpg') # read image data array img = plt.imread(filename) '''show image data''' # create figure 1 subplot fig, ax = plt.subplots(1, 1) # show image data in subplot ax.imshow(img, interpolation='none') # show figure on screen fig.show()
just create larger array black , fill central pixels image.
import numpy np import matplotlib.pyplot plt def frame_image(img, frame_width): b = frame_width # border size in pixel ny, nx = img.shape[0], img.shape[1] # resolution / number of pixels in x , y if img.ndim == 3: # rgb or rgba array framed_img = np.zeros((b+ny+b, b+nx+b, img.shape[2])) elif img.ndim == 2: # grayscale image framed_img = np.zeros((b+ny+b, b+nx+b)) framed_img[b:-b, b:-b] = img return framed_img img = np.random.rand(456, 333, 3) fig, (ax1, ax2) = plt.subplots(1,2) ax1.imshow(img, interpolation='none') ax2.imshow(frame_image(img, 20), interpolation='none') plt.show() 
Comments
Post a Comment