黑松山资源网 Design By www.paidiu.com
本文实例为大家分享了python实现多张图片垂直合并的具体代码,供大家参考,具体内容如下
# coding: utf-8 # image_merge.py # 图片垂直合并 # http://www.redicecn.com # redice@163.com import os import Image def image_resize(img, size=(1500, 1100)): """调整图片大小 """ try: if img.mode not in ('L', 'RGB'): img = img.convert('RGB') img = img.resize(size) except Exception, e: pass return img def image_merge(images, output_dir='output', output_name='merge.jpg', \ restriction_max_width=None, restriction_max_height=None): """垂直合并多张图片 images - 要合并的图片路径列表 ouput_dir - 输出路径 output_name - 输出文件名 restriction_max_width - 限制合并后的图片最大宽度,如果超过将等比缩小 restriction_max_height - 限制合并后的图片最大高度,如果超过将等比缩小 """ max_width = 0 total_height = 0 # 计算合成后图片的宽度(以最宽的为准)和高度 for img_path in images: if os.path.exists(img_path): img = Image.open(img_path) width, height = img.size if width > max_width: max_width = width total_height += height # 产生一张空白图 new_img = Image.new('RGB', (max_width, total_height), 255) # 合并 x = y = 0 for img_path in images: if os.path.exists(img_path): img = Image.open(img_path) width, height = img.size new_img.paste(img, (x, y)) y += height if restriction_max_width and max_width >= restriction_max_width: # 如果宽带超过限制 # 等比例缩小 ratio = restriction_max_height / float(max_width) max_width = restriction_max_width total_height = int(total_height * ratio) new_img = image_resize(new_img, size=(max_width, total_height)) if restriction_max_height and total_height >= restriction_max_height: # 如果高度超过限制 # 等比例缩小 ratio = restriction_max_height / float(total_height) max_width = int(max_width * ratio) total_height = restriction_max_height new_img = image_resize(new_img, size=(max_width, total_height)) if not os.path.exists(output_dir): os.makedirs(output_dir) save_path = '%s/%s' % (output_dir, output_name) new_img.save(save_path) return save_path if __name__ == '__main__': image_merge(images=['900-000-000-0501a_b.jpg', '900-000-000-0501b_b.JPG', '1216005237382a_b.jpg'])
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月07日
2024年10月07日
- 群星.1991-音乐工厂1·皇后大道东【音乐工厂】【WAV+CUE】
- 群星.1992-音乐工厂2·首都【音乐工厂】【WAV+CUE】
- 群星.1994-音乐工厂3·儿童乐园【音乐工厂】【WAV+CUE】
- 胡鸿钧.2021-ex:CHANGE【星梦娱乐】【WAV+CUE】
- 班得瑞原装进口《第一张新世纪专辑:仙境》1CD[APE/CUE分轨][292.3MB]
- 伍佰《摇滚教父 伍佰黄金精选 2CD》[WAV+CUE][990MB]
- 班得瑞原装进口《第二张新世纪专辑:寂静山林》1CD[APE/CUE分轨][327.1MB]
- Beyond.1989-真的见证(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 陈怡婷.2024-予你的心谁来赔【福茂】【FLAC分轨】
- 陈冠希.2003-HITS.OR.MISSES【英皇娱乐】【WAV+CUE】
- 班得瑞原装进口《第三张新世纪专辑:春野》1CD[APE/CUE分轨][278.9MB]
- 班得瑞原装进口《第四张新世纪专辑:蓝色天际》1CD[APE/CUE分轨][284.8MB]
- 班得瑞原装进口《第五张新世纪专辑:迷雾森林+EP》1CD[APE/CUE分轨][334.2MB]
- 田震.1997-顺其自然(日版)【红星生产社】【WAV+CUE】
- 小虫.1996-想得太美【滚石】【WAV+CUE】