【发布时间】:2021-02-04 20:04:48
【问题描述】:
我有一堆图片,我可以使用各种方法很好地加载它们,例如 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
【问题讨论】:
-
同意@MarcSances。问题是,
os.listdir()只提供文件名,但 cv2 需要图像的绝对路径(或者至少由于此类问题,强烈建议始终使用绝对路径)。 -
也许您可以通过 Dropbox 或 Google Drive 或类似工具分享 "unhappy" PNG 文件。
-
也许你也可以分享
cv2.getBuildInformation()的输出 -
甚至可能是一个实际的错误...呃搜索我在 SO 中发现了这个问题,如果不重复,则非常相关:stackoverflow.com/questions/17949268/…
-
您的 PNG 文件可能存在问题,PIL 比 OpenCV 准备接受的更宽容。我建议通过
pngcheck运行您的PNG,并打开所有详细选项。也可以通过 ImageMagick 使用magick identify -verbose YOURIMAGE.PNG并检查输出是否存在问题。