如何打开图像文件夹(Python)

时间:2022-06-02 05:52:55

I'm working on a program to open a folder full of images, copy the images, and then save the copies of the images in a different directory.

我正在开发一个程序来打开一个包含图像的文件夹,复制图像,然后将图像的副本保存在不同的目录中。

I am using Python 2.4.4, but I am open to upgrading the program to a newer version if that allows me to import PIL or Image because I cannot do that with my version.

我正在使用Python 2.4.4,但我愿意将程序升级到更新的版本,如果它允许我导入PIL或Image,因为我不能用我的版本。

As of now, I have:

截至目前,我有:

import Image
import os

def attempt():
    path1 = "5-1-15 upload"
    path2 = "test"

    listing = os.listdir(path1)
    for image in listing:
        im = Image.open(path1)
        im.save(os.path.join(path2))

I am new to Python, so this is probably obviously wrong for numerous reasons.

我是Python的新手,因此很多原因显然是错误的。

I mostly need help with opening a folder of images and iterating through the pictures in order to save them somewhere else.

我主要需要帮助打开图像文件夹并迭代图片以便将它们保存在其他地方。

Thanks!

Edit- I've tried this now:

编辑 - 我现在试过这个:

import shutil

def change():
    shutil.copy2("5-1-15 upload", "test")

And I am receiving an IOError now: IOError: System.IO.IOException: Access to the path '5-1-15 upload' is denied. ---> System.UnauthorizedAccessException: Access to the path '5-1-15 upload' is denied.

我现在收到一个IOError:IOError:System.IO.IOException:拒绝访问路径'5-1-15 upload'。 ---> System.UnauthorizedAccessException:拒绝访问路径'5-1-15 upload'。

at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in :0

在System.IO.FileStream..ctor(System.String路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,布尔匿名,FileOptions选项)[0x00000] in:0

Am I entering the folders wrong? How should I do this if it an a folder within a specific computer network.

我输错了文件夹吗?如果它是特定计算机网络中的文件夹,我该怎么做呢。

Basically there is a folder called images with multiple subfolders within it which I am trying to extract the images from.

基本上有一个名为images的文件夹,里面有多个子文件夹,我试图从中提取图像。

1 个解决方案

#1


Based on this answer, copyfile will do the trick, as you are just copying images from one side to another, as if it was another type of file.

根据这个答案,copyfile可以解决问题,因为您只是将图像从一侧复制到另一侧,就像它是另一种类型的文件一样。

import shutil
import os

def attempt():
    path1 = "5-1-15 upload"
    path2 = "test"

    listing = os.listdir(path1)
    for image in listing:
        copyfile(image, path2)

#1


Based on this answer, copyfile will do the trick, as you are just copying images from one side to another, as if it was another type of file.

根据这个答案,copyfile可以解决问题,因为您只是将图像从一侧复制到另一侧,就像它是另一种类型的文件一样。

import shutil
import os

def attempt():
    path1 = "5-1-15 upload"
    path2 = "test"

    listing = os.listdir(path1)
    for image in listing:
        copyfile(image, path2)