• python教程:把多张图片,合并成一张图


    D:\Wdpython\environment\Scripts\python.exe D:/Wdpython/爬虫/测试8.py
    图片列表 10 [‘刘亦菲/刘亦菲_1.jpg’, ‘刘亦菲/刘亦菲_11.jpg’, ‘刘亦菲/刘亦菲_12.jpg’, ‘刘亦菲/刘亦菲_13.jpg’, ‘刘亦菲/刘亦菲_15.jpg’, ‘刘亦菲/刘亦菲_2.jpg’, ‘刘亦菲/刘亦菲_3.jpg’, ‘刘亦菲/刘亦菲_4.jpg’, ‘刘亦菲/刘亦菲_5.jpg’, ‘刘亦菲/刘亦菲_8.jpg’]
    x_list [1454.0, 1472.0, 1473.0, 1479.0, 1479.0, 1479.0, 1479.0, 1481.0, 1483.0, 1484.0]
    y_list [826.0, 834.0, 920.0, 926.0, 1336.0, 1800.0, 1971.0, 1972.0, 1972.0, 2222.0]
    x_new, y_new 1479 1800
    x_s 1454 1336
    x_s 1481 2222
    x_s 1479 1972
    x_s 1479 1972
    x_s 1479 1971
    x_s 1483 926
    x_s 1473 826
    x_s 1484 834
    x_s 1479 1800
    x_s 1472 920
    图片已经合成了

    进程已结束,退出代码0

    # @Author : 小红牛
    # 微信公众号:wdPython
    import os
    import PIL.Image as Image
    
    def resize_by_width(infile, image_size):
        """按照宽度进行所需比例缩放"""
        im = Image.open(infile)
        (x, y) = im.size
        lv = round(x / image_size, 2) + 0.01
        x_s = int(x // lv)
        y_s = int(y // lv)
        print("x_s", x_s, y_s)
        out = im.resize((x_s, y_s), Image.LANCZOS)
        return out
    
    
    def get_new_img_xy(infile, image_size):
        """返回一个图片的宽、高像素"""
        im = Image.open(infile)
        (x, y) = im.size
        lv = round(x / image_size, 2) + 0.01
        x_s = x // lv
        y_s = y // lv
        # print("x_s", x_s, y_s)
        # out = im.resize((x_s, y_s), Image.ANTIALIAS)
        return x_s, y_s
    
    
    # 定义图像拼接函数
    def image_compose(image_colnum, image_size, image_rownum, image_names, image_save_path, x_new, y_new):
        to_image = Image.new('RGB', (image_colnum * x_new, image_rownum * y_new))  # 创建一个新图
        # 循环遍历,把每张图片按顺序粘贴到对应位置上
        total_num = 0
        for y in range(1, image_rownum + 1):
            for x in range(1, image_colnum + 1):
                from_image = resize_by_width(image_names[image_colnum * (y - 1) + x - 1], image_size)
                # from_image = Image.open(image_names[image_colnum * (y - 1) + x - 1]).resize((image_size,image_size ), Image.ANTIALIAS)
                to_image.paste(from_image, ((x - 1) * x_new, (y - 1) * y_new))
                total_num += 1
                if total_num == len(image_names):
                    break
        to_image.save(image_save_path)  # 保存新图
    
    
    def get_image_list_fullpath(dir_path):
        file_name_list = os.listdir(dir_path)
        image_fullpath_list = []
        for file_name_one in file_name_list:
            file_one_path = os.path.join(dir_path, file_name_one)
            if os.path.isfile(file_one_path):
                image_fullpath_list.append(file_one_path)
            else:
                img_path_list = get_image_list_fullpath(file_one_path)
                image_fullpath_list.extend(img_path_list)
        return image_fullpath_list
    
    
    def merge_images(image_dir_path, image_size, image_colnum,image_save_path):
        # 获取图片集地址下的所有图片名称
        image_fullpath_list = get_image_list_fullpath(image_dir_path)
        print("图片列表", len(image_fullpath_list), image_fullpath_list)
        # image_rownum = 4  # 图片间隔,也就是合并成一张图后,一共有几行
        image_rownum_yu = len(image_fullpath_list) % image_colnum
        if image_rownum_yu == 0:
            image_rownum = len(image_fullpath_list) // image_colnum
        else:
            image_rownum = len(image_fullpath_list) // image_colnum + 1
    
        x_list = []
        y_list = []
        for img_file in image_fullpath_list:
            img_x, img_y = get_new_img_xy(img_file, image_size)
            x_list.append(img_x)
            y_list.append(img_y)
    
        print("x_list", sorted(x_list))
        print("y_list", sorted(y_list))
        x_new = int(x_list[len(x_list) // 5 * 4])
        y_new = int(y_list[len(y_list) // 5 * 4])
        print(" x_new, y_new", x_new, y_new)
        image_compose(image_colnum, image_size, image_rownum, image_fullpath_list, image_save_path, x_new, y_new)  # 调用函数
        print('图片已经合成了')
    
    
    if __name__ == '__main__':
        image_dir_path = '刘亦菲/'  # 图片集地址
        image_size = 1200  # 每张小图片的大小
        image_colnum = 3   # 合并成一张图后,一行有几个小图
        image_save_path = '刘亦菲/合成图.png'  # 图片转换后的地址
        merge_images(image_dir_path, image_size, image_colnum, image_save_path)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91

    在这里插入图片描述

  • 相关阅读:
    类与对象(十六)----封装encapsulation
    电脑重装系统Win10“initpki.dll”加载失败怎么办?
    山西电力市场日前价格预测【2023-09-13】
    leetcode 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
    供应链 | 零售商-供应商柔性承诺契约:一种鲁棒优化方法 (二)
    OneFlow v0.5.0正式上线:四大特性实现轻快上手,高效、易用从此兼得
    【Vue2】Vue2-简易图书借阅管理
    从零开始的Django框架入门到实战教程(内含实战实例) - 08 用户界面(内含图形验证码的生成和校验详解)(学习笔记)
    Linux网络编程 I/O模型和服务器模型
    NOIP 2022 游记
  • 原文地址:https://blog.csdn.net/gxz888/article/details/134288911