cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Share your feedback on the Document Scanning Experience in the Dropbox App right here.

Discuss Dropbox Developer & API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help with python script uploading images to already created folders

Help with python script uploading images to already created folders

DeeG2009
Explorer | Level 3

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 folders. I am trying to get it to upload to existing folders using the destination folder that appears in the browser url string of the desired folder. Anyone willing to help me out on this ill be glad to share the script, i am very green to all of this so i am hoping it is just something silly i am overlooking. Thanks in advance. 

15 Replies 15

Здравко
Legendary | Level 20

Hi @DeeG2009,

There is a similar question here. You can take some idea out from there. :winking_face:

 


@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.

DeeG2009
Explorer | 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. 

DeeG2009
Explorer | Level 3
from PIL import Image, ImageDraw, ImageFont
import csv
import os
import dropbox
import piexif

# Read configuration from CSV
with open('C:/Program Files/Python310/Scripts/config.csv', 'r') as f:
    reader = csv.DictReader(f)
    config = next(reader)

# Dropbox access token
access_token = config['access_token']

# Connect to Dropbox
dbx = dropbox.Dropbox(access_token)

# Windows server folder path
windows_server_folder = config['windows_server_folder']

# Dropbox destination folder
destination_folder = config['destination_folder']

# Desired image size
size = tuple(map(int, config['size'].split('x')))

# Text to use as watermark
text = config['text']

# Font and size to use for the watermark text
font = ImageFont.truetype(config['font'], int(config['font_size']))

# List of images in the Windows server folder
images = [f for f in os.listdir(windows_server_folder) if f.endswith(".jpg")]

# Counter for naming images with the same name
counter = 1

# Loop through each image
for image in images:
    try:
        # Open the image
        with Image.open(os.path.join(windows_server_folder, image)) as img:
            # Resize the image
            img = img.resize(size, resample=Image.LANCZOS)

            # Create an ImageDraw object
            draw = ImageDraw.Draw(img)

            # Get the size of the image
            image_width, image_height = img.size

            # Get the size of the text
            text_width, text_height = draw.textsize(text, font=font)

            # Calculate x and y positions for the text
            x = (image_width - text_width) / 2
            y = (image_height - text_height) / 2

            # Draw the text on the image
            draw.text((x, y), text, font=font, fill=(255, 255, 255, 128))

            # Name the image with SEO keywords
            name = f"{config['name_prefix']}{counter}.jpg"
            img.save(os.path.join(windows_server_folder, name), quality=75)
            counter += 1
    except Exception as e:
        print(f"Error processing {image}: {e}")
        continue

    # Modify the EXIF data
    try:
        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 Dropbox
    try:
        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 folder
    try:
        os.remove(os.path.join(windows_server_folder, image))
    except Exception as e:
        print(f"Error removing {image} from Windows server: {e}")
        continue

    print(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? 

Здравко
Legendary | Level 20

@DeeG2009 wrote:
...
   # Upload the image to Dropbox
    try:
        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? :thinking_face: Don't suppose, check it! Does it match to the expected format?

Again, take a look on 'upload' function in referred thread above. :winking_face:

Good luck.

Greg-DB
Dropbox Staff

[Cross-linking for reference: https://stackoverflow.com/questions/75513737/assistance-with-python-script-uploading-images-to-alrea... ]

@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.

DeeG2009
Explorer | Level 3

/JD%20Byrd/CPGC/Akron/  - this is where i am sending the images.

DeeG2009_0-1677007594964.png

this is the folder directory

 

DeeG2009_1-1677007647612.png

This is the output from upload to dropbox. 

Здравко
Legendary | Level 20

@DeeG2009 wrote:

...

This is the output from upload to dropbox. 


@DeeG2009, Greg asked you for dump of the result from 'dbx.files_upload'. Post it.

 


@DeeG2009 wrote:

/JD%20Byrd/CPGC/Akron/  - this is where i am sending the images.

...


Are you sure? :thinking_face: How you got to it? Did you check/dump it?

Greg-DB
Dropbox Staff

@DeeG2009 Thanks for the additional information. In this case, "JD Byrd" is the name of your member folder. API calls already default to operating inside your member folder (check out the Team Files Guide for more information), so you shouldn't include that component in the path you supply in API calls. That is, the path you specify for that example should start with "/CPGC/Akron".

 

(Also, for reference, the "%20" is how a space, i.e., " ", is encoded for URLs. You wouldn't include those URL encodings in the path values you supply to a Dropbox API method like files_upload.)

DeeG2009
Explorer | Level 3

Wooowww. i feel like such a idiot now haha, ive been messing with this for almost a week now, and thats all it was. "/CPGC/Akron/" is working just fine to the existing folder. (like i said, totally new to anything coding, self teaching and sponging off others knowledge" ; now a new concern, when script is stopped and started again, i get an error: Error uploading Gutter_Cleaning_Akron1.jpg to Dropbox: ApiError('d', UploadError('path', UploadWriteFailed(reason=WriteError('conflict', WriteConflictError('file', None)), upload_session_id='pid_upload_session:

 

I am assuming it is because when my script runs it will name the images, example: image1.jpg,image2.jpg, etc... and once stopped and started again it starts at 1 again and will not overwrite, Could a timestamp always keep the naming unique ?

 

My ultimate goal is to be able to have the script run down a list of lines in a csv instead of just one line, each line in the csv would be for different folders and corresponding data for each image to be uploaded to, but i cant figure out how to have the script run each line in the csv say 10 times, then move on to the next and so on... 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    DeeG2009 Explorer | Level 3
  • User avatar
    Здравко Legendary | Level 20
What do Dropbox user levels mean?