k_schultz k_schultz - 1 month ago 11
Python Question

Seemingly Random Error when creating a new image file

My script, which uses opencv to rotate images to how they should reside, opencv does this automatically so its pretty easy just to open the image via opencv then save.

I then transfer the exif headers from the old image to the new(using pyexiv2) and delete the old renaming the new file to what the old file was.

However I get this seemingly random error:

Traceback (most recent call last):
File "C:/Users/name/PycharmProjects/exif_data/test.py", line 28, in <module>
metadata.write()
File "C:\Users\name\AppData\Roaming\Python\Python27\site-packages\pyexiv2\metadata.py", line 119, in write
self._image._writeMetadata()
IOError: C:\Users\name\Desktop\TEST\IMAGES\GIS\DUPLICATE.JPG5048: Failed to rename file to C:\Users\name\Desktop\TEST\IMAGES\GIS\DUPLICATE.JPG: File exists (errno = 17)


duplicate doesnt exist and is being renamed on script every time once it failed it leaves the file DUPLICATE.JPG5048 in the folder, is there a solution? Or am I missing something?

import numpy as np
import cv2
import pyexiv2
import os


directory = "C:\Users\name\Desktop\TEST\\IMAGES"

for dirname, dirnames, filenames in os.walk(directory):
print(dirname)
for file in os.listdir(dirname):
if file.endswith(('.jpeg', '.jpg')) or file.endswith(('.JPEG', '.JPG')) :
filename, file_extension = os.path.splitext(directory+file)
oldFile = dirname+"\\"+file
newFile = dirname+"\\DUPLICATE"+file_extension
image = cv2.imread(oldFile)
cv2.imwrite(newFile, image)
m1 = pyexiv2.ImageMetadata(oldFile)
m1.read()
m1.modified = True # not sure what this is good for
m2 = pyexiv2.metadata.ImageMetadata(newFile)
m2.read() # yes, we need to read the old stuff before we can overwrite it
m1.copy(m2)
m2.write()
metadata = pyexiv2.ImageMetadata(newFile)
metadata.read()
print ', '.join(metadata.exif_keys)
metadata.write()
key = 'Exif.Image.Orientation'
value = 1
metadata[key] = pyexiv2.ExifTag(key, value)
os.remove(oldFile)
os.rename(newFile, oldFile)
del m1
del m2
del oldFile
del newFile

CAB CAB
Answer

You have no exception handling, so when an error occurs, there's no chance to clean up. Try adding try-catch. The following isn't very elegant. You could improve it by adding handling for specific errors.

import numpy as np
import cv2
import pyexiv2
import os

directory = "C:\Users\name\Desktop\TEST\\IMAGES"

for dirname, dirnames, filenames in os.walk(directory):
  print(dirname)
  for file in os.listdir(dirname):
    if file.endswith(('.jpeg', '.jpg')) or file.endswith(('.JPEG', '.JPG')) :
        try:
            filename, file_extension = os.path.splitext(directory+file)
            oldFile = dirname+"\\"+file
            newFile = dirname+"\\DUPLICATE"+file_extension
            image = cv2.imread(oldFile)
            cv2.imwrite(newFile, image)
            m1 = pyexiv2.ImageMetadata(oldFile)
            m1.read()
            m1.modified = True  # not sure what this is good for
            m2 = pyexiv2.metadata.ImageMetadata(newFile)
            m2.read()  # yes, we need to read the old stuff before we can overwrite it
            m1.copy(m2)
            m2.write()
            metadata = pyexiv2.ImageMetadata(newFile)
            metadata.read()
            print ', '.join(metadata.exif_keys)
            metadata.write()
            key = 'Exif.Image.Orientation'
            value = 1
            metadata[key] = pyexiv2.ExifTag(key, value)
            os.remove(oldFile)
            os.rename(newFile, oldFile)
        except Exception as e:
            print oldfile, 'not converted'
            print str(e)
            os.remove(newFile) # remove the partial new file
Comments