python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python长图片分割

使用Python将长图片分割为若干张小图片

作者:cheese-liang

这篇文章主要为大家详细介绍了如何使用Python将长图片分割为若干张小图片,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

1. Python需求的任务

使用Python将长图片分隔为若干张小图片

我有如下的一张长图片

想要将其分割为若干张小图片

2. Python代码的实现

from PIL import Image

def cut_image(image_path, output_folder, width, height):
    image = Image.open(image_path)
    image_width, image_height = image.size

    # 计算需要切割成多少行和列的小图片
    rows = image_height // height
    columns = image_width // width

    count = 0

    for row in range(rows):
        for col in range(columns):
            x = col * width
            y = row * height

            # 切割图片
            cropped_image = image.crop((x, y, x + width, y + height))

            # 保存切割后的小图片
            cropped_image.save(f"{output_folder}/image_{count}.png")

            count += 1

image_path = "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/010-Slide photo to small/Excel文件售卖.png"
output_folder = "D:/200-Life/220-Money/236-Csdn/写作/Excel文件售卖"
width = 640  # 每个小图片的宽度
height = 1080  # 每个小图片的高度

cut_image(image_path, output_folder, width, height)

3. 代码修改的位置

image_path = "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/010-Slide photo to small/Excel文件售卖.png"

image_path是你长图片的地址。

output_folder = "D:/200-Life/220-Money/236-Csdn/写作/Excel文件售卖"
width = 640  # 每个小图片的宽度
height = 1080  # 每个小图片的高度

output_folder是小图片导出的地址

width = 640  是每个小图片的宽度

height = 1080 是每个小图片的高度

宽度根据原始图片的宽度进行定义。

高度可以自由定义,这里选为1080。

4. 运行结果

代码运行

运行结果

到此这篇关于使用Python将长图片分割为若干张小图片的文章就介绍到这了,更多相关Python长图片分割内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文