简体   繁体   English

如何 cv2.imread 压缩文件中的图像?

[英]How to cv2.imread an image within a zipfile?

I'm trying to use OpenCV2 to read images withing a zipfile with cv2.imread and then resize them and save them but I can't figure out how to read those images within the zipfile.我正在尝试使用 OpenCV2 读取带有cv2.imread的 zipfile 的图像,然后调整它们的大小并保存它们,但我不知道如何在 zipfile 中读取这些图像。 This is my code so far:-到目前为止,这是我的代码:-

import cv2
from zipfile import ZipFile

with ZipFile("sample_images.zip", "r") as zipFile:
    imgs = zipFile.namelist()
    img = zipFile.open(imgs[0])
    processedImg = cv2.imread(img)

This is the error I get:-这是我得到的错误:-

Traceback (most recent call last):
  File "C:/Users/Yash/Desktop/Programming/opencv test/test.py", line 8, in <module>
    processedImg = cv2.imread(img)
SystemError: <built-in function imread> returned NULL without setting an error

Please suggest a solution to this problem.请提出解决此问题的方法。 Thanks in advance!提前致谢!

Try this尝试这个

from io import BytesIO
from zipfile import ZipFile
from PIL import Image

with ZipFile("sample_images.zip", "r") as zipFile:
    imgs = zipFile.namelist()
    print(imgs[0])
    img = zipFile.read(imgs[0])
    processedImg = Image.open(BytesIO(img))
    processedImg.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM