我只想把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
- 输出:
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'
通过通读讨论Cv2.error : (-215:Assertion failed) !_img.empty() in function 'imwrite'
我在检查我的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
- 输出:
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
到目前为止,我还不知道如何解决我问题
1条答案
按热度按时间fwzugrvs1#
我没有测试。
要解决这个错误,我们必须删除
new_crop
变量的声明,并将image.append()
添加到它自己的行中。编辑:有另一种方法代替追加。如果这能起作用的话。