简体   繁体   English

为什么我的 cv2.imread() 调用返回 None?

[英]why is my cv2.imread() call returning None?

I just want to cut the img to samll piece and save it, but somehow cannot save我只想将 img 切成小块并保存,但不知何故无法保存

import os
import cv2
import numpy as np
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import pytesseract


sorted_predict_xy_list = [ [71,180], [221,180], [371,180], [521,180], [671,180], [821,180],
[71,291], [221,291], [371,291], [521,291], [671,291], [821,291],
[71,402], [221,402], [371,402], [521,402], [671,402], [821,402],
[71,513], [221,513], [371,513], [521,513], [671,513], [821,513],
[71,624], [221,624], [371,624], [521,624], [671,624], [821,624] ]

image = cv2.imread("/home/student_DC/desktop/optimization_11_10/original_duplicate.png")
j = 0
while j < len(sorted_predict_xy_list) :
    temp_xy = sorted_predict_xy_list[j]
    x = temp_xy[0]
    y = temp_xy[1]
    small_txt_size_w = 65
    small_txt_size_h = 16
    new_crop = image[y:y+small_txt_size_h, x:x+small_txt_size_w]
    cv2.imwrite("/home/student_DC/desktop/optimization_11_10/output_11_10__001/x_{x}_y_{y}.png", new_crop)
    j+=1
  • output: output:
Traceback (most recent call last):
  File "/home/student_DC/desktop/optimization_11_10/draw_squar_on_duplicate_01N.py", line 26, in <module>
    cv2.imwrite("/home/student_DC/desktop/optimization_11_10/output_11_10__001/x_{x}_y_{y}.png", new_crop)
cv2.error: OpenCV(3.4.15) /tmp/pip-req-build-9opz8s5q/opencv/modules/imgcodecs/src/loadsave.cpp:741: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'

by read through discussion Cv2.error: (-215:Assertion failed)._img.empty() in function 'imwrite'通过通读讨论Cv2.error: (-215:Assertion failed)._img.empty() in function 'imwrite'
I'm checking my x, y is not empty我正在检查我的 x,y 不为空

import os
import cv2
import numpy as np
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import pytesseract


sorted_predict_xy_list = [ [71,180], [221,180], [371,180], [521,180], [671,180], [821,180],
[71,291], [221,291], [371,291], [521,291], [671,291], [821,291],
[71,402], [221,402], [371,402], [521,402], [671,402], [821,402],
[71,513], [221,513], [371,513], [521,513], [671,513], [821,513],
[71,624], [221,624], [371,624], [521,624], [671,624], [821,624] ]

image = cv2.imread("/home/student_DC/desktop/optimization_11_10/original_duplicate.png")
j = 0
while j < len(sorted_predict_xy_list) :
    temp_xy = sorted_predict_xy_list[j]
    x = temp_xy[0]
    y = temp_xy[1]
    small_txt_size_w = 65
    small_txt_size_h = 16
    new_crop = image[y:y+small_txt_size_h, x:x+small_txt_size_w]
    print("x :" , x  , ",y :",y , ", x+small_txt_size_w", x+small_txt_size_w , ", y+small_txt_size_h :" , y+small_txt_size_h)
    j+=1
  • output: output:
Traceback (most recent call last):
  File "/home/student_DC/desktop/optimization_11_10/draw_squar_on_duplicate_01N.py", line 25, in <module>
    new_crop = image[y:y+small_txt_size_h, x:x+small_txt_size_w]
TypeError: 'NoneType' object is not subscriptable

so far I get here, but don't know how to slove my issue到目前为止我到了这里,但不知道如何解决我的问题

I did not test.我没有测试。

To solve this error, we must remove the declaration of the new_crop variable and add image.append() on its own line.要解决此错误,我们必须删除new_crop变量的声明并在其自己的行上添加image.append()

image.append(y:y+small_txt_size_h, x:x+small_txt_size_w)
print("x :" , x  , ",y :",y , ", x+small_txt_size_w", x+small_txt_size_w , ", y+small_txt_size_h :" , y+small_txt_size_h)
j+=1

Edit: there is another way instead of append. If this could work.编辑:有另一种方法可以代替 append。如果这可行的话。

image = cv2.imread(......    
    f image is None:
        exit
    j = 0

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

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