Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
DeeG2009
3 years agoExplorer | Level 3
Help with python script uploading images to already created folders
I am looking for some assistance with a script i am trying to get to work to transfer images from a windows server to a dropbox account. For the life of me i cannot get it to stop creating new folder...
Здравко
3 years agoLegendary | Level 20
Hi DeeG2009,
There is a similar question here. You can take some idea out from there. 😉
DeeG2009 wrote:... For the life of me i cannot get it to stop creating new folders. . ...
If the folder you are going to upload to is there, no other will be created. If such a folder is missing, it's gonna created automatic.
Hope this helps.
- DeeG20093 years agoExplorer | Level 3
The folder is there, just say its like this, /images/state/city, i will put that directory into my script, and run it. When running it will create a new folder called images with state/city inside of it instead of uploading to the existing.
- DeeG20093 years agoExplorer | Level 3from PIL import Image, ImageDraw, ImageFontimport csvimport osimport dropboximport piexif# Read configuration from CSVwith open('C:/Program Files/Python310/Scripts/config.csv', 'r') as f:reader = csv.DictReader(f)config = next(reader)# Dropbox access tokenaccess_token = config['access_token']# Connect to Dropboxdbx = dropbox.Dropbox(access_token)# Windows server folder pathwindows_server_folder = config['windows_server_folder']# Dropbox destination folderdestination_folder = config['destination_folder']# Desired image sizesize = tuple(map(int, config['size'].split('x')))# Text to use as watermarktext = config['text']# Font and size to use for the watermark textfont = ImageFont.truetype(config['font'], int(config['font_size']))# List of images in the Windows server folderimages = [f for f in os.listdir(windows_server_folder) if f.endswith(".jpg")]# Counter for naming images with the same namecounter = 1# Loop through each imagefor image in images:try:# Open the imagewith Image.open(os.path.join(windows_server_folder, image)) as img:# Resize the imageimg = img.resize(size, resample=Image.LANCZOS)# Create an ImageDraw objectdraw = ImageDraw.Draw(img)# Get the size of the imageimage_width, image_height = img.size# Get the size of the texttext_width, text_height = draw.textsize(text, font=font)# Calculate x and y positions for the textx = (image_width - text_width) / 2y = (image_height - text_height) / 2# Draw the text on the imagedraw.text((x, y), text, font=font, fill=(255, 255, 255, 128))# Name the image with SEO keywordsname = f"{config['name_prefix']}{counter}.jpg"img.save(os.path.join(windows_server_folder, name), quality=75)counter += 1except Exception as e:print(f"Error processing {image}: {e}")continue# Modify the EXIF datatry:exif_dict = {'GPSLatitudeRef': b'N','GPSLatitude': [(float(config['latitude']))],'GPSLongitudeRef': b'W','GPSLongitude': [(float(config['longitude']))],}exif_bytes = piexif.dump(exif_dict)piexif.insert(exif_bytes, os.path.join(windows_server_folder, name))except Exception as e:print(f"Error adding EXIF data to {name}: {e}")continue# Upload the image to Dropboxtry:with open(os.path.join(windows_server_folder, name), "rb") as f:dbx.files_upload(f.read(), os.path.join(destination_folder, name))except Exception as e:print(f"Error uploading {name} to Dropbox: {e}")continue# Remove the original image from the Windows server foldertry:os.remove(os.path.join(windows_server_folder, image))except Exception as e:print(f"Error removing {image} from Windows server: {e}")continueprint(f"{image} resized, watermarked, Geo Tagged, Keyword Embedded and uploaded successfully.")This is what i am currently running, but like i said it always creates a new folder instead of uploading to the existing folder. maybe i am not getting the path to the folder in dropbox right?
- Здравко3 years agoLegendary | Level 20
DeeG2009 wrote:...# Upload the image to Dropboxtry:with open(os.path.join(windows_server_folder, name), "rb") as f:dbx.files_upload(f.read(), os.path.join(destination_folder, name))except Exception as e:print(f"Error uploading {name} to Dropbox: {e}")continue... maybe i am not getting the path to the folder in dropbox right?DeeG2009, How the path, you are passing as destination (second argument) to 'files_upload', looks like? 🤔 Don't suppose, check it! Does it match to the expected format?
Again, take a look on 'upload' function in referred thread above. 😉
Good luck.
- Greg-DB3 years ago
Dropbox Community Moderator
[Cross-linking for reference: https://stackoverflow.com/questions/75513737/assistance-with-python-script-uploading-images-to-already-created-folders ]
DeeG2009 As Здравко said, a good way to start troubleshooting this would be to check the actual path value you're using, as that will determine where the file is uploaded. If you still need help, please share that value as well as the output of that files_upload call.
About Discuss Dropbox Developer & API
Make connections with other developers
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!