Python基于identicon库创建类似Github上用的头像功能

(编辑:jimmy 日期: 2024/9/27 浏览:2)

本文实例讲述了Python基于identicon库创建类似Github上用的头像功能。分享给大家供大家参考,具体如下:

Identicon在很多大型IT网站上可以见到,比如Github,Sourceforge,Stackoveflow等等, 刚刚注册的账号的个人信息的默认图标"_blank" href="https://github.com/shnjp/identicon" rel="external nofollow" >https://github.com/shnjp/identicon 下载identicon.py,
2. 将identicon.py放到你能找到的地方.
3. 然后在相同的目录里新建一个test.py
4. 打开test.py

输入以下代码:

import identicon
img= identicon.render_identicon('123123', 16)
img.show()

这样就能够看到图像了, 大小是3*16=48. 即图片大小是48X48像素的尺寸.如果报错了, 就是你没有安装python的图像处理模块PIL, 安装之后再试.

保存图像

上面的代码只是简单的使用, 还没保存.保存代码如下:

import identicon
img= identicon.render_identicon('123123', 16)
img.save('123123.png')

会在相同的目录保存一个png格式的图片

批量生成图片

代码如下:

import identicon
def gen_identicon(code,size):
  img= identicon.render_identicon(code, 16)
  #img.show()
  img.save('%s_%s.png'%(code,size))
for x in xrange(10000000,10000000+5):
  gen_identicon(x, 16)
for x in xrange(20000000,20000000+5):
  gen_identicon(x, 16)
for x in xrange(40000000,40000000+5):
  gen_identicon(x, 16)
for x in xrange(80000000,80000000+5):
  gen_identicon(x, 16)
for x in xrange(160000000,160000000+5):
  gen_identicon(x, 16)

说明:

identicon.render_identicon(code, 16)

这里的code是一个数值, 或者字符串数值, 如果code比较大, 比如code=10000000, 生成的图片就是彩色的. code比较小, 比如code=1~100之间的, 生成的图片就是黑白色的.

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。