0

I have data similar to the following :

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

10-0 being the credentials of an image(to find the path) and the values inside are coordinates of a box or rectangle each, the formula i'm working on is cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2) , note that 10-0 is the key of one element of a dictionary.

My code :

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

What i finally want is the image with red boxes around the regions of interest in a new destination folder leaving the original image intact. And i have referred to similar questions on here with the same error message but they weren't helpful to my case.

EDIT: I adjusted the following :

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

and put int() where i had to in cv2.rectangle section and now i have this error error: (-2) could not find a writer for the specified extension in function imwrite_

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
user9266899
  • 35
  • 1
  • 5
  • your problem is not about `read` and `write`, if you are drawing a rectangle around a image, then you should give your `rectangle` function a numpy array. – stucash Jan 26 '18 at 11:02
  • @stucash Ok any guidance there as in links to documentation or adjustments in my code. – user9266899 Jan 26 '18 at 11:09

2 Answers2

3

Ensure that FULL_PATH_TO_DESTINATION has image extension also mentioned in the string of file name. Read file has .png but I believe output path where you want to write image should have path as well filename with extention

sam
  • 2,263
  • 22
  • 34
0

give a read about cv2.rectangle here.

if you are drawing a rectangle, you should be passing a numpy array to rectangle function; then you can write your image to a specific path using imwrite.

I would assume you should have a for loop to iterate through all the images, the frame of which is a coloured rectangle.

an example here for converting an image to numpy array using PIL:

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)

you can have a look here how to use rectangle function and here how to write to a new file.

P.S. you can install PIL with following command in Python3:

pip3 install --upgrade image

stucash
  • 1,078
  • 1
  • 12
  • 23
  • Kindlynote that the link to `how to write` is the same exact way in my code used at the end of my code shared in the question, i adjusted the following part of the code ` img_path = im.open(FULL_PATH).convert('RGBA') img = cv2.imread(img_path) arr = np.array(img)` and still didn't work giving me this error `img = cv2.imread(img_path) TypeError: bad argument type for built-in operation` – user9266899 Jan 26 '18 at 11:24
  • @user9266899 yes, I noted that; however it's not the same flow as what I showed in the answer. in your comment, you treated the converted `img` as `img_path`; it's not a path anymore. you just convert it using `np.array` without reading it first. i.e. you don't need `img=cv2.imread(img_path)` before conversion. you can read it after you finish drawing it. – stucash Jan 26 '18 at 11:37
  • @user9266899 and the reason you getting the error in your comment is because you are giving `numpy.array` a wrong argument. i.e. `img` returned by `cv2.imread`. using your terminologies in the comment, you might want to do `arr = np.array(img_path)` – stucash Jan 26 '18 at 11:48
  • No worries fixed without using numpy. – user9266899 Jan 26 '18 at 12:23
  • @user9266899 great it's resolved. please consider rewrite your question to match what has happened to resolve your issue. Or if leave it be, it's better to reword the title to show it's a numpy error. I think your original title was correct; if you are using numpy, I believe this is how you going to do. Other people might bump into the same question again. Good day! – stucash Jan 26 '18 at 12:33