0

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.

Alina DW
  • 111
  • 1
  • 17

1 Answers1

0

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)
Community
  • 1
  • 1
otorrillas
  • 4,357
  • 1
  • 21
  • 34
  • Thanks! I tried this but i'm getting an error that says global name copyfile not found. This doesn't make sense though because I already imported it, right? – Alina DW May 07 '15 at 19:39
  • I tried shutil.copyfile but i'm receiving a different directory error with that not being able to find the file – Alina DW May 07 '15 at 19:40
  • @AlinaDW, this is because you probably need to include the full path. Please check this question: http://stackoverflow.com/questions/7132861/building-full-path-filename-in-python – otorrillas May 07 '15 at 19:47