简体   繁体   English

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

[英]how to open a folder of images (Python)

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或图像,则可以将程序升级到较新的版本,因为我无法使用该版本执行此操作。

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. 我现在收到IOError:IOError:System.IO.IOException:拒绝访问“ 5-1-15上传”路径。 ---> System.UnauthorizedAccessException: Access to the path '5-1-15 upload' is denied. ---> System.UnauthorizedAccessException:拒绝访问“ 5-1-15上传”路径。

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选项)中:0中的[0x00000]

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的文件夹,其中有多个子文件夹,我正在尝试从中提取图像。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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