LeoHsiao1 / pyexiv2

Read and write image metadata, including EXIF, IPTC, XMP, ICC Profile.
GNU General Public License v3.0
196 stars 39 forks source link

Err 2 #124

Closed acroloid closed 7 months ago

acroloid commented 8 months ago

Hi, I use this code to remove exif tags

import os
import pyexiv2
from pathlib import Path  
import pathlib

def remove_exif_tags(image_path):
    try:
        metadata = pyexiv2.Image(image_path)
        metadata.read_exif()

        metadata.clear_exif()

        metadata.write_exif()
        print(f"Removed exif tags for the image: {image_path}")
    except Exception as e:
        print(f"Error deleting exif tags for an image {image_path}: {e}")

def search_images(directory):
    for root, dirs, files in os.walk(directory):
        for file_name in files:
            if file_name.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
                image_path = os.path.join(root, file_name)
                print(image_path)
                remove_exif_tags(image_path)

start_directory = pathlib.Path.cwd()

search_images(start_directory)

but I get this error: Error deleting exif tags for an image C:\Users\Jalif\Desktop\delfot\foto\3\foto\IMG_20231018_115810.jpg: C:\Users\Jalif\Desktop\delfot\foto\3\foto\IMG_20231018_115810.jpg: Failed to open the data source: No such file or directory (errno = 2)

LeoHsiao1 commented 8 months ago

Hi It's a simple and clear exception. It's because this file path doesn't exist.

For example, my computer doesn't have the file, so I can't open it either:

>>> path = r'C:\Users\Jalif\Desktop\delfot\foto\3\foto\IMG_20231018_115810.jpg'
>>> f = open(path)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Jalif\\Desktop\\delfot\\foto\\3\\foto\\IMG_20231018_115810.jpg'
acroloid commented 8 months ago

LeoHsiao1 Thank you for your answer! I just have a lot of subfolders and a lot of photos. And this is just one example of an error, in fact there are thousands of such errors. Your answer probably works for one image, but I can't use it for thousands of variants. I use os.walk and os.path.join to get the absolute path for the file and, judging by, print this path to each image is correct. But why does pyexiv2 give a path error? How do I fix this?

LeoHsiao1 commented 8 months ago

Common causes of FileNotFoundError:

LeoHsiao1 commented 8 months ago

If the problem is not solved, you can try executing the following code and tell me what the output is

import pyexiv2
import os
import pathlib

for root, dirs, files in os.walk(pathlib.Path.cwd()):
    for file_name in files:
        if file_name.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
            image_path = os.path.join(root, file_name)
            print(image_path)
            print(os.path.exists(image_path))    # Test if the file exists
            img = pyexiv2.Image(image_path)      # Test if the file can be opened by pyexiv2.
            print(img)
            break
    break
acroloid commented 8 months ago

C:\Users\Jalif\Desktop\delfot\foto\1\foto\IMG_20231018_115715.jpg

True Traceback (most recent call last): File "C:\Users\Jalif\Desktop\delfot\foto\test.py", li ne 11, in img = pyexiv2.Image(image_path) # Test if the file can be opened by pyexiv2.

File "C:\Python\Python38\lib\site-packages\pyexiv2\core.py", line 15, in ini t self.img = exiv2api.Image(filename.encode(encoding)) RuntimeError: C:\Users\Jalif\Desktop\delfot\foto\1\foto\IMG_20231 018_115715.jpg: Failed to open the data source: No such file or directory (errno = 2)

But I decided to put the folder in the root of the C drive to check the problem in the folder path, and that's what happened, it seems to work and there is some kind of problem in getting the path to the folder on the desktop

C:\delfot\foto\1\foto\IMG_20231018_115715.jpg True <pyexiv2.core.Image object at 0x00000000025A2B80> C:\delfot\foto\2\foto\IMG_20231018_115715.jpg True <pyexiv2.core.Image object at 0x00000000028D8850> C:\delfot\foto\3\foto\IMG_20231018_115715.jpg True <pyexiv2.core.Image object at 0x00000000025A2C70>

LeoHsiao1 commented 8 months ago

Sounds like a file permission issue. Excuse me, could you try to execute the following code?

path = r'C:\Users\Jalif\Desktop\delfot\foto\3\foto\IMG_20231018_115810.jpg'
print(os.path.exists(path))    # Test if the file exists
f = open(path)                 # Test if the file can be opened by open()
img = pyexiv2.Image(path)      # Test if the file can be opened by pyexiv2
acroloid commented 8 months ago
True
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    img = pyexiv2.Image(path)      # Test if the file can be opened by pyexiv2
  File "C:\Python\Python38\lib\site-packages\pyexiv2\core.py", line 15, in __init__    self.img = exiv2api.Image(filename.encode(encoding))
RuntimeError: C:\Users\Jalif\Desktop\delfot\foto\3\foto\IMG_20231018_115810.jpg: Failed to open the data source: No such file or directory (errno  = 2)

I think it's a problem with encoding the path to the file. Some character in the path may contain a simbol that cannot be read or something like that.

LeoHsiao1 commented 8 months ago

If the file path contains unicode characters, then you need to declare the encoding format. https://github.com/LeoHsiao1/pyexiv2/blob/master/docs/Tutorial.md#class-image

Or you can open the image with the open() function and then call pyexiv2.Imagedata to open the image. https://github.com/LeoHsiao1/pyexiv2/blob/master/docs/Tutorial.md#class-imagedata