# OpenCV-Python – How to install OpenCV-Python package to Anaconda (Windows)

## 41 thoughts on “OpenCV-Python – How to install OpenCV-Python package to Anaconda (Windows)”

1. Anonymous says:

Thank you for the instruction! I will now be checking if I can properly integrate it and use it in my research.

1. Johnny says:

Thanks for the kind words. By the way I have just done a massive update on this post. Please check the revised instruction again! (the old version was more like a hack. This new version is more “official”, require less work, and more consistent). Good luck!

1. Johnny says:

Thank you for the encouraging words Shiva! I hope this post has helped you setting up OpenCV & FFMPEG on Anaconda – it took me 3 days doing trial-and-errors and I really hope that it would take the others much less time in getting things setup :)

2. Warren says:

I believe you mean the second and third lines in test.py to read:

cap=cv2.VideoCapture("input_video.mp4")
print cap.isOpened()   # True = read video successfully. False - fail to read video.


The cv attribute and the CV_FOURCC macro are missing from opencv 3.0, replaced with VideoWriter::fourcc() (in the C++ implementation, at least). Haven’t got that figured out yet either.

Still haven’t got it to read a test video, but I’m only a couple of hours in :)

1. Johnny says:

Hey Warren – thanks and do let us know how you get on! (at the time of testing the Python code it worked fine on my machine… things might have changed since then though I am not sure – might try out your suggestion later!) Thanks and good luck!!!

3. Pete says:

thanks for the instructions, saved me a few hours of mucking around, very clear and very appreciated

here are a couple of minor changes to your script to test FFMPEG,

import cv2
cap=cv2.VideoCapture("input_video.mp4")
print cap.isOpened()   # True = read video successfully. False - fail to read video.

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter("output_video.avi",fourcc, 20.0, (640,360))
print out.isOpened()  # True = write out video successfully. False - fail to write out video.

cap.release()
out.release()


note that your original script fails at a couple of points

(1) in lines 2 and 3 you need to define a handle/(more properly an instance) to cv2.VideoCapture() and test whether that handle points to an open object, (in your code you’re testing whether the library cv2 is an open object)

(2) as one of the other chaps pointed out FOUR_CC has been moved, and the above syntax appears to work

thanks again

1. Johnny says:

Thank you Pete! I have replaced the code in the article with yours – I have not tested this myself though. Please let us know how your test go – I’m sure this will benefit everybody! (I’m surprised this article is now one of the hottest on this site – how exciting :)

4. Junjie Liao says:

Hi Johnny, thanks for your instructions here. It’s really helpful and I appreciate it. I only have one question for you, so everything works if I use spyder to run the test codes. However, I try to use VS 2013 as my IDE, and I can import opencv correctly, however, it failed the test of ffmpeg. So do you have any suggestions of it? Thanks.

1. Johnny says:

Hey Junjie – I only tested this with Anaconda Spyder IDE. (I hadn’t tried this with Visual Studio). Anyone can hep out Junjie here? :)

1. Junjie Liao says:

Hi Johnny! I’m guess that VS doesn’t recognize the path variable: %OPENCV_DIR%. So it might solve the problem by just adding the variable inside the VS’s setting. However, I’m not able to try it out myself since I have no idea what I should add… Anyway, if you have time, see if you can test it to prove my guess. Thanks.

5. joseyukio says:

Thank you. Nice job here.

1. Johnny says:

Thanks @joseyukio – I hope it works for you! :)

6. Almazi says:

Successfully done! To add a small note: If you are seeing “False” on first line when you are doing FFMPEG codec test but you have done everything correctly and you supposed to get a “True”::: Check the sample video name, and try “input_video” instead of “input_video.mp4”. Sometimes the extension mp4 is not needed to add as your system can detect the app type automatically so when you use “input_video.mp4” it turns into “input_video.mp4.mp4”. This is the issue I was facing and solved.

1. Johnny says:

Excellent I’m glad you’ve got it working! And thank you so much for sharing your experience and tips – this will definitely help out lots. Thank you again!!!

7. Pala Rulec says:

I followed your instructions but, i cannot import opencv. I get this error everytime: DLL load failed with error code -1073741795

8. Nico says:

Same here, I get “ImportError: DLL load failed”. Followed all your steps… can you help me please?

9. Thankyou for the solution it saved me and my team a lot of time.

1. Johnny says:

Great stuff Rakesh! I’m glad the instruction has helped!

1. Johnny says:

hello world.

10. Thanks a lot – this worked really smoothly! The only issue I had was that I had been testing it in a console window that I had opened before adding the OPENCV_DIR and adding to PATH. Whilst the cv2 import worked fine, the cap.isOpened() always returned false. What fixed it was simply opening a new console window which then knew the updated PATH variable.

1. Johnny says:

Glad to hear Mario! And thanks for sharing your experience / tips! Have fun with OpenCV! :-)

11. qian cai says:

I have tried out your test.py, but I got the result of 2 falses. I don’t know why. And I have got a output_video.avi, but it is 0 byte! Can you explain it to me? Thank you in advance.

1. Make sure to save your .py file in the same directory as your test video – and of course make sure the name in the code matches the file name.

12. In the test code I needed to add parentheses around the print statements. Otherwise it worked great:

import cv2
cap = cv2.VideoCapture(“test.mp4”)
print (cap.isOpened()) # True = read video successfully. False – fail to read video.

fourcc = cv2.VideoWriter_fourcc(*’XVID’)
out = cv2.VideoWriter(“output_video.avi”, fourcc, 20.0, (640, 360))
print (out.isOpened()) # True = write out video successfully. False – fail to write out video.

cap.release()
out.release()

13. Anonymous says:

Hi !!!

Thanks you for this tutorial.This was my problem (now fixed up): DON’T USE SPACE IN ROUTES (Ex: C:\My Folder\opencv). This isn’t a good idea, use this other route and you will get a TRUE: C:\MyFolder\opencv

:D Thanksssss

1. Johnny says:

Excellent! thanks for sharing :-)

14. The code you posted prints out true,true but the output_video.avi is an empty 6kb file.
while True:
out.write(frame)
cv2.imshow(‘frame’,frame)
if cv2.waitKey(1) & 0xFF ==ord(‘q’):
break

after the line
print out.isOpened() # True = write out video successfully. False – fail to write out video.
writes out the video to the file.

15. btw if a comment exceeds 12 lines, the post button scrolls off the bottom of the page

16. Paulo says:

Hello All,
I follow the tutorial and were very good success, but one thing is missing and need some help. When i make an new .py file and make import cv2 and execute says no module cv2 found. If i go to python console and do the same works. What i’am missing ???

Thanks for great job

17. Anonymous says:

It worked!!! Thank you Mr. Johnny Chan

import cv2
cap = cv2.VideoCapture(“ball_tracking.mp4”)
warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:578)
cap = cv2.VideoCapture(“ball_tracking_example.mp4”)
print cap.isOpened()
True
fourcc = cv2.VideoWriter_fourcc(*’XVID’)
out = cv2.VideoWriter(“output_video.avi”, fourcc, 20.0, (640, 360))
print out.isOpened()
True
cap.release()
out.release()

1. Johnny says:

Great to hear! :-)

18. Anonymous says:

Works like a charm. Thanks for writing it out in such detail. The exact steps are mentioned which makes the installation so much easier.

1. Johnny says:

Excellent thanks! :-)

19. flyer1998 says:

Thank you so much for this! I had been struggling for two whole days trying figure out how exactly to do this when I stumbled upon this fantastic post. You saved me an extra day. XD

20. Anonymous says:

Worked like a charm for me
conda install -c menpo opencv3

21. Anonymous says:

Thank you so much. Works very good.

22. Anonymous says:

Thank you very much! works like a charm

23. Jia says:

hi My computer is Win10, Anaconda3 and I got an error:

import cv2
Traceback (most recent call last):
File “”, line 1, in