2016-03-15 16 views

Trả lời

7

này đã làm việc cho tôi:

from matplotlib.pyplot import figure, imshow, axis 
from matplotlib.image import imread 

def showImagesHorizontally(list_of_files): 
    fig = figure() 
    number_of_files = len(list_of_files) 
    for i in range(number_of_files): 
     a=fig.add_subplot(1,number_of_files,i+1) 
     image = imread(list_of_files[i]) 
     imshow(image,cmap='Greys_r') 
     axis('off') 

enter image description here

-1

Tôi nghĩ bạn sẽ phải tạo một hình với các ô con và gán từng hình vào một trong các ô con. Một cái gì đó như:

import matplotlib.pyplot as plt 
f,ax = plt.subplots(1,5) 
for i in range(5): 
    ax[i].imshow(yourimage) 
plt.show() # or display.display(plt.gcf()) if you prefer 
10

Bạn cũng có thể sử dụng HTML:

from IPython.display import display, HTML 
def make_html(folder, image): 
    return '<img src="{}" style="display:inline;margin:1px"/>' 
      .format(os.path.join(folder, image)) 

display(HTML(''.join(make_html(f, x)) for x in files)) 

Trong trường hợp của tôi, bằng cách thiết lập một biên độ sẽ sửa chữa un-alignment (và IMHO tạo ra kết quả đẹp hơn).

+0

Có thể bạn nên thêm HTML đến từ đâu. Sau khi một số googling tôi nghĩ rằng bạn hoặc tham khảo [html module] (https://pypi.python.org/pypi/html) hoặc để [HTML.py] (http://www.decalage.info/en/python/html). Trước đây cho tôi một lỗi khi cố gắng cài đặt theo python3.6 và sau này là quá kỳ lạ đối với tôi (tức là không phải trên pipy). – Toby

+1

Ahh, và bây giờ tôi tìm thấy những gì bạn thực sự đã đề cập đến: 'từ IPython.core.display nhập khẩu HTML ' – Toby

+0

@ Toby bạn là đúng, đã không nhận thấy nó. Đã sửa. – AkiRoss

1

Đây là một cải tiến đối với câu trả lời của AkiRoss. Điều này cho phép khả năng linh hoạt hơn về cách bạn hiển thị và tránh lặp lại hàm cho mỗi hàng bằng cách hiển thị theo định dạng lưới.

import matplotlib.pyplot as plt 

def grid_display(list_of_images, list_of_titles=[], no_of_columns=2, figsize=(10,10)): 

    fig = plt.figure(figsize=figsize) 
    column = 0 
    for i in range(len(list_of_images)): 
     column += 1 
     # check for end of column and create a new figure 
     if column == no_of_columns+1: 
      fig = plt.figure(figsize=figsize) 
      column = 1 
     fig.add_subplot(1, no_of_columns, column) 
     plt.imshow(list_of_images[i]) 
     plt.axis('off') 
     if len(list_of_titles) >= len(list_of_images): 
      plt.title(list_of_titles[i]) 

Tham số:

  • list_of_images - một danh sách chứa tất cả các hình ảnh mà bạn muốn hiển thị.
  • list_of_titles - danh sách chứa tất cả các tiêu đề của hình ảnh theo cùng một thứ tự.
  • no_of_columns - số cột trong lưới.
  • figsize - kích thước của mỗi hàng để tránh bị đè bẹp. (horizontal_size, vertical_size).

Ví dụ:

import cv2 
import matplotlib.pyplot as plt 

img = cv2.imread("files/tiger_monkey.jpg") 
grey_img = cv2.imread("files/tiger_monkey_grey.png") 

img_b, img_g, img_r = cv2.split(img) 
grey_img_b, grey_img_g, grey_img_r = cv2.split(grey_img) 

combi_one = cv2.merge((img_b, grey_img_g, img_r)) 
combi_two = cv2.merge((grey_img_b, grey_img_g, img_r)) 
combi_three = cv2.merge((img_b, img_g, grey_img_r)) 
combi_four = cv2.merge((grey_img_b, img_g, img_r)) 
combi_five = cv2.merge((grey_img_b, img_g, grey_img_r)) 
combi_six = cv2.merge((img_b, grey_img_g, grey_img_r)) 

titles = ["combi_one", 
      "combi_two", 
      "combi_three", 
      "combi_four", 
      "combi_five", 
      "combi_six"] 
images = [combi_one, 
      combi_two, 
      combi_three, 
      combi_four, 
      combi_five, 
      combi_six] 

images = [cv2.cvtColor(image, cv2.COLOR_BGR2RGB) for image in images] 
grid_display(images, titles, 3, (10,10)) 

hình ảnh gốc:

Các vấn đề liên quan