cv2.imread always returns NoneType python 3.6












-1















cv2.imread always returning NoneType.



I have the following code:



path = cv2.imread('D:/XXX/image.jpg')


The array returned is Nonetype



But when I use



path = cv2.imread('image.jpg')


I get the correct array.
The problem only occurs when I use full path.
I have tried backslashes and double slash as well, but it makes no difference. There are many similar questions asked before, I have havent got any suitable answer.
The file path and file name are both correct.



I am using Python version 3.6 adn openCV version 3.4.3



Update
I tried to get the file path using tkinter filedialog.askopenfilename. Yet unsuccessful.










share|improve this question




















  • 2





    I think you should use double backslashes: '\'

    – rockikz
    Nov 16 '18 at 9:55











  • The only thing I can think of is the path is somehow incorrect. Try navigating to it via the command line to check it's validity?

    – Ben Jones
    Nov 16 '18 at 9:56






  • 1





    Use os.path.join("D:", "XXX", "image.jpg") (multiple arguments, one per directory) if you want to be cross-platform compatible and/or are not sure about the exact directory separator syntax.

    – 9769953
    Nov 16 '18 at 9:57











  • @rockikz: alredy tried that.

    – Gaurav Kumar
    Nov 16 '18 at 10:03











  • if the second option works you can check what is your directory with os.getcwd() also you can check if the file exists in such path with os.path.exists(file_path)

    – api55
    Nov 16 '18 at 10:04
















-1















cv2.imread always returning NoneType.



I have the following code:



path = cv2.imread('D:/XXX/image.jpg')


The array returned is Nonetype



But when I use



path = cv2.imread('image.jpg')


I get the correct array.
The problem only occurs when I use full path.
I have tried backslashes and double slash as well, but it makes no difference. There are many similar questions asked before, I have havent got any suitable answer.
The file path and file name are both correct.



I am using Python version 3.6 adn openCV version 3.4.3



Update
I tried to get the file path using tkinter filedialog.askopenfilename. Yet unsuccessful.










share|improve this question




















  • 2





    I think you should use double backslashes: '\'

    – rockikz
    Nov 16 '18 at 9:55











  • The only thing I can think of is the path is somehow incorrect. Try navigating to it via the command line to check it's validity?

    – Ben Jones
    Nov 16 '18 at 9:56






  • 1





    Use os.path.join("D:", "XXX", "image.jpg") (multiple arguments, one per directory) if you want to be cross-platform compatible and/or are not sure about the exact directory separator syntax.

    – 9769953
    Nov 16 '18 at 9:57











  • @rockikz: alredy tried that.

    – Gaurav Kumar
    Nov 16 '18 at 10:03











  • if the second option works you can check what is your directory with os.getcwd() also you can check if the file exists in such path with os.path.exists(file_path)

    – api55
    Nov 16 '18 at 10:04














-1












-1








-1








cv2.imread always returning NoneType.



I have the following code:



path = cv2.imread('D:/XXX/image.jpg')


The array returned is Nonetype



But when I use



path = cv2.imread('image.jpg')


I get the correct array.
The problem only occurs when I use full path.
I have tried backslashes and double slash as well, but it makes no difference. There are many similar questions asked before, I have havent got any suitable answer.
The file path and file name are both correct.



I am using Python version 3.6 adn openCV version 3.4.3



Update
I tried to get the file path using tkinter filedialog.askopenfilename. Yet unsuccessful.










share|improve this question
















cv2.imread always returning NoneType.



I have the following code:



path = cv2.imread('D:/XXX/image.jpg')


The array returned is Nonetype



But when I use



path = cv2.imread('image.jpg')


I get the correct array.
The problem only occurs when I use full path.
I have tried backslashes and double slash as well, but it makes no difference. There are many similar questions asked before, I have havent got any suitable answer.
The file path and file name are both correct.



I am using Python version 3.6 adn openCV version 3.4.3



Update
I tried to get the file path using tkinter filedialog.askopenfilename. Yet unsuccessful.







python python-3.x opencv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 10:59







Gaurav Kumar

















asked Nov 16 '18 at 9:53









Gaurav KumarGaurav Kumar

154




154








  • 2





    I think you should use double backslashes: '\'

    – rockikz
    Nov 16 '18 at 9:55











  • The only thing I can think of is the path is somehow incorrect. Try navigating to it via the command line to check it's validity?

    – Ben Jones
    Nov 16 '18 at 9:56






  • 1





    Use os.path.join("D:", "XXX", "image.jpg") (multiple arguments, one per directory) if you want to be cross-platform compatible and/or are not sure about the exact directory separator syntax.

    – 9769953
    Nov 16 '18 at 9:57











  • @rockikz: alredy tried that.

    – Gaurav Kumar
    Nov 16 '18 at 10:03











  • if the second option works you can check what is your directory with os.getcwd() also you can check if the file exists in such path with os.path.exists(file_path)

    – api55
    Nov 16 '18 at 10:04














  • 2





    I think you should use double backslashes: '\'

    – rockikz
    Nov 16 '18 at 9:55











  • The only thing I can think of is the path is somehow incorrect. Try navigating to it via the command line to check it's validity?

    – Ben Jones
    Nov 16 '18 at 9:56






  • 1





    Use os.path.join("D:", "XXX", "image.jpg") (multiple arguments, one per directory) if you want to be cross-platform compatible and/or are not sure about the exact directory separator syntax.

    – 9769953
    Nov 16 '18 at 9:57











  • @rockikz: alredy tried that.

    – Gaurav Kumar
    Nov 16 '18 at 10:03











  • if the second option works you can check what is your directory with os.getcwd() also you can check if the file exists in such path with os.path.exists(file_path)

    – api55
    Nov 16 '18 at 10:04








2




2





I think you should use double backslashes: '\'

– rockikz
Nov 16 '18 at 9:55





I think you should use double backslashes: '\'

– rockikz
Nov 16 '18 at 9:55













The only thing I can think of is the path is somehow incorrect. Try navigating to it via the command line to check it's validity?

– Ben Jones
Nov 16 '18 at 9:56





The only thing I can think of is the path is somehow incorrect. Try navigating to it via the command line to check it's validity?

– Ben Jones
Nov 16 '18 at 9:56




1




1





Use os.path.join("D:", "XXX", "image.jpg") (multiple arguments, one per directory) if you want to be cross-platform compatible and/or are not sure about the exact directory separator syntax.

– 9769953
Nov 16 '18 at 9:57





Use os.path.join("D:", "XXX", "image.jpg") (multiple arguments, one per directory) if you want to be cross-platform compatible and/or are not sure about the exact directory separator syntax.

– 9769953
Nov 16 '18 at 9:57













@rockikz: alredy tried that.

– Gaurav Kumar
Nov 16 '18 at 10:03





@rockikz: alredy tried that.

– Gaurav Kumar
Nov 16 '18 at 10:03













if the second option works you can check what is your directory with os.getcwd() also you can check if the file exists in such path with os.path.exists(file_path)

– api55
Nov 16 '18 at 10:04





if the second option works you can check what is your directory with os.getcwd() also you can check if the file exists in such path with os.path.exists(file_path)

– api55
Nov 16 '18 at 10:04












1 Answer
1






active

oldest

votes


















-2














You are using it correctly but missed a parameter.
calling cv2.imread(jpeg_file,0) yeild a ndtype array object



however, try this and make sure image file is not zero size.



img = cv2.imread(filename_name,0)


as the doc says



    Use the function cv2.imread() to read an image. The image should be in the working directory or a full path of image should be given.

Second argument is a flag which specifies the way image should be read.

cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
Note Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.
See the code below:

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('messi5.jpg',0)


Cv2.imread usage






share|improve this answer
























  • the second parameter is optional as it can be seen in the documentation. And one should use the constant (i.e. cv2.IMREAD_GRAYSCALE) rather than 0 when it is needed. The default value of the flag is cv2.IMREAD_COLOR

    – api55
    Nov 16 '18 at 10:18













  • This works when the image is in the working directory, but doest wotk when I use the full path.

    – Gaurav Kumar
    Nov 16 '18 at 10:20











  • @api55 perfect, didn't go so deep looking.

    – TheExorcist
    Nov 16 '18 at 10:25











  • Use of magic numbers is an anti-pattern -- this code would not pass even a rudimentary code review. | Changing the second parameter of imread from the default cv2.IMREAD_COLOR to cv2.IMREAD_GRAYSCALE would have no effect on the returned data type. It's always a numpy array (except when loading fails, then it returns None). The only thing it would change is what (potential) conversions are done after the image has been loaded from file -- in this case it would always return a single channel image, as opposed to always returning a 3 channel image.

    – Dan Mašek
    Nov 16 '18 at 13:16











  • I think the community should delete the answer as the answer doesn't fulfill the purpose of asking.

    – TheExorcist
    Nov 17 '18 at 8:12












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335314%2fcv2-imread-always-returns-nonetype-python-3-6%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-2














You are using it correctly but missed a parameter.
calling cv2.imread(jpeg_file,0) yeild a ndtype array object



however, try this and make sure image file is not zero size.



img = cv2.imread(filename_name,0)


as the doc says



    Use the function cv2.imread() to read an image. The image should be in the working directory or a full path of image should be given.

Second argument is a flag which specifies the way image should be read.

cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
Note Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.
See the code below:

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('messi5.jpg',0)


Cv2.imread usage






share|improve this answer
























  • the second parameter is optional as it can be seen in the documentation. And one should use the constant (i.e. cv2.IMREAD_GRAYSCALE) rather than 0 when it is needed. The default value of the flag is cv2.IMREAD_COLOR

    – api55
    Nov 16 '18 at 10:18













  • This works when the image is in the working directory, but doest wotk when I use the full path.

    – Gaurav Kumar
    Nov 16 '18 at 10:20











  • @api55 perfect, didn't go so deep looking.

    – TheExorcist
    Nov 16 '18 at 10:25











  • Use of magic numbers is an anti-pattern -- this code would not pass even a rudimentary code review. | Changing the second parameter of imread from the default cv2.IMREAD_COLOR to cv2.IMREAD_GRAYSCALE would have no effect on the returned data type. It's always a numpy array (except when loading fails, then it returns None). The only thing it would change is what (potential) conversions are done after the image has been loaded from file -- in this case it would always return a single channel image, as opposed to always returning a 3 channel image.

    – Dan Mašek
    Nov 16 '18 at 13:16











  • I think the community should delete the answer as the answer doesn't fulfill the purpose of asking.

    – TheExorcist
    Nov 17 '18 at 8:12
















-2














You are using it correctly but missed a parameter.
calling cv2.imread(jpeg_file,0) yeild a ndtype array object



however, try this and make sure image file is not zero size.



img = cv2.imread(filename_name,0)


as the doc says



    Use the function cv2.imread() to read an image. The image should be in the working directory or a full path of image should be given.

Second argument is a flag which specifies the way image should be read.

cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
Note Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.
See the code below:

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('messi5.jpg',0)


Cv2.imread usage






share|improve this answer
























  • the second parameter is optional as it can be seen in the documentation. And one should use the constant (i.e. cv2.IMREAD_GRAYSCALE) rather than 0 when it is needed. The default value of the flag is cv2.IMREAD_COLOR

    – api55
    Nov 16 '18 at 10:18













  • This works when the image is in the working directory, but doest wotk when I use the full path.

    – Gaurav Kumar
    Nov 16 '18 at 10:20











  • @api55 perfect, didn't go so deep looking.

    – TheExorcist
    Nov 16 '18 at 10:25











  • Use of magic numbers is an anti-pattern -- this code would not pass even a rudimentary code review. | Changing the second parameter of imread from the default cv2.IMREAD_COLOR to cv2.IMREAD_GRAYSCALE would have no effect on the returned data type. It's always a numpy array (except when loading fails, then it returns None). The only thing it would change is what (potential) conversions are done after the image has been loaded from file -- in this case it would always return a single channel image, as opposed to always returning a 3 channel image.

    – Dan Mašek
    Nov 16 '18 at 13:16











  • I think the community should delete the answer as the answer doesn't fulfill the purpose of asking.

    – TheExorcist
    Nov 17 '18 at 8:12














-2












-2








-2







You are using it correctly but missed a parameter.
calling cv2.imread(jpeg_file,0) yeild a ndtype array object



however, try this and make sure image file is not zero size.



img = cv2.imread(filename_name,0)


as the doc says



    Use the function cv2.imread() to read an image. The image should be in the working directory or a full path of image should be given.

Second argument is a flag which specifies the way image should be read.

cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
Note Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.
See the code below:

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('messi5.jpg',0)


Cv2.imread usage






share|improve this answer













You are using it correctly but missed a parameter.
calling cv2.imread(jpeg_file,0) yeild a ndtype array object



however, try this and make sure image file is not zero size.



img = cv2.imread(filename_name,0)


as the doc says



    Use the function cv2.imread() to read an image. The image should be in the working directory or a full path of image should be given.

Second argument is a flag which specifies the way image should be read.

cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
Note Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.
See the code below:

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('messi5.jpg',0)


Cv2.imread usage







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 10:13









TheExorcistTheExorcist

9471215




9471215













  • the second parameter is optional as it can be seen in the documentation. And one should use the constant (i.e. cv2.IMREAD_GRAYSCALE) rather than 0 when it is needed. The default value of the flag is cv2.IMREAD_COLOR

    – api55
    Nov 16 '18 at 10:18













  • This works when the image is in the working directory, but doest wotk when I use the full path.

    – Gaurav Kumar
    Nov 16 '18 at 10:20











  • @api55 perfect, didn't go so deep looking.

    – TheExorcist
    Nov 16 '18 at 10:25











  • Use of magic numbers is an anti-pattern -- this code would not pass even a rudimentary code review. | Changing the second parameter of imread from the default cv2.IMREAD_COLOR to cv2.IMREAD_GRAYSCALE would have no effect on the returned data type. It's always a numpy array (except when loading fails, then it returns None). The only thing it would change is what (potential) conversions are done after the image has been loaded from file -- in this case it would always return a single channel image, as opposed to always returning a 3 channel image.

    – Dan Mašek
    Nov 16 '18 at 13:16











  • I think the community should delete the answer as the answer doesn't fulfill the purpose of asking.

    – TheExorcist
    Nov 17 '18 at 8:12



















  • the second parameter is optional as it can be seen in the documentation. And one should use the constant (i.e. cv2.IMREAD_GRAYSCALE) rather than 0 when it is needed. The default value of the flag is cv2.IMREAD_COLOR

    – api55
    Nov 16 '18 at 10:18













  • This works when the image is in the working directory, but doest wotk when I use the full path.

    – Gaurav Kumar
    Nov 16 '18 at 10:20











  • @api55 perfect, didn't go so deep looking.

    – TheExorcist
    Nov 16 '18 at 10:25











  • Use of magic numbers is an anti-pattern -- this code would not pass even a rudimentary code review. | Changing the second parameter of imread from the default cv2.IMREAD_COLOR to cv2.IMREAD_GRAYSCALE would have no effect on the returned data type. It's always a numpy array (except when loading fails, then it returns None). The only thing it would change is what (potential) conversions are done after the image has been loaded from file -- in this case it would always return a single channel image, as opposed to always returning a 3 channel image.

    – Dan Mašek
    Nov 16 '18 at 13:16











  • I think the community should delete the answer as the answer doesn't fulfill the purpose of asking.

    – TheExorcist
    Nov 17 '18 at 8:12

















the second parameter is optional as it can be seen in the documentation. And one should use the constant (i.e. cv2.IMREAD_GRAYSCALE) rather than 0 when it is needed. The default value of the flag is cv2.IMREAD_COLOR

– api55
Nov 16 '18 at 10:18







the second parameter is optional as it can be seen in the documentation. And one should use the constant (i.e. cv2.IMREAD_GRAYSCALE) rather than 0 when it is needed. The default value of the flag is cv2.IMREAD_COLOR

– api55
Nov 16 '18 at 10:18















This works when the image is in the working directory, but doest wotk when I use the full path.

– Gaurav Kumar
Nov 16 '18 at 10:20





This works when the image is in the working directory, but doest wotk when I use the full path.

– Gaurav Kumar
Nov 16 '18 at 10:20













@api55 perfect, didn't go so deep looking.

– TheExorcist
Nov 16 '18 at 10:25





@api55 perfect, didn't go so deep looking.

– TheExorcist
Nov 16 '18 at 10:25













Use of magic numbers is an anti-pattern -- this code would not pass even a rudimentary code review. | Changing the second parameter of imread from the default cv2.IMREAD_COLOR to cv2.IMREAD_GRAYSCALE would have no effect on the returned data type. It's always a numpy array (except when loading fails, then it returns None). The only thing it would change is what (potential) conversions are done after the image has been loaded from file -- in this case it would always return a single channel image, as opposed to always returning a 3 channel image.

– Dan Mašek
Nov 16 '18 at 13:16





Use of magic numbers is an anti-pattern -- this code would not pass even a rudimentary code review. | Changing the second parameter of imread from the default cv2.IMREAD_COLOR to cv2.IMREAD_GRAYSCALE would have no effect on the returned data type. It's always a numpy array (except when loading fails, then it returns None). The only thing it would change is what (potential) conversions are done after the image has been loaded from file -- in this case it would always return a single channel image, as opposed to always returning a 3 channel image.

– Dan Mašek
Nov 16 '18 at 13:16













I think the community should delete the answer as the answer doesn't fulfill the purpose of asking.

– TheExorcist
Nov 17 '18 at 8:12





I think the community should delete the answer as the answer doesn't fulfill the purpose of asking.

– TheExorcist
Nov 17 '18 at 8:12




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335314%2fcv2-imread-always-returns-nonetype-python-3-6%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine