zoukankan      html  css  js  c++  java
  • Python3 生成微信好友头像的图片合集

    刚才在github上看到一个大神写的生成微信好友图像集合的脚本
    自己运行了一下发现挺好玩的

    原帖地址:https://github.com/aloneZERO/py-party/tree/master/wechat-imgs
    
    #!python3
    # coding: utf-8
    
    import itchat
    import os
    
    from PIL import Image
    import math
    
    
    # 首先登陆python版本微信itchat,生成二维码
    # itchat.auto_login(enableCmdQR=True)
    itchat.auto_login()
    
    # 获取好友列表
    friends = itchat.get_friends(update=True)[0:]
    
    # 以自己的用户名加密码创建文件夹来存储图片
    user = friends[0]["UserName"]
    print("User Code: "+user)
    os.mkdir(user)
    
    # 使用itchat的get_head_img(userName=none)函数
    # 爬取好友列表的头像,并下载到本地
    num = 0
    for i in friends:
    	img = itchat.get_head_img(userName=i["UserName"])
    	with open(user + "/" + str(num) + ".jpg", 'wb') as fileImage:
    		fileImage.write(img)
    	num += 1
    
    # 计算好友数量
    pics = os.listdir(user)
    numPic = len(pics)
    print("好友总数:"+str(numPic))
    
    # 计算每张头像缩小后的边长(默认为正方形)
    eachsize = int(math.sqrt(float(640 * 640) / numPic))
    # print(eachsize)
    
    # 计算合成图片每一边分为多少小边
    numline = int(640 / eachsize)
    toImage = Image.new('RGBA', (640, 640))
    # print(numline)
    
    # 缩小并拼接图片
    x, y = 0, 0
    for i in pics:
    	try:
    		# 打开图片
    		img = Image.open(user + "/" + i)
    	except IOError:
    		print("Error: 没有找到文件或读取文件失败")
    	else:
    		# 缩小图片
    		img = img.resize((eachsize, eachsize), Image.ANTIALIAS)
    		# 拼接图片
    		toImage.paste(img, (x * eachsize, y * eachsize))
    		x += 1
    		if x == numline:
    			x = 0
    			y += 1
    
    # 保存拼接好的图片
    # 通过文件助手发送给自己
    toImage.save("funny.jpg")
    itchat.send_image("funny.jpg", 'filehelper')
    print("好友头像拼接完毕,快去查看吧~")
    
  • 相关阅读:
    URL和URI之间的区别
    nom Uncaught Error: Cannot find module 'babel-runtime/regenerator'
    PHP中使用curl获取头信息headers的一些笔记
    Typora中自定义命令上传图片
    iOS链接big sur弹出“文件找不到”
    云开发cannot find module wx-server-sdk
    关于Laravel框架中Guard的底层实现
    PHP8中字符串与数字的比较更智能
    git refusing to merge unrelated histories
    k8s入门-资源文件实现
  • 原文地址:https://www.cnblogs.com/xuyuQAQ/p/8426021.html
Copyright © 2011-2022 走看看