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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Python

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

Q&A

解決済

2回答

6477閲覧

pythonで画像表示

O.K.

総合スコア30

Python

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

0グッド

0クリップ

投稿2016/11/10 14:21

pythonでPILを使用して画像を表示させようとしていますが、できません。
プログラムと画像は両方デスクトップに置いてあり
macのターミナルから実行しています。

以下ソースです.

python

1from PIL import Image 2im = Image.open("imgres.jpg") 3im.show()

実行しようとすると
Traceback (most recent call last):
File "image.py", line 3, in <module>
img = Image.open('imgres.jpg')
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 1952, in open
fp = builtin.open(fp, "rb")
IOError: [Errno 2] No such file or directory: 'imgres.jpg'
oks:Desktop ok$ python image.py
Traceback (most recent call last):
File "image.py", line 3, in <module>
im.show()
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 1483, in show
_show(self, title=title, command=command)
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 2123, in _show
apply(_showxv, (image,), options)
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 2127, in _showxv
apply(ImageShow.show, (image, title), options)
File "/Library/Python/2.7/site-packages/PIL/ImageShow.py", line 41, in show
if viewer.show(image, title=title, **options):
File "/Library/Python/2.7/site-packages/PIL/ImageShow.py", line 66, in show
self.show_image(image, **options)
File "/Library/Python/2.7/site-packages/PIL/ImageShow.py", line 85, in show_image
return self.show_file(self.save_image(image), **options)
File "/Library/Python/2.7/site-packages/PIL/ImageShow.py", line 81, in save_image
return image._dump(format=self.get_format(image))
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 488, in _dump
self.load()
File "/Library/Python/2.7/site-packages/PIL/ImageFile.py", line 189, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 385, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available
と、なります。

どうすれば表示できますか?

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

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

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

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

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

guest

回答2

0

ベストアンサー

’プログラムと画像は両方デスクトップに置いてあり ’とあるから混乱されていると思います。
yonaさんも指摘されていますが、Pathが異なっているから読めない。
エラーにも’IOError: [Errno 2] No such file or directory: 'imgres.jpg' と表示されているので、適当な場所に移動されてFullPathで指定する事を勧めます。

投稿2016/11/10 14:52

MasahikoHirata

総合スコア3747

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

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

O.K.

2016/11/10 15:01

pathというのがよくわからないのですが、 Documentsにpythonというフォルダを作って、そこに画像とプログラムファイルを入れてから 実行すると’IOError: [Errno 2] No such file or directory: 'imgres.jpg'というのは消えましたが、 Traceback (most recent call last): File "image.py", line 3, in <module> im.show() File "/Library/Python/2.7/site-packages/PIL/Image.py", line 1483, in show _show(self, title=title, command=command) File "/Library/Python/2.7/site-packages/PIL/Image.py", line 2123, in _show apply(_showxv, (image,), options) File "/Library/Python/2.7/site-packages/PIL/Image.py", line 2127, in _showxv apply(ImageShow.show, (image, title), options) File "/Library/Python/2.7/site-packages/PIL/ImageShow.py", line 41, in show if viewer.show(image, title=title, **options): File "/Library/Python/2.7/site-packages/PIL/ImageShow.py", line 66, in show self.show_image(image, **options) File "/Library/Python/2.7/site-packages/PIL/ImageShow.py", line 85, in show_image return self.show_file(self.save_image(image), **options) File "/Library/Python/2.7/site-packages/PIL/ImageShow.py", line 81, in save_image return image._dump(format=self.get_format(image)) File "/Library/Python/2.7/site-packages/PIL/Image.py", line 488, in _dump self.load() File "/Library/Python/2.7/site-packages/PIL/ImageFile.py", line 189, in load d = Image._getdecoder(self.mode, d, a, self.decoderconfig) File "/Library/Python/2.7/site-packages/PIL/Image.py", line 385, in _getdecoder raise IOError("decoder %s not available" % decoder_name) IOError: decoder jpeg not available と出てきました。 これはどうすればいいでしょうか?
MasahikoHirata

2016/11/10 15:05

Path=フォルダーと考えて構わないと思います。エラーの内容はライブラリーが見つからない(ライブラリーがあるPathにリンクが通ってないと考えられます。
O.K.

2016/11/10 15:06

リンクを通すにはどうすればいいですか?
MasahikoHirata

2016/11/10 15:10

まずパスがどうなっているかを確認。 例: $ python Python 2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path # パスを確認する ['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7'] >>> exit() # 終了 $ これは表示例ですが、pythonを起動して、 >>> import sys >>> sys.path # パスを確認する と入力します。
O.K.

2016/11/10 15:12

画像をjpgからpngに変更したらうまくいきました
katsuko

2016/11/10 15:18

あぁ、コメントではリンクは有効にならないのですね。ごめんなさい。
O.K.

2016/11/10 15:28

libjpeg入ってませんでした
guest

0

pathが異なるからですね。プログラムファイルと同階層にファイルを移動してから実行してみてください。

投稿2016/11/10 14:30

yona

総合スコア18155

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

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

O.K.

2016/11/10 14:48

すいません。 もう少し具体的にお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問