3

I am not a developer and I have a very limited knowledge of programming. I understand how to use and operate python scripts, however writing them is something I am yet to learn. Please can someone help a total noob :)

I am using an API by sightengine to asses a large folder of .jpg images for their sharpness and colour properties. The documentation for the API only provides a small script for assessing one image at a time. I have spoken to Sight Engines support and they are unwilling to provide a script for batch processing which is bizarre considering all other API companies usually do.

I need some help creating a for loop that will use a python script to iterate through a folder of images and output the API result into a single JSON file. Any help in how to structure this script would be extremely appreciated.

Here is the sightengine code for a simple one image check:

from sightengine.client import SightengineClient

client = SightengineClient("{api_user}", "{api_secret}")
output = client.check('properties','type').set_file('/path/to/local/file.jpg')

Thank you

Chamila Maddumage
  • 3,304
  • 2
  • 32
  • 43
BiggidyBox
  • 31
  • 6
  • 2
    Could you please update the question with the current code you are using? – Vikas NS Jun 26 '19 at 10:13
  • 2
    Don't apologize, but provide some code :) How does the call look for one image? What did you try so far? – Frieder Jun 26 '19 at 10:13
  • Apologies, should have added the code. I'm stumped on how to iterate through the files. I'm still working my way through the w3schools 'python for loops' and i'm no way near understanding where to start. Any help would be greatly appreciated. – BiggidyBox Jun 26 '19 at 11:28
  • 1
    Possible duplicate of [How can I iterate over files in a given directory?](https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory) – albert Jun 26 '19 at 11:35

1 Answers1

1

Part of this is sort of a guess, as I don't know exactly what output will look like. I am assuming it's returned as json format. Which if thats the case, you can append the individual json responses into a single json structure, then use json.dump() to write to file.

So that part is a guess. The other aspect is you want to iterate through your jpg files, which you can do by using os and fnmatch. Just adjust the root directory/folder for it to walk through while it searches fro all the .jpg extensions.

from sightengine.client import SightengineClient
import os
from fnmatch import fnmatch
import json

client = SightengineClient("{api_user}", "{api_secret}")

# Get your jpg files into a list
r = 'C:/path/to/local'
pattern = "*.jpg"
filenames = []

for path, subdirs, files in os.walk(r):
    for name in files:
        if fnmatch(name, pattern):
            #print (path+'/'+name)
            filenames.append(path+'/'+name)


# Now iterate through those jpg files
jsonData = []            
for file in filenames:
    output = client.check('properties','type').set_file(file)
    jsonData.append(output)

with open('C:/result.json', 'w') as fp:
    json.dump(jsonData, fp, indent=2)
chitown88
  • 27,527
  • 4
  • 30
  • 59
  • Thank you for the response and apologies for the delay in responding. The above script runs however it does not pull the images from the directory. It only returns one result which is for the last image i used during my test runs a few days ago. It appears to take the info from the last request made to the API server rather than iterating through the local directory. Any ideas on a solution? Thanks – BiggidyBox Jun 28 '19 at 12:15
  • Also there is no output file created with the above script – BiggidyBox Jun 28 '19 at 12:25
  • Sorted, this worked perfectly, It was user error rather than your code. Thank you – BiggidyBox Dec 13 '19 at 22:38