【发布时间】:2022-12-26 21:31:41
【问题描述】:
I just want to cut the img to samll piece and save it , but somehow cannot save
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:
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'
I'm checking my x, y is not empty
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:
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
【问题讨论】:
-
file does not exist or isn't where you thought it was, or it's corrupted. it is that simple. -- that other question you found doesn't deal with your error at all. it deals with an entirely different error. they just have the same consequence.
-
Both errors tell you the same thing: your 'image' variable does not exist or is emtpy. Two tips: (a) rename it, there's a decent chance that identifier is already taken by one of the libraries. (b) check that it exists by yourself first with mat.size() and mat.type()
-
@nick (1) none of the imports can possibly assign anything to the global
imagevariable. renaming is pointless. if that's the entire code, I don't even see room for typos. in python, anundefinedvariable doesn't have a value. using it raises a NameError, but that didn't happen here. (2) .size() and .type() don't exist in python. it's all numpy arrays. -- clearly imread returned None. that's all there is to it.