Ask Your Question

imshow() Python/macOS not showing the image

asked 2019-01-23 17:22:41 -0500

AnnaMP gravatar image

updated 2019-01-24 07:21:53 -0500

I'm literally just starting out, but already stuck .. I'm trying to get an image to display, but imshow() only opens an empty window. I don't get any errors, and the program doesn't hang (pressing a key closes the window just fine) - it's just that there is no image in the window.

I know that the image is loaded correctly, because I'm printing out the arrays and image data looks right. Also I can see that the window always has the correct _measurements_ for whichever image file I'm picking.

import numpy as np
import cv2

img = cv2.imread('butterfly.jpg', 0)

cv2.imshow('Butterfly', img)

Things I have tried (to no avail):

  • using an absolute path instead
  • adding cv2.startWindowThread(); cv2.namedWindow('Butterfly') before the imshow() command
  • reading lots of posts with similar problems

I'm using Python3.6, on macOS (Mojave), cv2 v.4.0.1

Any ideas what I might be doing wrong?

-- Edit -- print(cv2.getBuildInformation()) gives me this:

General configuration for OpenCV 4.0.1 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            /tmp/opencv-20190105-31032-o160to/opencv-4.0.1/opencv_contrib/modules
    Version control (extra):     unknown

    Timestamp:                   2019-01-05T01:41:15Z
    Host:                        Darwin 18.2.0 x86_64
    CMake:                       3.13.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/local/Homebrew/Library/Homebrew/shims/mac/super/gmake
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3 SSSE3 SSE4_1 POPCNT SSE4_2
      requested:                 DETECT
      disabled:                  SSE4_1 SSE4_2 AVX AVX2
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (0 files):          +
      SSE4_2 (0 files):          +
      FP16 (0 files):            + FP16 AVX
      AVX (4 files):             + AVX
      AVX2 (11 files):           + FP16 FMA3 AVX AVX2
      AVX512_SKX (1 files):      + FP16 FMA3 AVX AVX2 AVX_512F AVX512_SKX

    Built as dynamic libs?:      YES
    C++ Compiler:                /usr/local/Homebrew/Library/Homebrew/shims/mac/super/clang++  (ver
    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/local/Homebrew/Library/Homebrew/shims/mac/super/clang
    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden ...
edit retag flag offensive close merge delete


can you check if image is None ?

img = cv2.imread('butterfly.jpg', 0)
if img is None:
    print("Check file path")
LBerger gravatar imageLBerger ( 2019-01-24 01:55:37 -0500 )edit

Thanks for looking into this! But no, it's not None: print(img) gives me the correct array, and I can even do cv2.imwrite('butterfly_bw.jpg',img) and get a saved copy of the bw version.

AnnaMP gravatar imageAnnaMP ( 2019-01-24 06:23:34 -0500 )edit

can you add cv2.namedWindow('Butterfly',cv2.WINDOW_NORMAL) before imshow ?

Does it work in C++?

can you add to your question print(cv2.getBuildInformation())

LBerger gravatar imageLBerger ( 2019-01-24 06:50:40 -0500 )edit

hm, that doesn't make any difference either ... :-( Sorry - overlooked the C++ question - idk ...

AnnaMP gravatar imageAnnaMP ( 2019-01-24 06:56:32 -0500 )edit

there is a close issue here. is it same problem

LBerger gravatar imageLBerger ( 2019-01-24 07:00:55 -0500 )edit

got the cv2.getBuildInformation()) ... but it's a lot ... is there a better way than pasting the whole lot in a comment here? (sorry - forum newb)

^^ I _think_ that issue is different as they only see the window title bar, and it seemed to have been related to problems getting window dimensions. I do get a window with the correct dimensions, just empty

AnnaMP gravatar imageAnnaMP ( 2019-01-24 07:08:08 -0500 )edit

you can add to your question and select text and apply icon 101à10

can you use opencv-python 3.3

LBerger gravatar imageLBerger ( 2019-01-24 07:11:44 -0500 )edit

It seems you build yourself opencv. I don't understand I'm using 'Python3.6, on macOS (Mojave), cv2 v.4.0.1' but in build I can read Interpreter: /usr/local/opt/python/bin/python3 (ver 3.7.2)

LBerger gravatar imageLBerger ( 2019-01-24 07:27:55 -0500 )edit

Er, yes, I did have some weird issues during installation ... I installed with 'brew install opencv' and it got installed into /usr/local/lib/python3.7/site-packages and but when I start python3 it runs Python 3.6 and I didn't know how to start Python 3.7, so I created a symbolic link to /usr/local/opt/opencv/lib/python3.7/site-packages/cv2/python-3.7/ ... (sorry, completely forgot about that once it seemed to be working!) - So I guess I should try to solve that problem some other way ...

AnnaMP gravatar imageAnnaMP ( 2019-01-24 07:44:03 -0500 )edit

I must say that I don't know Mac install procedure. Using cmake you can set python path cmake -D

LBerger gravatar imageLBerger ( 2019-01-24 07:49:38 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2019-01-29 14:05:30 -0500

AnnaMP gravatar image

Update: So it turned out that it was indeed an installation problem. I managed to uninstall Python 3.6, and then with brew link python link to the 3.7 installation that somehow co-existed with 3.6 before. Thanks very much again to @LBerger for spotting the root cause of my issue!

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-01-23 17:22:41 -0500

Seen: 620 times

Last updated: Jan 29