UPDATE: SOLVED - missing important line of code in matplotlib program - below edited to work.
I want to display a pi camera image and thought I might use matplotlib since it is installed.
It doesn’t complain of errors.
It claims to be using TkAgg as the background, but my
mplplt.imshow(image)
does not display a window.
Pyimagesearch claims this is because of missing dependancies - ideas?
Any other way to display an image?
(I also tried PIL - no luck. pygame works but is a painful work-around.)
Attempt using Pillow/PIL
pip3 freeze | grep Pillow
**Pillow** ==5.4.1
#!/usr/bin/env python3
# file: pil_test.py
from PIL import Image
import numpy as np
from time import sleep
im = Image.open('capture0.jpg')
im.show()
sleep(5)
File capture0.jpg:
Attempt using matplotlib:
#!/usr/bin/env python3
# file: mpl_test.py
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from time import sleep
# plt.ion()
print("matplotlib.get_backend():", plt.get_backend())
img = mpimg.imread('capture0.jpg')
print("image.shape:", img.shape)
imgplot = plt.imshow(img)
plt.show() # <--- UPDATE - this makes it work
sleep(5)
Using pygame works but ugly…
#!/usr/bin/env python3
# file: pygame_test.py
from PIL import Image
import numpy as np
from time import sleep
import pygame
pygame.init()
clk = pygame.time.Clock()
im = np.array(Image.open('capture0.jpg'))
win = pygame.display.set_mode((im.shape[1],im.shape[0]))
img = pygame.image.load('capture0.jpg')
while True:
try:
win.blit(img,(0,0))
pygame.display.flip()
clk.tick(3)
sleep(5)
exit(0)
except KeyboardInterrupt:
print("\nExiting")
break
BTW, on my system “pip” seems to be messed up:
$ python --version
Python 2.7.16
$ pip3 --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
$ python3 --version
Python 3.7.3
$ pip
Traceback (most recent call last):
File "/usr/bin/pip", line 9, in <module>
from pip._internal import main
File "/usr/lib/python2.7/dist-packages/pip/_internal/__init__.py", line 19, in <module>
from pip._vendor.urllib3.exceptions import DependencyWarning
File "/usr/lib/python2.7/dist-packages/pip/_vendor/__init__.py", line 64, in <module>
vendored("cachecontrol")
File "/usr/lib/python2.7/dist-packages/pip/_vendor/__init__.py", line 36, in vendored
__import__(modulename, globals(), locals(), level=0)
File "/usr/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachecontrol/__init__.py", line 9, in <module>
File "/usr/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachecontrol/wrapper.py", line 1, in <module>
File "/usr/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachecontrol/adapter.py", line 4, in <module>
File "/usr/share/python-wheels/requests-2.21.0-py2.py3-none-any.whl/requests/__init__.py", line 95, in <module>
File "/usr/share/python-wheels/urllib3-1.24.1-py2.py3-none-any.whl/urllib3/contrib/pyopenssl.py", line 46, in <module>
File "/usr/lib/python2.7/dist-packages/OpenSSL/__init__.py", line 8, in <module>
from OpenSSL import crypto, SSL
File "/usr/lib/python2.7/dist-packages/OpenSSL/crypto.py", line 12, in <module>
from cryptography import x509
File "/usr/lib/python2.7/dist-packages/cryptography/x509/__init__.py", line 8, in <module>
from cryptography.x509.base import (
File "/usr/lib/python2.7/dist-packages/cryptography/x509/base.py", line 16, in <module>
from cryptography.x509.extensions import Extension, ExtensionType
File "/usr/lib/python2.7/dist-packages/cryptography/x509/extensions.py", line 24, in <module>
from cryptography.x509.general_name import GeneralName, IPAddress, OtherName
File "/usr/lib/python2.7/dist-packages/cryptography/x509/general_name.py", line 16, in <module>
from cryptography.x509.name import Name
File "/usr/lib/python2.7/dist-packages/cryptography/x509/name.py", line 28, in <module>
_ASN1_TYPE_TO_ENUM = dict((i.value, i) for i in _ASN1Type)
TypeError: 'type' object is not iterable