关于javascript:Python-生成你的朋友圈九宫格图片

7次阅读

共计 2523 个字符,预计需要花费 7 分钟才能阅读完成。

对于微信之前写过以下文章,有趣味能够点击查看:

如何导出你的微信语音

c 盘空间又满了?微信清理神器帮你开释空间

微信撤回的音讯也能看到!

如何备份可能被删的公众号文章和网页

如何在电脑上登陆多个微信

如何发一条空白的朋友圈

那些你可能不晓得的微信奇技淫巧

一键生成你的微信社交数据报告

你可能在朋友圈看过九宫格图片(把一张图片依照比例分成九份),就像这样的:

还有微博九宫格图 https://weibo.com/2717930601/…:

这种九宫格图片怎么发的呢?上面用 Python 搞定它,Python 是门很简略实用的语言,即便不做开发工作也能够学习下,比方之前的下载抖音 一键批量下载抖音无水印视频,下载公众号文章一键下载公众号所有文章,导出文件反对 PDF,HTML,Markdown,Excel,chm 等格局 都是用的 Python,之后我会写一篇如何应用 Python 来抓取数据,代码非常简单,只有你意识英文字母就会用。

PIL 解决图片

先用 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 打包 exe

打包用的工具是 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,上传图片下载即可。

公众号 苏生不惑

正文完
 0