Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Lưu mảng numpy thành ảnh grayscale với Matplotlib


Các bước

  1. Thiết lập kích thước hình và padding.
  2. Tạo dữ liệu ngẫu nhiên 5x5.
  3. Đặt colormap 'gray' bằng plt.gray().
  4. Hiển thị bằng imshow().
  5. Gọi show().

Ví dụ

import numpy as np
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

arr = np.random.rand(5, 5)
plt.gray()
plt.imshow(arr)
plt.show()

Kết quả

Lưu mảng numpy thành ảnh grayscale với Matplotlib