python-3.x 未创建输出.jpg文件

8tntrjer  于 2023-10-21  发布在  Python
关注(0)|答案(2)|浏览(119)

我这里有一个Python脚本,它应该创建一组16张照片。它还添加出血输出jpg。我的问题是脚本创建了一个输出文件夹,但里面没有jpg文件。

from PIL import Image, ImageOps
import os

# Directory path for the input images
input_directory_path = r'C:\Users\User\Desktop\ScriptTest\jpgRearrangment'

# Output directory path
output_directory = os.path.join(input_directory_path, 'output_with_bleeds_layout')
os.makedirs(output_directory, exist_ok=True)

# Output dimensions for the new .jpg with bleeds
output_width = 1270  # 127mm at 300 DPI
output_height = 1780  # 178mm at 300 DPI

# Bleed dimensions
top_bleed = 50  # 5mm at 300 DPI
bottom_bleed = 50  # 5mm at 300 DPI
left_bleed = 42  # 3.5mm at 300 DPI
right_bleed = 42  # 3.5mm at 300 DPI

# Calculate the dimensions of each individual photo with bleeds
photo_width = (output_width - left_bleed - right_bleed) // 4
photo_height = (output_height - top_bleed - bottom_bleed) // 4

# Function to add bleeds to an image and save it
def add_bleeds_and_save(input_image_path, output_image_path):
    img = Image.open(input_image_path)

    # Resize the original image to match the individual photo dimensions
    img = img.resize((photo_width, photo_height), Image.ANTIALIAS)

    # Create a new image with the desired dimensions and white background
    new_img = Image.new('RGB', (output_width, output_height), (255, 255, 255))

    # Calculate the position to paste the resized original image into the layout with bleeds
    x_offset = left_bleed
    y_offset = top_bleed

    # Paste the resized original image into the layout with bleeds
    for i in range(4):
        for j in range(4):
            new_img.paste(img, (x_offset + j * photo_width, y_offset + i * photo_height))

    # Save the final image with bleeds
    new_img.save(output_image_path, 'JPEG')

# List all .jpg files in the input directory
jpg_files = [file for file in os.listdir(input_directory_path) if file.endswith('.jpg')]

# Process each .jpg file
for jpg_file in jpg_files:
    # Create an output image with bleeds and layout
    output_image_name = f'rearranged_with_bleeds_layout_{jpg_file}'
    output_image_path = os.path.join(output_directory, output_image_name)
    add_bleeds_and_save(os.path.join(input_directory_path, jpg_file), output_image_path)

    print(f'Processed: {jpg_file} -> {output_image_name}')

print("All .jpg files rearranged with bleeds and layout and saved.")

可以做些什么,以便在这里创建一个jpg文件?

4urapxun

4urapxun1#

可以做的是在你的环境中调试你的代码。这里有一个可能出错的方法:你在Windows上,文件名以“.JPG”结尾,而不是“.JPG”。在进入for循环之前打印出jpg_files,也许你会学到一些东西。
另外,请描述运行脚本时发生的情况。你是否看到一堆以“已处理:“,还是直接进入“所有.jpg文件重新排列出血和布局并保存"?

xam8gpfp

xam8gpfp2#

好吧,我应该做的,是亚历克西斯说的;密码。包含日志后,出现错误,称模块“PIL.Image”没有属性“ANTIALIAS”。原来Pillow 10.0.0删除了Antialias。因此,卸载Pillow 10.0.0并获得Pillow 9.5.0为我解决了这个问题。

相关问题