Skip to content

Instantly share code, notes, and snippets.

@Soyuzbek
Last active June 22, 2021 16:12
Show Gist options
  • Save Soyuzbek/10da2f7ea8a0ebf22453bbe5acea6810 to your computer and use it in GitHub Desktop.
Save Soyuzbek/10da2f7ea8a0ebf22453bbe5acea6810 to your computer and use it in GitHub Desktop.
import os
import sys
import requests
VALID_IMAGE_EXTENSIONS = ('.png', '.jpg', '.jpeg')
def image_uploader(url, directory):
images = []
for root, dirs, files in os.walk(directory): # walking through source input directory
images.extend([{'file': os.path.join(root, file), 'name': file} for file in files if
file.endswith(VALID_IMAGE_EXTENSIONS)])
print(images)
if images:
filehandlers = []
try:
filehandlers = [open(image['file'], 'rb') for image in images]
response = requests.post(url, files=[(fh.name.split('/')[-1], fh) for fh in filehandlers])
print(response.status_code)
finally:
for fh in filehandlers:
fh.close()
if __name__ == '__main__':
if len(sys.argv) > 2:
url = sys.argv[1]
input_directory = sys.argv[2]
if not os.path.exists(input_directory):
print(input_directory, " does not exist")
sys.exit(1)
if not os.path.isdir(input_directory):
print(input_directory, ' is not a directory')
sys.exit(1)
image_uploader(url, input_directory)
else:
print("The url & the input images directory required")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment