Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv2.imread does not read jpg files

Tags:

python

opencv

I am working on a toolbox in Python where I use cv2.imread function to load images.

While I am working with .png files it is OK, but it returns NoneType when I want to read a .jpg file from the same folder.

  1. Why does this happen? and how can I fix it?
  2. How can I read images from a subfolder?

Thanks

import sys
import numpy as np
import os
sys.path.append("/usr/local/lib1/python2.7/site-packages")
import cv2
im1=cv2.imread('pic1.png')
print im1.shape
#output: (512, 512, 3)
im2=cv2.imread('pic1.jpg')
print im2.shape
#output:
-------------------------------------------------------------------------
AttributeError                         Traceback (most recent call last)
<ipython-input-8-2d36ac00eca0> in <module>()
----> 1 print im2.shape
AttributeError: 'NoneType' object has no attribute 'shape'


print cv2.getBuildInformation()

Media I/O: 
ZLib:                        /lib64/libz.so (ver 1.2.8)
JPEG:                        /lib64/libjpeg.so (ver 80)
WEBP:                        /lib64/libwebp.so (ver encoder: 0x0202)
PNG:                         /lib64/libpng.so (ver 1.6.17)
TIFF:                        /lib64/libtiff.so (ver 42 - 4.0.2)
JPEG 2000:                   /lib64/libjasper.so (ver 1.900.1)

Two pictures are in my home folder:

enter image description here

  from os import getcwd, listdir, path
  current_dir = getcwd()
  files = [f for f in listdir('.') if path.isfile(f)]
  print(('Current directory: {c_dir}\n\n'
         'Items in the current directory:\n   {files}').format(
         c_dir=current_dir, 
         files=str.join('\n   ', files)))
  #Output:
  Items in the current directory:
  .node_repl_history
  mysh.sh~
  test.sh
  blender_tofile.sh
  **pic1.jpg**
  rapid.sh
  matlab_crash_dump.8294-1
  .gtk-bookmarks
  any2any
  beethoven.ply
  Face.blend
  Untitled1.ipynb
  sphere1.pbrt
  multirow.log
  .Xauthority
  .gtkrc-2.0-kde4
  Theory and Practice.pdf
  simple_example.gpx~
  pbrt.sh
  blender.sh~
  Untitled4.ipynb
  java.log.3414
  kinect_test.py
  matlab_crash_dump.7226-1
 .bashrc~~
 .ICEauthority
 infoslipsviewer.desktop
 GTW_Global_Numbers.pdf
 index.htm
 Untitled2.ipynb
 **pic1.png**



 os.access('pic1.jpg', os.R_OK)
 #output:
 True
like image 247
Ali Sharifi B. Avatar asked Apr 25 '16 17:04

Ali Sharifi B.


People also ask

Can cv2 read JPEG?

So, OpenCV can always read JPEGs, PNGs, and TIFFs.

Why is cv2 Imread not working?

If you are receiving a NoneType error and your code is calling cv2. imread , then the likely cause of the error is an invalid file path supplied to cv2.

How do I read Imread images?

A = imread( filename ) reads the image from the file specified by filename , inferring the format of the file from its contents. If filename is a multi-image file, then imread reads the first image in the file.

Can cv2 Imread read PNG?

The syntax of imread() function contains a second argument whose default value is cv2. IMREAD_COLOR. Any transparency present in the image is not read. To read PNG images with transparency (alpha) channel, use cv2.


1 Answers

Something is off in your build of cv2. Rebuild it from source, or get it from the package manager.

As a workaround, load jpeg files with matplotlib instead:

>>> import cv2
>>> import matplotlib.pyplot as plt
>>> a1 = cv2.imread('pic1.jpg')
>>> a1.shape
(286, 176, 3)
>>> a2 = plt.imread('pic1.jpg')
>>> a2.shape
(286, 176, 3)

Note that opencv and matplotlib read the colour channels differently by default (one is RGB and one is BGR). So if you rely on the colours at all, you had better swap the first and third channels, like this:

>>> a2 = a2[..., ::-1]  # RGB --> BGR
>>> (a2 == a1).all()
True

Other than that, cv2.imread and plt.imread should return the same results for jpeg files. They both load into 3-channel uint8 numpy arrays.

like image 119
wim Avatar answered Oct 01 '22 10:10

wim