错误:( - 2)找不到函数imwrite_中指定扩展名的writer

时间:2018-01-26 10:21:07

标签: python numpy opencv cv2

我的数据类似于以下内容:

10-0 = [
    [1915, 387, 1933, 402],
    [3350, 387, 3407, 391],
    [842, 505, 863, 521],
 ]

10-0是图像的凭证(找到路径),里面的值是每个框或矩形的坐标,我正在处理的公式是cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2),请注意10 -0是字典中一个元素的关键。

我的代码:

import cv2
    for key in my_dict:
        folder_blueprint = re.findall(r'\d+', key)
        img = PATH_TO_SOURCE+str(folder_blueprint[0])+'-'+str(folder_blueprint[1])+'.png'
        for line in key:
            line_number = 0
            cv2.rectangle(img,( my_dict[key][line_number][0],my_dict[key][line_number][1]),(my_dict[key][line_number][2],my_dict[key][line_number][3]),(255,0,0),2)
            # cv2.imread(img)
            line_number = line_number + 1
        cv2.imwrite(FULL_PATH_TO_DESTINATION, img)
        cv2.imshow(FULL_PATH_TO_DESTINATION, img)
        k = cv2.waitKey(0)  # 0==wait forever

我最终想要的是在新目标文件夹中感兴趣区域周围带有红色框的图像,使原始图像保持原样。 我在这里提到了类似的问题,并提供了相同的错误信息,但它们对我的案例没有帮助。

编辑:我调整了以下内容:

img_path = PATH_TO_SOURCE+str(folder_blueprint[0])+'-'+str(folder_blueprint[1])+'.png'
img = cv2.imread(img_path) 

并将int()放在cv2.rectangle部分所需的位置,现在我有了这个错误error: (-2) could not find a writer for the specified extension in function imwrite_

2 个答案:

答案 0 :(得分:0)

在此处阅读cv2.rectangle

如果你正在绘制一个矩形,你应该将一个numpy数组传递给<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="accordionWrapper"> <div class="accordionItem open"> <h2 class="accordionItemHeading">About accordions</h2> <div class="accordionItemContent"> <p>JavaScript accordions let you squeeze a lot of content into a small space in a Web page.</p> <p>This simple accordion degrades gracefully in browsers that don't support JavaScript or CSS.</p> </div> </div> <div class="accordionItem close"> <h2 class="accordionItemHeading">Accordion items</h2> <div class="accordionItemContent"> <p>A JavaScript accordion is made up of a number of expandable/collapsible items. Only one item is ever shown at a time.</p> <p>You can include any content you want inside an accordion item. Here's a bullet list:</p> <ul> <li>List item #1</li> <li>List item #2</li> <li>List item #3</li> </ul> </div> </div> <div class="accordionItem close"> <h2 class="accordionItemHeading">How to use a JavaScript accordion</h2> <div class="accordionItemContent"> <p>Click an accordion item's heading to expand it. To collapse the item, click it again, or click another item heading.</p> </div> </div> </div> <script src="custom.js"></script> </body> </html>函数;然后,您可以使用rectangle将图像写入特定路径。

我认为你应该有一个imwrite循环遍历所有图像,其框架是彩色矩形。

这里使用PIL将图像转换为numpy数组的示例:

for

您可以在此处查看how to use import numpy as np from PIL import Image as im # read image into a PIL data type: PIL.Image.Image, note it is not path/string. img = im.open("sample.png").convert('RGBA') # conver to numpy.nparray arr = np.array(img) # draw a rectangle on your existing image object cv2.rectangle(img, (384,0),(510,128),(0,255,0),3) # once you finished drawing, you can write it to a specific file. cv2.imwrite("write_to_new_file.png", img) 个功能,此处how to write查看新文件。

P.S。您可以在Python3中使用以下命令安装PIL:

rectangle

答案 1 :(得分:0)

确保FULL_PATH_TO_DESTINATION具有在文件名字符串中也提到的图像扩展名。 读取文件具有.png,但我认为要写入图像的输出路径应具有路径以及扩展名

的文件名