關於微信以前寫過如下文章,有興趣能夠點擊查看:python
如何導出你的微信語音小程序
一鍵生成你的微信社交數據報告 spa
你可能在朋友圈看過九宮格圖片(把一張圖片按照比例分紅九份),就像這樣的:
還有微博九宮格圖 https://weibo.com/2717930601/... :命令行
這種九宮格圖片怎麼發的呢?下面用Python搞定它,Python是門很簡單實用的語言,即便不作開發工做也能夠學習下,好比以前的下載抖音 一鍵批量下載抖音無水印視頻 ,下載公衆號文章一鍵下載公衆號全部文章,導出文件支持PDF,HTML,Markdown,Excel,chm等格式 都是用的Python,以後我會寫一篇如何使用Python來抓取數據,代碼很是簡單,只要你認識英文字母就會用。
先用pip安裝切割圖片的庫 PIL pip install Pillow
, 而後編輯代碼:
from PIL import Image,ImageSequence,ImageFilter pic = input("請輸入圖片文件名:") im = Image.open(pic) width = im.size[0]//3 height = im.size[1]//3 x = 0 y = 0 filename = 1#保存的文件名 for i in range(3): for j in range(3): crop = im.crop((x, y, x+width, y+height)) crop.save(str(filename) + '.jpg') x += width filename += 1 x = 0 y += height
直接命令行執行 python pic.py ,輸入圖片文件名便可在本地生成9張小圖。
還能夠將代碼打包爲exe可執行文件,這樣不用安裝Python就能夠運行了。
打包用的工具是pyinstaller,先pip install pyinstaller
安裝它,而後pyinstaller -F pic.py
,不過我運行的時候出錯了。
for real_module_name, six_module_name in real_to_six_module_name.items(): AttributeError: 'str' object has no attribute 'items'
谷歌了下須要升級庫。
λ pip install -U setuptools Collecting setuptools Downloading https://files.pythonhosted.org/packages/6d/ed/52e05469314a266f68d9f251a8c1ab7a21a03327b1724056e3eea654bfd1/setuptools-50.0.3-py3-none-any.whl (784kB) Installing collected packages: setuptools Found existing installation: setuptools 41.2.0 Uninstalling setuptools-41.2.0: Successfully uninstalled setuptools-41.2.0 Successfully installed setuptools-50.0.3 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple # 更新pip版本 python -m pip install -U pip λ pip uninstall enum34 Uninstalling enum34-1.1.6: Would remove: d:\python\lib\site-packages\enum34-1.1.6.dist-info\* d:\python\lib\site-packages\enum\* Proceed (y/n)? y Successfully uninstalled enum34-1.1.6 """
再次執行pyinstaller -F pic.py
終於成功了,不過生成的exe文件有點大(20多MB,公衆號後臺回覆 朋友圈
獲取exe文件),雙擊exe文件輸入文件名就能夠運行了。 https://www.lanzoux.com/iWtJN...
60106 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully. 60112 INFO: Bootloader d:\python\lib\site-packages\PyInstaller\bootloader\Windows-32bit\run.exe 60114 INFO: checking EXE 60115 INFO: Building EXE because EXE-00.toc is non existent 60115 INFO: Building EXE from EXE-00.toc 60115 INFO: Appending archive to EXE d:\download\dist\pic.exe 60279 INFO: Building EXE from EXE-00.toc completed successfully.
PIL除了切割圖片還能夠對照片去色。
img = Image.open("jay.jpg") img2 = img.convert("L") img2.save("jay2.jpg")
去色效果圖:
對照片旋轉90度。
img3 = img.rotate(90) img3.save("jay_rotate.jpg")
旋轉效果圖:
對照片翻轉。
img4 = img.transpose(Image.FLIP_LEFT_RIGHT) img4.save("jay_transpose.jpg")
翻轉效果圖,周杰倫七里香幾個字翻過來了:
除了使用Python 也有在線工具和微信小程序能夠生成九宮格圖片 https://www.dute.org/image-clip ,上傳圖片下載便可。