[英]How to download images from a list to a folder in Python
I am trying to crop images according to their masked images (image masking) but i am running into 3 problems:我正在尝试根据蒙版图像(图像蒙版)裁剪图像,但我遇到了 3 个问题:
If I run it on Jupyter notebook, it successfully crops 1 image.如果我在 Jupyter notebook 上运行它,它会成功裁剪 1 张图像。 If I add even 1 more image, it gives list index out of range
error.如果我再添加 1 个图像,它会给出list index out of range
错误。
I have tried several different methods to download the cropped images saved in the Dataset_cropped list to a folder in both jupyter notebook and google colab.我尝试了几种不同的方法将保存在 Dataset_cropped 列表中的裁剪图像下载到 jupyter notebook 和 google colab 中的文件夹中。 But the target folder remains empty in both.但是目标文件夹在两者中都是空的。
When I run this code on Google Colab, I am unable to display the cropped image on the screen using plt.imshow(img_org)
command.当我在 Google Colab 上运行此代码时,我无法使用plt.imshow(img_org)
命令在屏幕上显示裁剪后的图像。 It generated the error TypeError: Image data of dtype object cannot be converted to float
at first.它产生错误TypeError: Image data of dtype object cannot be converted to float
。 Now it is not producing any result and not giving an error either.现在它没有产生任何结果,也没有给出错误。 -This command works fine on jupyter notebook. -此命令在 jupyter notebook 上运行良好。
Please let me know if you can help in this regard.请让我知道您是否可以在这方面提供帮助。 Thanks!谢谢!
The code I am using is:我正在使用的代码是:
import cv2
import glob
import os
import matplotlib.pyplot as plt
Dataset_cropped = []
org_list = sorted(glob.glob('\content\drive\MyDrive\original*'))
msk_list = sorted(glob.glob('\content\drive\MyDrive\mask*'))
for i in range(len(org_list)):
img_org = cv2.imread(org_list[i])
img_mask = cv2.imread(msk_list[i])
img_org = cv2.resize(img_org, (400,400), interpolation = cv2.INTER_AREA)
img_mask = cv2.resize(img_mask, (400,400), interpolation = cv2.INTER_AREA)
for h in range(len(img_mask)):
for w in range(len(img_mask)):
if img_mask[h][w][0]==0:
for i in range (3):
img_org[h][w][i] = 255
else:
continue
#plt.imshow(img_org)
dataset_cropped.append(img_org)
continue
plt.imshow(img_org)
Some of the methods I have tried to download the saved images:我尝试下载保存的图像的一些方法:
Using these inside the loop:在循环中使用这些:
img = cv2.imread(img_org) cv2.imwrite("\content\drive\MyDrive\cropped\CH.png", img)
Using these outside the loop:在循环外使用这些:
image = np.asarray(TBB_CROPPED)
count=0
i=0
while i < len(TBB_CROPPED):
cv2.imwrite('\content\drive\MyDrive\cropped\CH%d'+str(count)+'.png',image)
i = i+1
count = count + 1
Christoph Rackwitz is right: the slashes are pretty much the problem. Christoph Rackwitz 是对的:斜线几乎就是问题所在。 Try changing them from backward to forward when you save the file:保存文件时尝试将它们从向后更改为向前:
cv2.imwrite("/content/drive/MyDrive/cropped/CH.png", img)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.