协助 python 脚本将图像上传到已创建的文件夹

问题描述 投票:0回答:0

在编码方面我是新手,但我有一个 Windows 服务器,用于托管我们公司的图像。我已经得到了脚本,它可以调整大小、压缩、命名、水印、地理标记,然后将图像传输到保管箱。我想不通的是如何让它转移到保管箱中已经创建的文件夹。它总是创建一个新文件夹,比如说目录是 images/state/city,这就是我在 csv 文件中放入的目录,当运行脚本时,它将创建一个名为 images 的主文件夹,state 目录和 sub 作为城市。希望这很简单,提前致谢。

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.")

我已经运行了我刚刚提供的代码,它会做我想做的一切,除了上传到保管箱中已经创建的文件夹。也许我只是不了解保管箱中的正确路径目录?当我在目标文件夹中时,我从 url 字符串中获取它。

python dropbox windows-server
© www.soinside.com 2019 - 2024. All rights reserved.