Как я могу сохранить компоненты изображения в 3 разных файлах за один раз? - PullRequest
0 голосов
/ 04 марта 2020

Ниже код сохраняет все компоненты RGB как один файл с именем, как показано на рисунке. Возможно ли, что я могу сохранить каждый из них в разные файлы за один раз? Также я не понимаю, почему синий компонент имеет нумерованные оси, а другие нет.

from matplotlib import pyplot as plt
from PIL import Image
import numpy as np

#Image into RGB Components
img1 = np.array(Image.open('1.jpeg'))
figure, plots = plt.subplots(ncols=3, nrows=1,figsize=(12, 4))
for i, subplot in zip(range(3), plots):
    temp = np.zeros(img1.shape, dtype='uint8')
    temp[:,:,i] = img1[:,:,i]   
    subplot.imshow(temp)
    plt.savefig("RedComponent.png", bbox_inches='tight')
    subplot.set_axis_off()
plt.show()

enter image description here

1 Ответ

1 голос
/ 04 марта 2020

Ваш последний вызов savefig происходит перед удалением оси, поэтому он находится на объекте конечных осей.

from matplotlib import pyplot as plt
from PIL import Image
import numpy as np

#Image into RGB Components
img1 = np.array(Image.open('1.jpeg'))
rgb = ("Red", "Green", "Blue")  # this gives you the basis for different filenames
figure, plots = plt.subplots(ncols=3, nrows=1,figsize=(12, 4))

for i, subplot, channel in zip(range(3), plots, rgb):
    # create an additional figure within the loop for the single colour
    fig2, ax2 = plt.subplots(figsize=(4, 4))
    temp = np.zeros(img1.shape, dtype='uint8')
    temp[:,:,i] = img1[:,:,i]   
    subplot.imshow(temp)
    ax2.imshow(temp)
    # removing the axis before saving
    ax2.set_axis_off()
    subplot.set_axis_off()
    # Save single colour panel as its own file
    fig2.savefig("{}Component.png".format(channel), bbox_inches='tight')
    plt.close(fig2)  # remove the single-colour plot
# save the three-Axes figure separately once outside the loop
figure.savefig("AllComponents.png", bbox_inches='tight')


plt.show()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...