利用Python實(shí)現(xiàn)朋友圈中的九宮格圖片效果
前言
大家應(yīng)該經(jīng)常在朋友圈看到有人發(fā)九宮格圖片,其實(shí)質(zhì)就是將一張圖片切成九份,然后在微信中一起發(fā)這九張圖即可。
說到切圖,Python 就可以實(shí)現(xiàn),主要用到的 Python 庫為 Pillow,安裝使用 pip install pillow 即可,切圖的主要步驟如下:
打開要處理的圖片 判斷打開的圖片是否為正方形 如果是正方形,就進(jìn)行九等分,如果不是正方形,先用白色填充為正方形,再進(jìn)行九等分 保存處理完的圖片主要實(shí)現(xiàn)代碼如下:
# 填充新的 imagedef fill_image(image): width, height = image.size _length = width if height > width: _length = height new_image = Image.new(image.mode, (_length, _length), color=’white’) if width > height: new_image.paste(image, (0, int((_length - height) / 2))) else: new_image.paste(image, (int((_length - width) / 2), 0)) return new_image# 裁剪 imagedef cut_image(image): width, height = image.size _width = int(width / 3) box_list = [] for i in range(0, 3): for j in range(0, 3): box = (j * _width, i * _width, (j + 1) * _width, (i + 1) * _width) box_list.append(box) image_list = [image.crop(box) for box in box_list] return image_list# 將 image 列表的里面的圖片保存def save_images(image_list, res_dir): index = 1 if not os.path.exists(res_dir): os.mkdir(res_dir) for image in image_list: new_name = os.path.join(res_dir, str(index) + ’.png’) image.save(new_name, ’PNG’) index += 1
原圖:
效果圖:
總結(jié)
到此這篇關(guān)于利用Python實(shí)現(xiàn)朋友圈中的九宮格圖片效果的文章就介紹到這了,更多相關(guān)Python實(shí)現(xiàn)朋友圈九宮格圖片內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. html中的form不提交(排除)某些input 原創(chuàng)2. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享3. ASP常用日期格式化函數(shù) FormatDate()4. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效5. asp.net core項(xiàng)目授權(quán)流程詳解6. XMLHTTP資料7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. CSS3中Transition屬性詳解以及示例分享9. jsp文件下載功能實(shí)現(xiàn)代碼10. 開發(fā)效率翻倍的Web API使用技巧
