Search code examples
pythonexiftool

How do I use exiftool in Python?


I am running my code on Ubuntu. I installed exiftool via sudo apt install exiftool and when I say the command which exiftool everything is fine I get the response /usr/bin/exiftool. After that I try to make a function to change the tags of the photo in Python, but Python I think doesn't see exiftool and nothing is added. How do I solve this?

import subprocess

def set_jpeg_metadata_exiftool():

  exiftool_path = "exiftool"


  title    = "Title"   
  subject  = "Subject"
  author   = "Me"
  comment  = "Comment"
  rating_val  = 5
  copyright_val = "Company name"


  keywords = 'test'
  cmd = [
      exiftool_path,
      f'-Title={title}',
      f'-Subject={subject}',
      f'-Author={author}',
      f'-Comment={comment}',
      f'-Rating={rating_val}',
      f'-Copyright={copyright_val}',
      f'-Keywords={keywords}',
      '-overwrite_original',  
      file_path
  ]

  subprocess.run(cmd, check=True, encoding='utf-8')

Solution

  • It seems like Django can’t find exiftool even though it works fine in your terminal. This is usually an issue with the environment. few things you can check is,

    1. The PATH Django uses might not include /usr/bin. You can fix this by adding it manually in your code like import os os.environ['PATH'] += os.pathsep + '/usr/bin'
    2. you can use full path instead of.exiftool_path = "/usr/bin/exiftool"
    3. I think that you have already restarted the server after installation.

    here is a sample code which I have added a sample full path.

    import subprocess
    

    def set_jpeg_metadata_exiftool(file_path): exiftool_path = "/usr/bin/exiftool" # Full path to exiftool

    title = "Title"
    subject = "Subject"
    author = "Me"
    comment = "Comment"
    rating_val = 5
    copyright_val = "Company name"
    keywords = "test"
    
    cmd = [
        exiftool_path,
        f'-Title={title}',
        f'-Subject={subject}',
        f'-Author={author}',
        f'-Comment={comment}',
        f'-Rating={rating_val}',
        f'-Copyright={copyright_val}',
        f'-Keywords={keywords}',
        '-overwrite_original',  
        file_path
    ]
    

    . . . .