0

I have some images in a folder and if their dimensions are not what I want, I copy them to another folder and then resize them using the thumbnail command. After I've done that I want to rename them and as part of their new name I want to include the pixel sizes.

Anyway I've tried my code and I run into the following error:

File "C:/Anaconda/PhD/Scikit-Learn/Image Resizing3.py", line 55, in resize_img
os.rename(image_directory + '/Resized'+ "/" + image_filenames[i],image_directory + '/Resized'+ "/" + image_filenames[i][0:(len(image_filenames[i])-4)]+ str(img1.size[0]) + ' x ' + str(img1.size[1]) + '.jpg' )

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:

I thought if I used the with keyword that it would close any processes I was using. If anyone can see any problems in my code wrt to the error then I'd really appreciate it if you have a solution for it.

import scipy.io as sio
from matplotlib import pyplot as plt
from matplotlib import image as mpimg
from PIL import Image
from collections import OrderedDict
import shutil as sh
import os


image_directory = 'C:/StreetView/Labelled Images with Classes/Folder1      /Labelled/Classes/wood'

def resize_img(image_directory, pixel_width, pixel_height):
    image_filenames = []
    DIR = os.listdir(image_directory)

    for n in DIR:
        if n.endswith('.jpg'):
            image_filenames.append(n)

    image_sizes = []
    images = []

    for i in range(len(image_filenames)):
        with Image.open(image_directory + '/' + image_filenames[i]) as     images:
            image_sizes.append(tuple(images.size))

    ordered_imsize = tuple(OrderedDict.fromkeys(image_sizes))
    pixarea = []

    for i in range(len(ordered_imsize)):
        arg1 = ordered_imsize[i][0]
        arg2 = ordered_imsize[i][1]
        pixarea.append(arg1 * arg2)   

    print('The maximum pixel area is', max(pixarea))
    print('The image dimensions giving the largest area is:',     ordered_imsize[pixarea.index(max(pixarea))] )

    print('The minimum pixel area is', min(pixarea))
    print('The image dimensions giving the smallest area is:',     ordered_imsize[pixarea.index(min(pixarea))] )

    if not os.path.exists(image_directory + '/Resized'): 
        os.mkdir(image_directory + '/Resized') # Then make the folder

    for i in range(len(image_directory)):
        if (image_sizes[i][0] >= 100) or (image_sizes[i][1] >= 400):
            print('Inside the greater than loop')
            sh.copy(image_directory + "/" +     image_filenames[i],image_directory + '/Resized')
            image_sizes1 = []
            with Image.open(image_directory + '/Resized'+ "/" +    image_filenames[i]) as img1: # need to use the with keyword because this makes     sure the file is closed after its been used so it doesn't cause any conflicts like it did when it wasn't used.
                print('Checking size: ', img1.size)
                img2 = img1.thumbnail((pixel_width,pixel_height)) # img1 is not the thumbnail
                image_sizes1.append(tuple(img1.size))

            os.rename(image_directory + '/Resized'+ "/" +   image_filenames[i],image_directory + '/Resized'+ "/" + image_filenames[i][0:(len(image_filenames[i])-4)]+ str(img1.size[0]) + ' x ' + str(img1.size[1]) + '.jpg' )

            print('Image number', i, 'has been resized')

        else:
            print('inside the else loop')
            sh.copy(image_directory + "/" + image_filenames[i],image_directory + '/Resized')
            image_sizes2 = []
            with Image.open(image_directory + '/Resized'+ "/" + image_filenames[i]) as img3:
                image_sizes2.append(tuple(img3.size))
                img3.save(image_directory + '/Resized' + "/" + image_filenames[i][0:len(image_filenames[i])-4] + '_' + str(img3.size[0]) + 'x' + str(img3.size[1]) + ".jpg")

            #os.remove(image_directory + '/Resized'+ "/" + image_filenames[i])

            print('Image number', i, 'has been copied over and not resized')

resize_img(image_directory,100,400)

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process

Mr Moose
  • 3
  • 1
  • 5

1 Answers1

0

Solved it, I used shutil.move instead of os.rename.

Mr Moose
  • 3
  • 1
  • 5