创建新图像文件时看似随机错误

时间:2016-10-26 18:53:31

标签: python opencv

我的脚本使用opencv将图像旋转到它们应该如何驻留,opencv会自动执行此操作,因此只需通过opencv打开图像然后保存即可。

然后我将exif标头从旧图像传输到新图像(使用pyexiv2)并删除旧文件将新文件重命名为旧文件。

但是我得到了这个看似随机的错误:

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.JPG5048留下,是否有解决方案?或者我错过了什么?

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

1 个答案:

答案 0 :(得分:0)

您没有异常处理,因此当发生错误时,没有机会进行清理。尝试添加try-catch。以下不是很优雅。您可以通过添加特定错误的处理来改进它。

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