python使用PIL實(shí)現(xiàn)多張圖片垂直合并
本文實(shí)例為大家分享了python實(shí)現(xiàn)多張圖片垂直合并的具體代碼,供大家參考,具體內(nèi)容如下
# coding: utf-8 # image_merge.py # 圖片垂直合并 # http://www.redicecn.com # redice@163.com import os import Image def image_resize(img, size=(1500, 1100)): """調(diào)整圖片大小 """ 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 # 計(jì)算合成后圖片的寬度(以最寬的為準(zhǔn))和高度 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 # 產(chǎn)生一張空白圖 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'])
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python使用matplotlib畫餅狀圖
- Python使用matplotlib的pie函數(shù)繪制餅狀圖功能示例
- python實(shí)現(xiàn)彩色圖轉(zhuǎn)換成灰度圖
- python實(shí)現(xiàn)圖片彩色轉(zhuǎn)化為素描
- python實(shí)現(xiàn)多張圖片拼接成大圖
- Python數(shù)據(jù)可視化之畫圖
- Python數(shù)據(jù)可視化教程之Matplotlib實(shí)現(xiàn)各種圖表實(shí)例
- python+opencv 讀取文件夾下的所有圖像并批量保存ROI的方法
- python將處理好的圖像保存到指定目錄下的方法
- Python餅狀圖的繪制實(shí)例
相關(guān)文章
利用Python內(nèi)置庫實(shí)現(xiàn)創(chuàng)建命令行應(yīng)用程序
Python?有一個(gè)叫做argparse的內(nèi)置庫,可以用它來創(chuàng)建一個(gè)命令行界面。本文將詳解如何利用argparse實(shí)現(xiàn)創(chuàng)建一個(gè)命令行應(yīng)用程序,需要的可以參考一下2022-06-06Python可視化mhd格式和raw格式的醫(yī)學(xué)圖像并保存的方法
今天小編就為大家分享一篇Python可視化mhd格式和raw格式的醫(yī)學(xué)圖像并保存的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01pandas 如何保存數(shù)據(jù)到excel,csv
這篇文章主要介紹了pandas 如何保存數(shù)據(jù)到excel,csv的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07基于Python使用永中文檔轉(zhuǎn)換服務(wù)的方式
這篇文章主要介紹了基于Python使用永中文檔轉(zhuǎn)換服務(wù)的方式,本文給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05如何用pandas讀取一個(gè)文件或某個(gè)文件夾下所有文件
這篇文章主要介紹了如何用pandas讀取一個(gè)文件或某個(gè)文件夾下所有文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02python 不關(guān)閉控制臺(tái)的實(shí)現(xiàn)方法
在win32下,雙擊python程序會(huì)打開dos窗口,但是執(zhí)行完畢后就會(huì)關(guān)閉,看不到輸出的結(jié)果2011-10-10