LoginSignup
0
1

More than 1 year has passed since last update.

googlecolabで画像を複数一気にアップロードして表示させる方法

Posted at

はじめに

googlecolabを使っているとローカルから画像をアップロードしたくなることがありますよね。
そんな時、ちゃんとアップできたか確認するために画像をすべて表示したくなることがあります。
複数パスの取得など躓いたところがあったので書いておきます。

image.png

コード

こちらの記事を参考にしました。

アップロードについてはfiles.upload()ででき、以下のようなものが表示されるようになります。
この際、複数ファイルを選択してしたり、ファイル選択の部分に複数ファイルをドラックアンドドロップすることでまとめてアップロードすることができて便利です。

image.png

画像名の一括取得は以下のようにできます。

from google.colab import files
uploaded_files = files.upload()
uploaded_file_names = list(uploaded_files.keys())

これを利用して画像の表示までしたものが以下のコードです。

from google.colab import files
uploaded_files = files.upload()
uploaded_file_names = list(uploaded_files.keys())
uploaded_file_names.sort()
print(uploaded_file_names)

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



fig = plt.figure(figsize=(30, 40))
images=[]
for i in range(len(uploaded_file_names)):
    img = Image.open(uploaded_file_names[i])    
    images = np.asarray(img)
    ax = fig.add_subplot(10, 10, i+1, xticks=[], yticks=[])
    image_plt = np.array(images)
    ax.imshow(image_plt)
    ax.set_xlabel(str(i), fontsize=20)               
plt.show()
plt.close()  
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1