简体   繁体   中英

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.

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.

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.

at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [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.

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.

import shutil
import os

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

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

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