質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
WSL(Windows Subsystem for Linux)

WSL (Windows Subsystem for Linux) は、Windows10のOS上でLinux向けのバイナリプログラムを実行可能にする機能です。また、WindowsOSのAPIを用いた仕組みを提供しており、Linux側からWindowsOSへのファイルアクセスもできます。

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

apt-get

apt-getコマンドはUNIX系OSのパッケージのインストールなど、パッケージ管理を行うためのコマンドです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

2630閲覧

WSL環境でPIL(Pillow)を使ってmnistの画像を表示させたい

xozeit

総合スコア19

WSL(Windows Subsystem for Linux)

WSL (Windows Subsystem for Linux) は、Windows10のOS上でLinux向けのバイナリプログラムを実行可能にする機能です。また、WindowsOSのAPIを用いた仕組みを提供しており、Linux側からWindowsOSへのファイルアクセスもできます。

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

apt-get

apt-getコマンドはUNIX系OSのパッケージのインストールなど、パッケージ管理を行うためのコマンドです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/09/05 10:04

編集2021/09/07 22:22

mnistの画像が表示されない

オライリー・ジャパンの「ゼロから作るDeep Learning」P74
の以下のコードで画像を出そうとしているのですが、出なくて困っています。。。

発生している問題

エラーは出ないのに画像が表示されない。

該当のソースコード

python3

1# coding: utf-8 2import sys, os 3sys.path.append(os.pardir) # 親ディレクトリのファイルをインポートするための設定 4import numpy as np 5from dataset.mnist import load_mnist 6from PIL import Image 7 8def img_show(img): 9 pil_img = Image.fromarray(np.uint8(img)) 10 pil_img.show() 11 12(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False) 13 14img = x_train[0] 15label = t_train[0] 16print(label) # 5 17 18print(img.shape) # (784,) 19img = img.reshape(28, 28) # 形状を元の画像サイズに変形 20print(img.shape) # (28, 28) 21 22img_show(img)
#結果 5 (784,) (28, 28)

環境

Windows10
mobaxterm
WSL-Ubuntu

試したこと

①そもそも画像が表示できる環境か?
結果:問題なくテスト画像(試しに落としたいらすとや画像)は表示された。

python3

1import numpy as np 2from PIL import Image 3from matplotlib import pyplot as plt 4 5filename = "eto_tora_banzai.png" 6imgPIL = Image.open(filename) 7arrPIL = np.asarray(imgPIL) 8plt.imshow(arrPIL) 9plt.show()

② ①のテスト画像は問題の関数で表示されるか?
結果:されない。

# coding: utf-8 import sys, os sys.path.append(os.pardir) # 親ディレクトリのファイルをインポートするための設定 import numpy as np from dataset.mnist import load_mnist from PIL import Image def img_show(img): pil_img = Image.fromarray(np.uint8(img)) pil_img.show() (x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False) #img = x_train[0] img = Image.open("eto_tora_banzai.png") print(type(img)) arr = np.array(img) print(type(arr)) arr2 = np.ravel(arr) img = Image.fromarray(arr) print(type(img)) print("arr:",arr.shape) print("arr:",arr2.shape) img = arr2.reshape(789, 803, 4) print(img.shape) img_show(img)
<class 'PIL.PngImagePlugin.PngImageFile'> <class 'numpy.ndarray'> <class 'PIL.Image.Image'> arr: (789, 803, 4) arr: (2534268,) (789, 803, 4)

③別の人の同質問の回答を確認

この回答の以下のコードを実行。
結果:いずれも表示されない。

https://teratail.com/questions/323922

import tensorflow as tf import numpy as np from PIL import Image (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() img = x_train[0] im = Image.fromarray(np.uint8(img)) im.show()
#結果 2021-09-05 18:56:15.522536: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory 2021-09-05 18:56:15.522709: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.

https://teratail.com/questions/321884

pytho3

1# coding: utf-8 2import sys, os 3sys.path.append(os.pardir) # 親ディレクトリのファイルをインポートするための設定 4import numpy as np 5from dataset.mnist import load_mnist 6from PIL import Image 7from IPython.display import display 8from matplotlib import pyplot as plt 9 10 11 12def img_show(img): 13 pil_img = Image.fromarray(np.uint8(img)) 14 #pil_img = Image.open(np.uint8(img)) 15 #pil_img.show() 16 display(pil_img) 17 18(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False) 19 20img = x_train[0] 21label = t_train[0] 22print(label) # 5 23 24print(img.shape) # (784,) 25print(type(img)) 26img = img.reshape(28, 28) # 形状を元の画像サイズに変形 27print(img.shape) # (28, 28) 28 29img_show(img) 30print(type(img)) 31
#結果 5 (784,) <class 'numpy.ndarray'> (28, 28) <PIL.Image.Image image mode=L size=28x28 at 0x7F5920077AF0> <class 'numpy.ndarray'>

■210907追記

imagemagickはなぜかインストールがNot Foundで上手くいきませんでした。

kataoka@DESKTOP-IJMM4SU:/mnt/c/Users/tiezo/Desktop/python/deep-learning-from-scratch/ch03$ sudo apt install imagemagick [sudo] password for kataoka: Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: bridge-utils libfprint-2-tod1 ubuntu-fan Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: gsfonts imagemagick-6-common imagemagick-6.q16 libdjvulibre-text libdjvulibre21 libfftw3-double3 libilmbase24 liblqr-1-0 libmagickcore-6.q16-6 libmagickcore-6.q16-6-extra libmagickwand-6.q16-6 libnetpbm10 libopenexr24 libwmf0.2-7 netpbm Suggested packages: imagemagick-doc autotrace cups-bsd | lpr | lprng enscript ffmpeg gimp gnuplot grads graphviz hp2xx html2ps libwmf-bin mplayer povray radiance texlive-base-bin transfig ufraw-batch libfftw3-bin libfftw3-dev inkscape libjxr-tools libwmf0.2-7-gtk The following NEW packages will be installed: gsfonts imagemagick imagemagick-6-common imagemagick-6.q16 libdjvulibre-text libdjvulibre21 libfftw3-double3 libilmbase24 liblqr-1-0 libmagickcore-6.q16-6 libmagickcore-6.q16-6-extra libmagickwand-6.q16-6 libnetpbm10 libopenexr24 libwmf0.2-7 netpbm 0 upgraded, 16 newly installed, 0 to remove and 59 not upgraded. Need to get 2513 kB/8898 kB of archives. After this operation, 28.0 MB of additional disk space will be used. Do you want to continue? [Y/n] y Ign:1 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 imagemagick-6-common all 8:6.9.10.23+dfsg-2.1ubuntu11.1 Ign:2 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 libmagickcore-6.q16-6 amd64 8:6.9.10.23+dfsg-2.1ubuntu11.1 Ign:3 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 libmagickwand-6.q16-6 amd64 8:6.9.10.23+dfsg-2.1ubuntu11.1 Ign:4 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 imagemagick-6.q16 amd64 8:6.9.10.23+dfsg-2.1ubuntu11.1 Ign:5 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 imagemagick amd64 8:6.9.10.23+dfsg-2.1ubuntu11.1 Ign:6 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 libmagickcore-6.q16-6-extra amd64 8:6.9.10.23+dfsg-2.1ubuntu11.1 Ign:1 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 imagemagick-6-common all 8:6.9.10.23+dfsg-2.1ubuntu11.1 Err:2 http://security.ubuntu.com/ubuntu focal-updates/universe amd64 libmagickcore-6.q16-6 amd64 8:6.9.10.23+dfsg-2.1ubuntu11.1 404 Not Found [IP: 2001:67c:1360:8001::23 80] Err:3 http://security.ubuntu.com/ubuntu focal-updates/universe amd64 libmagickwand-6.q16-6 amd64 8:6.9.10.23+dfsg-2.1ubuntu11.1 404 Not Found [IP: 2001:67c:1360:8001::23 80] Err:4 http://security.ubuntu.com/ubuntu focal-updates/universe amd64 imagemagick-6.q16 amd64 8:6.9.10.23+dfsg-2.1ubuntu11.1 404 Not Found [IP: 2001:67c:1360:8001::23 80] Err:5 http://security.ubuntu.com/ubuntu focal-updates/universe amd64 imagemagick amd64 8:6.9.10.23+dfsg-2.1ubuntu11.1 404 Not Found [IP: 2001:67c:1360:8001::23 80] Err:6 http://security.ubuntu.com/ubuntu focal-updates/universe amd64 libmagickcore-6.q16-6-extra amd64 8:6.9.10.23+dfsg-2.1ubuntu11.1 404 Not Found [IP: 2001:67c:1360:8001::23 80] Ign:1 http://security.ubuntu.com/ubuntu focal-updates/universe amd64 imagemagick-6-common all 8:6.9.10.23+dfsg-2.1ubuntu11.1 Err:1 http://security.ubuntu.com/ubuntu focal-updates/universe amd64 imagemagick-6-common all 8:6.9.10.23+dfsg-2.1ubuntu11.1 404 Not Found [IP: 2001:67c:1360:8001::23 80] E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/i/imagemagick/imagemagick-6-common_6.9.10.23+dfsg-2.1ubuntu11.1_all.deb 404 Not Found [IP: 2001:67c:1360:8001::23 80] E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/i/imagemagick/libmagickcore-6.q16-6_6.9.10.23+dfsg-2.1ubuntu11.1_amd64.deb 404 Not Found [IP: 2001:67c:1360:8001::23 80] E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/i/imagemagick/libmagickwand-6.q16-6_6.9.10.23+dfsg-2.1ubuntu11.1_amd64.deb 404 Not Found [IP: 2001:67c:1360:8001::23 80] E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/i/imagemagick/imagemagick-6.q16_6.9.10.23+dfsg-2.1ubuntu11.1_amd64.deb 404 Not Found [IP: 2001:67c:1360:8001::23 80] E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/i/imagemagick/imagemagick_6.9.10.23+dfsg-2.1ubuntu11.1_amd64.deb 404 Not Found [IP: 2001:67c:1360:8001::23 80] E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/i/imagemagick/libmagickcore-6.q16-6-extra_6.9.10.23+dfsg-2.1ubuntu11.1_amd64.deb 404 Not Found [IP: 2001:67c:1360:8001::23 80] E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

一方matplotlibに変えたら表示できました!ありがとうございます。
ただ本に記載されているのを毎回デバッグするのも大変なので出来れば原因把握したいのですが、何が考えられるでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori
jbpb0

2021/09/06 00:17

> ①そもそも画像が表示できる環境か? ではmatplotlibで表示できてて、 > ② ①のテスト画像は問題の関数で表示されるか? では同じ画像がPIL(Pillow?)で表示できてないのだから、mnist画像もmatplotlibで表示させたらいいのでは??
xozeit

2021/09/06 21:42 編集

ご回答ありがとうございます。 見づらかったので本文末尾に返信を移動します。
hoshi-takanori

2021/09/06 22:04

WSL 使ってないので良く知りませんが、一種の仮想環境なので環境構築に苦労するし、本に記載するほど一般的な環境でもない (本のコードを何も考えずにそのまま動かしたいなら同じ環境でやるべき) ってことでは。 あと、③別の人の同質問の回答を確認 は Jupyter の話で、ターミナルから直接実行する話ではないのでは。
jbpb0

2021/09/06 23:21

タグに「WSL」と「apt-get」を追加して、質問の題名も編集してそのあたりの単語を含めたら、詳しい人が回答してくれるかも
xozeit

2021/09/07 22:33 編集

お二方のおかげで目下の問題はクリアできました。迅速にご回答ありがとうございました。 ③Jupyterの話については、ダメもとで参考にしました。こちらの環境では基本的にUbuntuでスクリプト組んでターミナルで実行しています。Jupyterも使ったことありますが実行結果はほとんど同じ感触だったので期待したのですが。 ちなみに書籍にはWindows, Mac, Linuxいずれの環境でも動くって書いてあるのですけどね。(さすがにWSLとは書いてないですが。。。) アドバイスに従ってタイトル変更しました。もうしばらく様子見てみます。
jbpb0

2021/09/07 22:45

sudo apt install… を実行する前に sudo apt update を実行してますよね?
xozeit

2021/09/12 01:32

sudo apt updateはやっておりませんでした。 やったらうまくsudo apt install imagemagickできました。 そして無事に元のスクリプトのまま画像を表示できました! ありがとうございました!
guest

回答1

0

自己解決

【まとめ】
(Case1) 書籍のスクリプトのままX環境で表示する場合
①sudo apt update
②sudo apt install imagemagick
その後にスクリプトを実行するだけ。
imagemagickでWSL環境にはないビューワーが追加されることでPILが表示できるようになる。

(Case2)matplotlibで表示する。

def img_show(img): pil_img = Image.fromarray(np.uint8(img)) plt.imshow(pil_img) plt.show()

投稿2021/09/12 01:48

xozeit

総合スコア19

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問