Last active
June 22, 2021 16:12
-
-
Save Soyuzbek/10da2f7ea8a0ebf22453bbe5acea6810 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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