[Solved] Display image in (updated) October R4R?

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:
capture0

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
1 Like

Have you tried the Dexter System Update utility? It refreshes just about everything.

If not, you can see the line in the logs where it looks at pip and decides if it needs to be replaced or not, find the installation candidate package name and then do a re-install.

apt-get install --reinstall [package]

Or, if you want to go totally wonkers on it:

apt-get purge [package]  # uninstalls, deletes, and sweeps up afterwards
apt-get install [package]

which nukes it back to the stone-age and then does a squeaky-clean re-install.

I no longer use any python2.7 so I don’t really care that pip is broken, and I think the correct update for pip is:

pip install -U pip

or 

python2 -m pip install --upgrade pip
1 Like

Fair try.

If you do that, let us know what happens. It would be good information.

Sorry, “There be dragons there”. Pip and Pip3 are strangely connected such that whichever one you do last can mess up the first.

1 Like

Oh, did I hear you say something about not messing with sleeping dragons?

Reminds me of something from way, WAY back. . .

(From the 1970’s game Colossal Cave or Adventure depending on the local system implementation.)

Seems like you need to roll-back to a clean backup. Sorry to hear that.

Been there, done that, bought the T-shirt. (Actually, I have an entire dresser drawer full of those T-shirts.)

1 Like

2 Likes

HOLY CHRISTMAS!

What is that, a map of the Colossal Cave dungeon?

Cool beanies! I’ve never seen that before!! Totally righteous!

The only thing I have that’s even close to that is a copy of the original Digital Computer PDP source files hanging around somewhere. Bitch to port over since they made extensive use of the various hardware and software peculiarities of the PDP architecture to make things work in ways the original designers never imagined.

There are other ports around, just never got my hands on the code.

Totally wizard!

from github.com/troglobit/advent4
unzip, cd into advent4-master, type make, copy advent4 to where you want
./advent4 to run it

AdventureSecretLevel

1 Like

( @mitch.kremm, I’ve heard y’all are feverishly working at the moment. Sorry if this comes at a bad time.)

I just submitted a github issue against Raspbian_For_Robots:

2 Likes

Feverish? Yes. Working? Not much

Good catch! You get the gold bug.

Visual Oxymoron:

1 Like

(Issue, not a bug!)

R4R may not be the culprit. If PiOS comes with matlab installed the issue needs to go against PiOS…

1 Like

Needed show(). Or pause(0.1)

1 Like