【Python图像】给你的头像+1
早些年,微信朋友圈有段时间非常流行这个头像+1的套路,简直逼死强迫症。
将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。 类似于图中效果

涉及知识:
Python Imaging Library (PIL)图像库,Image, ImageDraw, ImageFont模块
话不多说先上代码:
from PIL import Image, ImageDraw, ImageFont def add_num(img):
draw = ImageDraw.Draw(img)
myfont = ImageFont.truetype("C:\\WINDOWS\\Fonts\\SIMYOU.TTF", 200)
width, height = img.size
draw.ellipse((width-200,0,width,200),fill="red",outline ="red")
draw.text((width-150, 15), '', font=myfont, fill="white") img.save('result.jpg','jpeg') image = Image.open('yoyo.jpg')
add_num(image)
其中ImageFont.truetype(file,size) ? Font instance
含义:加载一个TrueType或者OpenType字体文件,并且创建一个字体对象。这个函数从指定的文件加载了一个字体对象,并且为指定大小的字体创建了字体对象。
在windows系统中,如果指定的文件不存在,加载器会顺便看看windows的字体目录下是否存在。
绘制效果:


过程中可能会出现的问题:
首先是PIL库的安装。我用的是pycharm,Python版本2.7。第一次装也是装了几次才成功,主要原因是,通过pycharm直接安装,里面装的版本只有1.1.6的,安装之后,运行不了,会提示你找不到合适的Python版本。

然后我就去网上查了下,知道了PIL库主要2.5 2.6用的比较多,2.7需要装最新版的库。
The following downloads are currently available:
PIL 1.1.7
- Python Imaging Library 1.1.7 Source Kit (all platforms) (November 15, 2009)
- Python Imaging Library 1.1.7 for Python 2.4 (Windows only)
- Python Imaging Library 1.1.7 for Python 2.5 (Windows only)
- Python Imaging Library 1.1.7 for Python 2.6 (Windows only)
- Python Imaging Library 1.1.7 for Python 2.7 (Windows only)
Additional downloads may be found here.
PIL 1.1.6
- Python Imaging Library 1.1.6 Source Kit (all platforms) (440k TAR GZ) (December 3, 2006)
- Python Imaging Library 1.1.6 for Python 2.2 (Windows only)
- Python Imaging Library 1.1.6 for Python 2.3 (Windows only)
- Python Imaging Library 1.1.6 for Python 2.4 (Windows only)
- Python Imaging Library 1.1.6 for Python 2.5 (Windows only)
- Python Imaging Library 1.1.6 for Python 2.6 (Windows only)
然后下载了2.7可以用的1.1.7,双击安装,然后再Pycharm - File - Setting - Project Interpreter - Available Packages 然后面板左下角的Manage Repositories中添加,PIL安装的路径,我的路径是(C:\Python27\Lib\site-packages\PIL)。
退回到settings界面,可以看到PIL库已经安装好了。

在程序运行的过程中又出现了错误提示PIL库中:The _imagingft C module is not installed错误。
提示有个C模块咩有安装,明明是在官网下的,怎么会出现这样的情况呢,又查了一番,原来是PIL库中,有一个模块那没有编译,导致提示C模块找不到,附上一个编译过的1.1.7PIL库。(http://pan.baidu.com/s/1pLCTb1h)
安装之前,记得要将原来的版本卸载掉。
【Python图像】给你的头像+1的更多相关文章
- python Django注册页面显示头像
python Django注册页面显示头像(views) def register(request): ''' 注册 :param request: :return: ''' if request.m ...
- 【Tool】Augmentor和imgaug——python图像数据增强库
Augmentor和imgaug--python图像数据增强库 Tags: ComputerVision Python 介绍两个图像增强库:Augmentor和imgaug,Augmentor使用比较 ...
- python图像、视频转字符画
python图像转字符画需要用到matplotlib.pyplot库,视频转字符画需要用到opencv库,这里的代码基于python 3.5 图像转字符画需要先将图像转为灰度图,转灰度图的公式是 gr ...
- [Python] 图像简单处理(PIL or Pillow)
前几天弄了下django的图片上传,上传之后还需要做些简单的处理,python中PIL模块就是专门用来做这个事情的. 于是照葫芦画瓢做了几个常用图片操作,在这里记录下,以便备用. 这里有个字体文件,大 ...
- python批量下载微信好友头像,微信头像批量下载
#!/usr/bin/python #coding=utf8 # 自行下载微信模块 itchat 小和QQ496631085 import itchat,os itchat.auto_login() ...
- Python图像全屏显示
需要在嵌入式设备上全屏显示图像,使用pil显示图像时,只能通过系统的图像浏览器显示.所以使用Python自带的tkinter import Tkinter as tk 这句在Python3中已经改 ...
- 【Python图像特征的音乐序列生成】使用Python生成简单的MIDI文件
这个全新的Python音乐创作系列,将会不定期更新.写作这个系列的初衷,是为了做一个项目<基于图像特征的音乐序列生成模型>,实时地提取照片特征,进行神经网络处理,生成一段音乐. 千里之行, ...
- Python之微信-微信好友头像合成
仔细看下图,你的头像就藏在里面哦!!! 有没有犯密集恐惧症?这并不震撼,如果你有 5000 位好友的话,做出来的图看着会更刺激些. 看完了图,你可能想知道这个图咋做出来的,不会是我闲着无聊把把好友头像 ...
- python 图像的离散傅立叶变换
图像(MxN)的二维离散傅立叶变换可以将图像由空间域变换到频域中去,空间域中用x,y来表示空间坐标,频域由u,v来表示频率,二维离散傅立叶变换的公式如下: 在python中,numpy库的fft模块有 ...
随机推荐
- [LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Product of Array Except Self 除本身之外的数组之积
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 归一化方法 Normalization Method
1. 概要 数据预处理在众多深度学习算法中都起着重要作用,实际情况中,将数据做归一化和白化处理后,很多算法能够发挥最佳效果.然而除非对这些算法有丰富的使用经验,否则预处理的精确参数并非显而易见. 2. ...
- Redis 常用操作
import org.junit.Before;import org.junit.Test;import redis.clients.jedis.Jedis;import java.util.Set; ...
- Android 轮换控件
首先是控件轮换 一.创建主布局 1.用到的控件是 TextSwitcher (文本轮换) 那么其他对应的也就是 ImageSwitcher (图片轮换) <LinearLayout xmlns: ...
- css-列表或标题的多级计数
利用css实现多级计数,比如1/1.1/1.1.1这种层层嵌套的计数,主要利用到counter-reset/counter-increment/counter/content/:before. 一.标 ...
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- POJ2096 Collecting Bugs
Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 5090 Accepted: 2529 Case Time Limit: ...
- theano scan optimization
selected from Theano Doc Optimizing Scan performance Minimizing Scan Usage performan as much of the ...