Exemples de comment ajouter une bordure (cadre) à une image en python ?
Importer une image
Commençons par importer une image en utilisant python(par exemple Eiffel Tower):
from matplotlib import imageimport matplotlib.pyplot as pltimg = image.imread("eiffel-tower.jpeg")plt.imshow(img)plt.show()print(img.shape)

Ajouter un cadre à une image
Pour ajouter un cadre/bordure à une image, une solution consiste à utiliser numpy.pad.
Un exemple en ajoutant une bordure noire "constant_values=0":
img1 = np.pad(img, ((100, 100), (200, 200), (0,0)), constant_values=0)print(img1.shape)plt.imshow(img1)plt.savefig("pad_image_01.png", bbox_inches='tight', dpi=100)plt.show()
donne alors

Cadre uniquement en haut et en bas de l'image
img1 = np.pad(img, ((100, 100), (0, 0), (0,0)), constant_values=0)print(img1.shape)plt.imshow(img1)plt.savefig("pad_image_02.png", bbox_inches='tight', dpi=100)plt.show()
donne

Padding uniquement sur les côtés droit et gauche de l'image
img1 = np.pad(img, ((0, 0), (200, 200), (0,0)), constant_values=0)print(img1.shape)plt.imshow(img1)plt.savefig("pad_image_03.png", bbox_inches='tight', dpi=100)plt.show()
donne

Exemple utilisant les valeurs les plus proches en utilisant 'edge'
img1 = np.pad(img, ((100, 100), (0, 0), (0,0)), 'edge')print(img1.shape)plt.imshow(img1)plt.savefig("pad_image_04.png", bbox_inches='tight', dpi=100)plt.show()

Supprimer les axes avec matplotlib
Remarque : pour obtenir uniquement l'image sans les axes avec matplotlib une solution est de faire
import numpy as npimport matplotlib.cm as cmimport matplotlib.mlab as mlabimport matplotlib.pyplot as pltmy_dpi=100Shape = img1.shapefig = plt.figure()ax = plt.Axes(fig, [0., 0., 1., 1.])ax.set_axis_off()fig.add_axes(ax)im = plt.imshow(img1)plt.savefig("pad_image_05.png", bbox_inches='tight', dpi=100)plt.show()
donne ici

