02.用Python来合并图片

目的

为第三步制作照片墙功能热身。(照片墙功能中用到了 PIL paste方法)

自学知识点(百度)

  • PIL paste方法

代码:
将第一个任务分解出来的图片放到项目的images目录中。

import os
from PIL import Image

'''
把目录下的图片拼接成一张大图片
'''

# 图片压缩后的大小
width_i = 200
height_i = 300

# 每行每列显示图片数量
line_max = 5
row_max = 5

# 参数初始化
all_path = []
num = 0
pic_max = line_max * row_max

for filename in os.listdir('./images'):
    if filename.endswith('jpg') or filename.endswith('png'):
        all_path.append(os.path.join('./images', filename))

toImage = Image.new('RGBA', (width_i * line_max, height_i * row_max))

for i in range(0, row_max):
    for j in range(0, line_max):

        # 图片比计划的少
        if num >= len(all_path):
            print("breadk")
            break

        pic_fole_head = Image.open(all_path[num])
        width, height = pic_fole_head.size

        tmppic = pic_fole_head.resize((width_i, height_i))

        loc = (int(i % line_max * width_i), int(j % line_max * height_i))

        # print("第" + str(num) + "存放位置" + str(loc))
        toImage.paste(tmppic, loc)
        num = num + 1

    # 图片比计划的多
    if num >= pic_max:
        break

print(toImage.size)
toImage.save('merged.png')


转载链接:https://www.jianshu.com/p/321b14e413d2

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值