裁剪图像时出现Python错误:平铺无法扩展图像外部

2024-04-26 13:38:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大小为2048x1536的JPG图像文件夹。在每个图像中,日期、时间、温度在顶部给出,相机型号名称在末尾给出。我只想裁剪这些图像的上部下部

图像样本:https://drive.google.com/file/d/1TefkFws5l2RBnI2iH22EI5vkje8JbeBk/view?usp=sharing

使用下面的代码,我得到一个错误-对于我提供的任何尺寸,瓷砖都不能延伸到外部图像**例如(500500)**。我的目标it(1500200015002000)

从PIL导入图像 导入操作系统

#Create an Image Object from an Image
dir=r"C:\\Users\\Desktop\\crop1"
output_dir = r"C:\\Users\\Desktop\\crop2"
file_names = os.listdir(dir)

for file_name in file_names:
    file_path = dir +"\{}".format(file_name)
    im = Image.open(r"{}".format(file_path))
    cropped = im.crop((2000,1500,2000,1500))
    output_file= output_dir+"\{}".format(file_name)
    cropped.save(r"{}".format(output_file))

Tags: pathname图像imageanformatoutputnames
1条回答
网友
1楼 · 发布于 2024-04-26 13:38:26

“(2000150020001500)”是一个空框,这可以解释为什么即使“瓷砖无法延伸到外部图像”错误不完全合适,裁剪也会失败。crop的4元组参数的含义是“(左、上、右、下)”。来自Image.crop() documentation的示例:

from PIL import Image

im = Image.open("hopper.jpg")

# The crop method from the Image module takes four coordinates as input.
# The right can also be represented as (left+width)
# and lower can be represented as (upper+height).
(left, upper, right, lower) = (20, 20, 100, 100)

# Here the image "im" is cropped and assigned to new variable im_crop
im_crop = im.crop((left, upper, right, lower))

相关问题 更多 >