简体   繁体   中英

How to save a list of modified images into another folder?

def gcd(m, n):
  fm = []
for i in range(1, m + 1):
  if (m % i) == 0:
    fm.append(i)
fn = []
for j in range(1, n + 1):
  if (n % i) == 0:
    fn.append(j)
cf = []
for f in fm:
  if f in fn:
  cf.append(f)
return (cf[-1]) for i in pick:
  if scores[i] > min_threshold:
  box = boxes_pixels[i]
box = np.round(box).astype(int)# Draw bounding box.
image = cv2.rectangle(
  image, (box[1], box[0]), (box[3], box[2]), (0, 255, 0), 2)
label = "{}:{:.2f}".format(int(classes[i]), scores[i])

print(classes[i], end = ' ')# Draw label(class index and probability).
draw_label(image, (box[1], box[0]), label)
print(box[1])

print(box[1], box[0], box[3], box[2])# im = image.crop((105, 10, 131, 53))
cropped_image = image[10: 53, 105: 131]
else :
  continue
for n in range(0, len(onlyfiles)): #Save and display the labeled image.
save_image(image[: ,: , ::-1])
Image(fname, onlyfiles[n])
imshow(image)

def save_image(data, fname = '/home/sunil/image/', swap_channel = True):
  if swap_channel:
  data = data[..., ::-1]
cv2.imwrite(fname, data)

error Traceback (most recent call last) in 38 for n in range(0, len(onlyfiles)): 39 #Save and display the labeled image. ---> 40 save_image(image[:, :, ::-1]) 41 Image(fname,onlyfiles[n]) 42 imshow(image)

in save_image(data, fname, swap_channel) 2 if swap_channel: 3 data = data[..., ::-1] ----> 4 cv2.imwrite(fname, data)

error: OpenCV(4.1.0) /io/opencv/modules/imgcodecs/src/loadsave.cpp:662: error: (-2:Unspecified error) could not find a writer for the specified extension in function 'imwrite_'

Default filename for your save_image function is /home/sunil/image/ looks like a directory and has no extension. Try changing the function definition to

def save_image(data, fname = '/home/sunil/image/processed.jpg', swap_channel = True):

or maybe, since you want to save multiple files into a same directory, introduce a pattern for file name:

def save_image(data, fname = '/home/sunil/image/processed_{n:03d}.jpg', swap_channel = True, n=0):
    if swap_channel:
        data = data[..., ::-1]
        cv2.imwrite(fname.format(n=n), data)

then call the function like:

save_image(image[: ,: , ::-1], n=n)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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