zoukankan      html  css  js  c++  java
  • 【Python爬虫】制作微信好友头像拼接大图

    爬取微信好友头像拼接为大图

    代码:

    #导入对应的包
    from numpy import *
    import itchat
    import PIL.Image as Image
    import os
    
    def get_imgs():#完成主要的下载头像的任务
        #使用热登录(已经登录的程序,再一次运行程序不需要扫描验证码),执行这一步就会有二维码需要扫描登录
        itchat.auto_login(hotReload=True)
        #获取朋友列表,返回字典类型的数据集,获取好友的索引数
        friends = itchat.get_friends(update=True)[0:256]
        #为图片命名的变量
        num = 0
        #遍历好友列表
        for i in friends:
            #获取好友的头像
            img = itchat.get_head_img(userName=i["UserName"])
            #在项目文件的主创建一个user文件用于放头像,并写入对应的图片名,空白的
            if not os.path.exists('user'):os.makedirs("user")
            fileImage = open( "user\" + str(num) + ".jpg",'wb')
            #将获取到的头像文件写到创建的图片文件中
            fileImage.write(img)
            #关闭资源
            fileImage.close()
            num += 1
    #制作大的大头像
    def get_big_img():
        #获取usr文件夹所有文件的名称
        pics = os.listdir("user")
        numPic = len(pics)
        #创建图片大小
        toImage = Image.new("RGB", (800, 800))
        #用于图片的位置
        x = 0
        y = 0
        #遍历user文件夹的图片
        for i in pics:
            try:
                #依次打开图片
                img = Image.open("user\{}".format(i))
            except IOError:
                print("Error: 没有找到文件或读取文件失败",i)
            else:
                #重新设置图片的大小
                img = img.resize((50, 50), Image.ANTIALIAS)
                #将图片粘贴到最后的大图片上,需要注意对应的位置
                toImage.paste(img, (x * 50, y * 50))
                #设置每一行排16个图像
                x += 1
                if x == 16:
                    x = 0
                    y += 1
        #保存图片为bigPhoto.jpg
        toImage.save("user\" +"bigPhoto.jpg")
        #将做好图片发送东自己的手机上
        itchat.send_image("user\" +"bigPhoto.jpg", 'filehelper')
    #定义执行的主函数
    def main():
        get_imgs()
        get_big_img()
        
    #运行
    if __name__=="__main__":
        main()

    性能提升---线程池并发

    from numpy import *
    import itchat
    import PIL.Image as Image
    import os,uuid,math
    from concurrent.futures import ThreadPoolExecutor
    
    def get_info(friend):
        # 获取好友的头像
        return itchat.get_head_img(userName=friend["UserName"])
    
    def downloadImg(res):
        img = res.result()
        # 在user文件夹写入对应的图片名
        fileImage = open("user\" + str(uuid.uuid4()) + ".jpg", 'wb')
        # 将获取到的头像文件写到创建的图片文件中
        fileImage.write(img)
        # 关闭资源
        fileImage.close()
    
    # 制作大的大头像
    def get_big_img():
        # 获取usr文件夹所有文件的名称
        pics = os.listdir("user")
        numPic = len(pics)
        img_width = math.floor(sqrt(numPic))
        img_height = math.ceil(sqrt(numPic))
        # 创建图片大小
        toImage = Image.new("RGB", (img_width*50, img_height*50))
        # 用于图片的位置
        x = 0
        y = 0
        # 遍历user文件夹的图片
        for i in pics:
            try:
                # 依次打开图片
                img = Image.open("user\{}".format(i))
            except IOError:
                print("Error: 没有找到文件或读取文件失败", i)
            else:
                # 重新设置图片的大小
                img = img.resize((50, 50), Image.ANTIALIAS)
                # 将图片粘贴到最后的大图片上,需要注意对应的位置
                toImage.paste(img, (x * 50, y * 50))
                # 设置每一行排16个图像
                x += 1
                if x == 16:
                    x = 0
                    y += 1
        # 保存图片为bigPhoto.jpg
        toImage.save("user\" + "bigPhoto.jpg")
        # 将做好图片发送东自己的手机上
        itchat.send_image("user\" + "bigPhoto.jpg", 'filehelper')
    
    # 运行
    if __name__ == "__main__":
        # 创建user文件夹
        if not os.path.exists('user'): os.makedirs("user")
        # 使用热登录(已经登录的程序,再一次运行程序不需要扫描验证码),执行这一步就会有二维码需要扫描登录
        itchat.auto_login(hotReload=True)
        # 获取朋友列表,返回字典类型的数据集,获取好友的索引数
        friends = itchat.get_friends(update=True)[0:256]
    
        pool = ThreadPoolExecutor()
        # 遍历好友列表
        for friend in friends:
            v = pool.submit(get_info, friend)
            v.add_done_callback(downloadImg)
    
        pool.shutdown(wait=True)
        get_big_img()

    知识点:

    1、itchat 操作微信

    2、线程池实现并发

    3、uuid 给图片命名

    4、根据头像图片数量自动计算大图的宽高尺寸

  • 相关阅读:
    POJ 2342 树形DP入门题
    《4》CentOS7.0+OpenStack+kvm云平台部署—配置Nova
    GDI+ 填充背景时,非常多时候不起作用,GDI、GDI+配合运用
    Jave中System.getProperty()获取的值
    NYOJ128 前缀式计算(栈的运用)
    Web安全測试二步走
    一个SQL update语句
    C#时间格式之GMT时间的格式
    粘包的处理
    socket对于大数据的发送和接收
  • 原文地址:https://www.cnblogs.com/XJT2018/p/11038523.html
Copyright © 2011-2022 走看看