国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python使用PIL實現多張圖片垂直合并

2020-02-16 00:39:20
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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']) 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 博湖县| 安阳市| 莆田市| 罗定市| 庆云县| 龙州县| 峨眉山市| 红原县| 黄冈市| 临沭县| 尚志市| 册亨县| 海伦市| 米林县| 凤阳县| 上思县| 翼城县| 天峻县| 新安县| 沂南县| 锡林郭勒盟| 策勒县| 康乐县| 房产| 梁河县| 怀来县| 韩城市| 九台市| 鄂托克旗| 平定县| 乐陵市| 富民县| 留坝县| 得荣县| 香河县| 名山县| 新昌县| 保亭| 江口县| 霍林郭勒市| 安吉县|