1

So i setup OpenCV and python3 on my mac using the guide here: http://www.pyimagesearch.com/2016/12/05/macos-install-opencv-3-and-python-3-5/#comment-427533

In one of his guide, he has an example code:

from __future__ import print_function
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "From Terminal")
args = vars(ap.parse_args())


image = cv2.imread(args["image"])
cv2.imwrite("./Output/image1.jpg", image)
cv2.imshow("Test_imShow", image)
cv2.waitKey(0)

After i execute it in terminal with

python read_n_write.py --image ./Resource/Me.jpg

while the imwrite function successfully creates a duplicate of the read image, imshow failed to display it in the window. There's no background, just a floating top window bar. enter image description here

I'm really confused here, after a bit of google, i've tried the suggested solution

cv2.startWindowThread()
cv2.namedWindow("test")

Both won't work, any suggestions would be appreciated, thank you.


Edit 1: Big thanks to api55. I used matplotlib

plt.imshow(canvas, interpolation='nearest')
plt.show()

And it totally worked, still have no idea why cv2.imshow isn't working. So i will leave this question open.

libra
  • 673
  • 1
  • 10
  • 30
  • If you've installed Python 3 then why are you using `from __future__ import print_function`? This is for the newest versions of Python 2 to use the `print()` function from Python 3. Additionally, check `which python` to see what version of Python terminal is executing. – alkasm Jun 17 '17 at 11:21
  • If you installed `python3`, shouldn't you invoke `python3 read_n_write.py --image ./Resource/Me.jpg`? What does `python -V` give you and `python3 -V`? – Mark Setchell Jun 17 '17 at 11:38
  • @MarkSetchell, i tried python3 read_n_write...., same result. python -V and python3 -V both give me python 3.6.1 – libra Jun 17 '17 at 11:46
  • @AlexanderReynolds, in the book it mentioned: "Throughout this book you’ll see us importing the print_ function from the __future__ package. We’ll be using the actual print() function rather than the print statement so that our code will work with both Python 2.7 and Python 3". And by checking python verions, it is python 3.6.1 – libra Jun 17 '17 at 11:48
  • 1
    Your screenshot looks kind of weird, the new window should not be call Image, but Test_imShow as in your code. If it is called Image, something is completely wrong :/ either the code is not the one shown here or the opencv has this by default and is somehow skipping the instructions. You can try plotting it [with matplotlib](http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_image_display/py_image_display.html), to check if indeed the problem is openCV imshow function. – api55 Jun 17 '17 at 12:13
  • @api55, thank you i just tested with matplotlib code: plt.imshow(canvas, interpolation='nearest') ||| plt.show() It worked – libra Jun 17 '17 at 14:09
  • good, so this tells you that something with the imshow function is not working good. However your code is quite simple, so I am out of ideas of why it may not work. Perhaps a version problem, or if you got them from source, perhaps there is a bug? – api55 Jun 17 '17 at 14:17
  • @api55 yes, I agree, I posted this question to the author of the guide I mentioned, hoping he might find a solution. – libra Jun 17 '17 at 14:36
  • 1
    You may also try the github issues section, since this may be a bug (maybe it is even reported already) – api55 Jun 17 '17 at 14:57
  • Curious, does it fail if you make the argument positional instead of optional? I.e. `ap.add_argument('image', help='From Terminal')` and then calling with `python read_n_write.py ./Resource/Me.jpg`? – alkasm Jun 18 '17 at 16:08
  • @AlexanderReynolds . I tried. It says [[[ usage: load_display_save.py [-h] image load_display_save.py: error: unrecognized arguments: --image ]]] – libra Jun 20 '17 at 04:20
  • I ran into a similar issue a while ago and [this](https://stackoverflow.com/a/44553641/4962554) was my solution – eshirima Jun 27 '17 at 22:03

0 Answers0