简体   繁体   English

如何将图像从列表下载到 Python 中的文件夹

[英]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 个问题:

  1. 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错误。

  2. 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.但是目标文件夹在两者中都是空的。

  3. 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.

相关问题 如何从 url 列表中抓取图像并将其下载到本地文件夹? - How to I scrape images from a list of urls and download it to local folder? 如何从 python 中的 url 列表中异步下载图像? - How can i asyncronously download images from a list of urls in python? Python脚本,使用BeautifulSoup将网站上的所有图像下载到指定的文件夹 - Python script to download all images from a website to a specified folder with BeautifulSoup 从 CSV 中的 Urls 下载图像到本地文件夹 Python 中的文件 - Download Images to Local Folder from Urls in CSV file in Python 如何使用 python 从 firebase 下载文件夹? - How to download a folder from firebase with python? 如何将数据集的图像列表保存(写入)到新文件夹-openCV Python? - How to save (write) a list of images from a dataset into a new folder - openCV Python? 如何将多个图像从一个文件夹移动到 python 中的另一个文件夹? - how to move multiple images from a folder to another folder in python? 将图像从文件夹 python 导入到 numpy 数组列表 - import images from folder python to numpy array list Python:从Google下载图片 - Python: Download Images from Google python从url下载图像 - python download images from url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM