derrianx / photo-journey

documentation of film photography resources
0 stars 0 forks source link

iso, spd, focal length, aper #2

Open derrianx opened 1 year ago

derrianx commented 1 year ago

https://www.thepythoncode.com/article/extracting-image-metadata-in-python

derrianx commented 1 year ago

try this

import exifread import os import csv

Specify the directory containing the image files

directory = '/path/to/directory'

Open the CSV file for writing

with open('metadata.csv', 'w', newline='') as csvfile:

Create a CSV writer

writer = csv.writer(csvfile)

# Write the header row
writer.writerow(['Filename', 'ISO', 'Aperture', 'Shutter Speed', 'Focal Length'])

# Iterate through all the files in the directory
for filename in os.listdir(directory):
    # Only process JPEG files
    if filename.endswith('.jpg'):
        # Open the image file
        with open(os.path.join(directory, filename), 'rb') as f:
            # Extract EXIF metadata
            tags = exifread.process_file(f)

        # Write the metadata for the current image to the CSV file
        writer.writerow([filename, tags["EXIF ISOSpeedRatings"], tags["EXIF FNumber"], tags["EXIF ExposureTime"], tags["EXIF FocalLength"]])
derrianx commented 1 year ago

stack overflow example

infoDict = {} #Creating the dict to get the metadata tags exifToolPath = 'D:/ExifTool/exifTool.exe' #for Windows user have to specify the Exif tool exe path for metadata extraction.

For mac and linux user it is just

"""exifToolPath = exiftool""" imgPath = 'D:/Images/12.jpg'

''' use Exif tool to get the metadata ''' process = subprocess.Popen([exifToolPath,imgPath],stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True) ''' get the tags in dict ''' for tag in process.stdout: line = tag.strip().split(':') infoDict[line[0].strip()] = line[-1].strip()

for k,v in infoDict.items(): print(k,':', v)