使用PIL打开图片时无法通过cv2(opencv 4.4)加载图片

尼古拉斯GZ

我有一堆图片,可以使用多种方法完美加载,例如Image.open。但是当涉及到opencv时,我什么也没得到:

from PIL import Image
import os
import cv2

 for fi in os.listdir():
            image = cv2.imread(fi)
            print(image)

            image = Image.open(fi)
            print(image)
            print(np.array(image.getdata()))

返回:

None
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=1920x1080 at 0x8AF2A30>
[[31 63 84]
 [31 63 84]
 [31 63 84]
 ...
 [16 16 16]
 [16 16 16]
 [16 16 16]]

无论我为imread的第二个参数提供什么值,它都保持不变。给出明确的路径也不会带来任何改变。

我的文件是1920 * 1080 RGB png文件,我正在使用python 3.7.3和opencv 4.4.0.44。

谢谢你的帮助

编辑1:

General configuration for OpenCV 4.4.0 =====================================
  Version control:               4.4.0

  Platform:
    Timestamp:                   2020-09-23T00:00:53Z
    Host:                        Windows 6.3.9600 AMD64
    CMake:                       3.18.2
    CMake generator:             Visual Studio 14 2015
    CMake build tool:            C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe
    MSVC:                        1900

  CPU/HW features:
    Baseline:                    SSE SSE2
      requested:                 SSE2
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX
      requested:                 SSE4_1 SSE4_2 AVX FP16
      SSE4_1 (15 files):         + SSE3 SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (0 files):            + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (4 files):             + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 AVX

  C/C++:
    Built as dynamic libs?:      NO
    C++ standard:                11
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe  (ver 19.0.24241.7)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise  /arch:SSE /arch:SSE2 /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP  /MT /O2 /Ob2 /DNDEBUG 
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise  /arch:SSE /arch:SSE2 /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP  /MTd /Zi /Ob0 /Od /RTC1 
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise  /arch:SSE /arch:SSE2 /MP   /MT /O2 /Ob2 /DNDEBUG 
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise  /arch:SSE /arch:SSE2 /MP /MTd /Zi /Ob0 /Od /RTC1 
    Linker flags (Release):      /machine:X86  /NODEFAULTLIB:atlthunk.lib /INCREMENTAL:NO  /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:libcpmtd.lib /NODEFAULTLIB:msvcrtd.lib
    Linker flags (Debug):        /machine:X86  /NODEFAULTLIB:atlthunk.lib /debug /INCREMENTAL  /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:libcpmt.lib /NODEFAULTLIB:msvcrt.lib
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          ade wsock32 comctl32 gdi32 ole32 setupapi ws2_32
    3rdparty dependencies:       ittnotify libprotobuf zlib libjpeg-turbo libwebp libpng libtiff libjasper IlmImf quirc ippiw ippicv

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 java js python2 ts
    Applications:                -
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI: 
    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.2.11)
    JPEG:                        build-libjpeg-turbo (ver 2.0.5-62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.0.10)
    JPEG 2000:                   build Jasper (ver 1.900.1)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.54.100)
      avformat:                  YES (58.29.100)
      avutil:                    YES (56.31.100)
      swscale:                   YES (5.5.100)
      avresample:                YES (4.0.0)
    GStreamer:                   NO
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      NO

  Parallel framework:            Concurrency

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2020.0.0 Gold [2020.0.0]
           at:                   C:/Users/appveyor/AppData/Local/Temp/1/pip-req-build-4q0kz99f/_skbuild/win32-3.7/cmake-build/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2020.0.0)
              at:                C:/Users/appveyor/AppData/Local/Temp/1/pip-req-build-4q0kz99f/_skbuild/win32-3.7/cmake-build/3rdparty/ippicv/ippicv_win/iw
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  OpenCL:                        YES (NVD3D11)
    Include path:                C:/Users/appveyor/AppData/Local/Temp/1/pip-req-build-4q0kz99f/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 C:/Python37/python.exe (ver 3.7.5)
    Libraries:                   C:/Python37/libs/python37.lib (ver 3.7.5)
    numpy:                       C:/Users/appveyor/AppData/Local/Temp/1/pip-build-env-5cllkua4/overlay/Lib/site-packages/numpy/core/include (ver 1.14.5)
    install path:                python

  Python (for build):            C:/Python27/python.exe

  Java:                          
    ant:                         NO
    JNI:                         C:/Program Files/Java/jdk1.8.0/include C:/Program Files/Java/jdk1.8.0/include/win32 C:/Program Files/Java/jdk1.8.0/include
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    C:/Users/appveyor/AppData/Local/Temp/1/pip-req-build-4q0kz99f/_skbuild/win32-3.7/cmake-install

编辑2


File: ... (1273217 bytes)
  chunk IHDR at offset 0x0000c, length 13
    1920 x 1080 image, 24-bit RGB, non-interlaced
  chunk pHYs at offset 0x00025, length 9: 11811x11811 pixels/meter (300 dpi)
  chunk IDAT at offset 0x0003a, length 65536
    zlib: deflated, 32K window, default compression
  chunk IDAT at offset 0x10046, length 65536
  chunk IDAT at offset 0x20052, length 65536
  chunk IDAT at offset 0x3005e, length 65536
  chunk IDAT at offset 0x4006a, length 65536
  chunk IDAT at offset 0x50076, length 65536
  chunk IDAT at offset 0x60082, length 65536
  chunk IDAT at offset 0x7008e, length 65536
  chunk IDAT at offset 0x8009a, length 65536
  chunk IDAT at offset 0x900a6, length 65536
  chunk IDAT at offset 0xa00b2, length 65536
  chunk IDAT at offset 0xb00be, length 65536
  chunk IDAT at offset 0xc00ca, length 65536
  chunk IDAT at offset 0xd00d6, length 65536
  chunk IDAT at offset 0xe00e2, length 65536
  chunk IDAT at offset 0xf00ee, length 65536
  chunk IDAT at offset 0x1000fa, length 65536
  chunk IDAT at offset 0x110106, length 65536
  chunk IDAT at offset 0x120112, length 65536
  chunk IDAT at offset 0x13011e, length 27727
  chunk IEND at offset 0x136d79, length 0
No errors detected in ... (23 chunks, 79.5% compression).

编辑3

Image:
  Filename: ...png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 1920x1080+0+0
  Resolution: 118.11x118.11
  Print size: 16.256x9.14402
  Units: PixelsPerCentimeter
  Colorspace: sRGB
  Type: TrueColor
  Base type: Undefined
  Endianness: Undefined
  Depth: 8-bit
  Channel depth:
    Red: 8-bit
    Green: 8-bit
    Blue: 8-bit
  Channel statistics:
    Pixels: 2073600
    Red:
      min: 0  (0)
      max: 255 (1)
      mean: 88.4175 (0.346735)
      standard deviation: 48.6837 (0.190916)
      kurtosis: 2.88645
      skewness: 1.60217
      entropy: 0.667925
    Green:
      min: 0  (0)
      max: 255 (1)
      mean: 78.7912 (0.308985)
      standard deviation: 52.3865 (0.205437)
      kurtosis: 2.33529
      skewness: 1.54681
      entropy: 0.672343
    Blue:
      min: 0  (0)
      max: 255 (1)
      mean: 134.848 (0.528816)
      standard deviation: 81.9142 (0.321232)
      kurtosis: -1.36843
      skewness: -0.507794
      entropy: 0.668643
  Image statistics:
    Overall:
      min: 0  (0)
      max: 255 (1)
      mean: 100.686 (0.394846)
      standard deviation: 60.9948 (0.239195)
      kurtosis: -0.758096
      skewness: 0.627416
      entropy: 0.669637
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Matte color: grey74
  Background color: white
  Border color: srgb(223,223,223)
  Transparent color: none
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 1920x1080+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Convex hull: 0,0 1919,0 1919,1039 1915,1079 104,1079 17,1066 16,1065 0,1039 0,0
  Minimum bounding box: 1919,0 1919,1079 0,1079 0,0
  Properties:
    date:create: 2020-10-21T19:06:24+00:00
    date:modify: 2020-10-21T19:22:08+00:00
    minimum-bounding-box:_p: 0,0
    minimum-bounding-box:_q: 1919,0
    minimum-bounding-box:_v: 1915,1079
    minimum-bounding-box:angle: 0
    minimum-bounding-box:area: 2.0706e+06
    minimum-bounding-box:height: 1919
    minimum-bounding-box:unrotate: -0
    minimum-bounding-box:width: 1079
    png:IHDR.bit-depth-orig: 8
    png:IHDR.bit_depth: 8
    png:IHDR.color-type-orig: 2
    png:IHDR.color_type: 2 (Truecolor)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 1920, 1080
    png:pHYs: x_res=11811, y_res=11811, units=1
    png:sRGB: intent=0 (Perceptual Intent)
    signature: bcf111b6c823a95bd31bbec26225847dffa81c1a07cbf95cf07396f5404ffa5c
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 1.21423MiB
  Number pixels: 2073600
  Pixels per second: 28.8118MP
  User time: 0.063u
  Elapsed time: 0:01.071
  Version: ImageMagick 7.0.10-34 Q16 x64 2020-10-08 http://www.imagemagick.org
尼古拉斯GZ

它不能解决无法解决的问题,但是我找到了解决该问题的方法:

    image = Image.open(fi)
    im_arr = np.array(image.getdata(), dtype=np.uint16).reshape(1080, 1920, 3)
    im_arr = np.vectorize(lambda x: x/255)(im_arr)
    cv2.imshow("image", im_arr)
    cv2.waitKey()

我将进一步调查该问题。

编辑:

尽管读取和写入过程占用了时间和空间,但是这似乎仍然可行并且更加令人满意:

    image = Image.open(fi)
    im_arr = np.array(image.getdata()).reshape(1080, 1920, 3)
    cv2.imwrite(os.path.join(path_data, "image_data_clean", rep, fi), im_arr)
    image = cv2.imread(os.path.join(path_data, "image_data_clean", rep, fi))

在那种情况下,我确实不需要类型声明和映射(但仍然需要重塑)

编辑2:

为了探索文件可能已损坏的可能性,我从源头开始将它们重新压缩。我粗心地将它们放在同一位置,因此我丢失了先前的内容,因此有进行比较的可能性。但是,当我在新文件上运行时:

    test = cv2.imread(os.path.join(path_data, "image_data", rep, fi))
    print(test)
    im_arr = np.array(Image.open(os.path.join(path_data, "image_data", rep, fi)))
    print(im_arr.shape)
    print(im_arr.dtype)
    cv2.imshow("image", im_arr)
    cv2.waitKey()
>>>
None
(1080, 1920, 3)
uint8

然后,我现在得到了shape和dtype的预期答案,并且imshow可以完美地工作,而无需重塑/声明类型!我真的确定今天上午不是这种情况,所以一定是文件有问题-或我的大脑-而不是PIL

但是,直接尝试进行读操作仍然可悲地失败

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用PIL打开图片时无法通过cv2(opencv 4.4)加载图片

来自分类Dev

某些Webp图片无法在Android 4+中加载

来自分类Dev

Ionic 2 / Ionic 3 / Ionic 4:(懒惰)正在加载微调器以获取图片

来自分类Dev

webpack 4哈巴狗加载程序的图片网址不正确

来自分类Dev

连续设置4张图片,而不是2张图片

来自分类Dev

连续设置4张图片,而不是2张图片

来自分类Dev

Java-将图片分割成4张图片

来自分类Dev

随机图片名称图片上传MVC 4

来自分类Dev

图片为按钮C4

来自分类Dev

图片未在Laravel 4 Framework中显示

来自分类Dev

如何使4张图片反应灵敏

来自分类Dev

Laravel 4-更改(上传图片名)

来自分类Dev

TinyMCE 4和图片网址

来自分类Dev

图片未在Laravel 4 Framework中显示

来自分类Dev

用于水平处理4k图片

来自分类Dev

连续4张图片,共3行

来自分类Dev

MVC4图片上传和预览

来自分类Dev

无法加载 Carousel - Bootstrap 4

来自分类Dev

若要使用OpenCV / cv2比较并标记2张图像(带图片)之间的差异

来自分类Dev

Gmaps4Rails-自定义标记图片无法点击/未打开信息窗口

来自分类Dev

Gmaps4Rails-自定义标记图片无法点击/未打开信息窗口

来自分类Dev

Rails 4:使用回形针上传多张图片

来自分类Dev

Gaufrette(使用AWS)和Symfony 4-阅读图片

来自分类Dev

Rails 4:使用回形针上传多张图片

来自分类Dev

使用 Bootstrap 4 的图片库缩略图大小

来自分类Dev

加载图片时卸载div

来自分类Dev

无法使用jquery mvc 4加载内容

来自分类Dev

无法加载图片

来自分类Dev

图片无法加载(HTML)