本文介绍了cv2.imread 不读取 jpg 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 开发一个工具箱,我使用 cv2.imread 函数来加载图像.

当我使用 .png 文件时,它没问题,但是当我想从中读取 .jpg 文件时它返回 NoneType同一个文件夹.

  1. 为什么会发生这种情况?我该如何解决?
  2. 如何从子文件夹中读取图像?

谢谢

导入系统将 numpy 导入为 np导入操作系统sys.path.append("/usr/local/lib1/python2.7/site-packages")导入 cv2im1=cv2.imread('pic1.png')打印 im1.shape#output: (512, 512, 3)im2=cv2.imread('pic1.jpg')打印 im2.shape#输出:-------------------------------------------------------------------------AttributeError 回溯(最近一次调用最后一次)<ipython-input-8-2d36ac00eca0>在 <module>()---->1 打印 im2.shapeAttributeError: 'NoneType' 对象没有属性 'shape'打印 cv2.getBuildInformation()媒体输入/输出:ZLib:/lib64/libz.so(1.2.8 版)JPEG:/lib64/libjpeg.so(80 版)WEBP:/lib64/libwebp.so(版本编码器:0x0202)PNG:/lib64/libpng.so(1.6.17 版)TIFF:/lib64/libtiff.so(版本 42 - 4.0.2)JPEG 2000:/lib64/libjasper.so(1.900.1 版)

我的主文件夹中有两张图片:

 from os import getcwd, listdir, pathcurrent_dir = getcwd()files = [f for f in listdir('.') if path.isfile(f)]print(('当前目录:{c_dir}

''当前目录中的项目:
 {files}').format(c_dir=current_dir,files=str.join('
', files)))#输出:当前目录中的项目:.node_repl_history我的.sh~测试文件Blender_tofile.sh**图片1.jpg**快速文件matlab_crash_dump.8294-1.gtk-书签任何2任何贝多芬.ply面部混合无题1.ipynbsphere1.pbrt多行日志.X权限.gtkrc-2.0-kde4理论与实践.pdfsimple_example.gpx~文件搅拌机.sh~无题4.ipynbjava.log.3414kinect_test.pymatlab_crash_dump.7226-1.bashrc~~.ICE权威infoslipsviewer.desktopGTW_Global_Numbers.pdf索引.htm无题2.ipynb**图片1.png**os.access('pic1.jpg', os.R_OK)#输出:真的
解决方案

在您的 cv2 构建中出现问题.从源代码重建它,或者从包管理器中获取它.

作为一种解决方法,使用 matplotlib 加载 jpeg 文件:

>>>导入 cv2>>>导入 matplotlib.pyplot 作为 plt>>>a1 = cv2.imread('pic1.jpg')>>>a1.shape(286, 176, 3)>>>a2 = plt.imread('pic1.jpg')>>>a2.shape(286, 176, 3)

请注意,默认情况下,opencv 和 matplotlib 读取颜色通道的方式不同(一个是 RGB,一个是 BGR).所以如果你完全依赖颜色,你最好交换第一个和第三个通道,像这样:

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

除此之外,对于 jpeg 文件,cv2.imreadplt.imread 应该返回相同的结果.它们都加载到 3 通道 uint8 numpy 数组中.

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:

  from os import getcwd, listdir, path
  current_dir = getcwd()
  files = [f for f in listdir('.') if path.isfile(f)]
  print(('Current directory: {c_dir}

'
         'Items in the current directory:
   {files}').format(
         c_dir=current_dir,
         files=str.join('
   ', 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
解决方案

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.

这篇关于cv2.imread 不读取 jpg 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 14:47