python:images

Manipulation d'images avec Python

Source

from PIL import Image
 
im1 = Image.open('data/src/lena.jpg')
dst = Image.new('RGB', (im1.width + im1.width, im1.height))
dst.paste(im1, (0, 0))
dst.paste(im1, (im1.width, 0))
dst.save('data/dst/pillow_concat_h.jpg')

Source

from PIL import Image
import numpy as np
 
img_pil = Image.open("01bw.png")
img = np.array(img_pil)

Si l'image est en niveau de gris, le tableau Numpy obtenu n'a que deux dimensions. Le tableau obtenu est row-major: img[x][y].


from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
 
img_pil = Image.open("01bw.png")
img = np.array(img_pil)
 
plt.imshow(img)
plt.show()

Si l'image est en niveau de gris, il faut le préciser:

plt.imshow(img, cmap='gray', vmin=0, vmax=255)

Il est possible d'ajouter des lignes horizontales et verticales sur l'affichage de l'image:

plt.axhline(y=black_keys_line)
plt.axvline(x=x, color='green')
  • python/images.txt
  • Dernière modification : 2021/04/04 17:01
  • de 127.0.0.1