PIL:Python Imaging Library(图像处理标准库)和Qrcode:二维码生成
2024-09-07 20:03:45
安装PIL
Mac或Linux安装命令:sudo easy_install PIL
如果报错:fatal error: 'freetype/fterrors.h' file not found
Mac下所依赖的FreeType链接变更问题,解决如下:
ln -s /usr/local/include/freetype2 /usr/local/include/freetype
sudo easy_install -U pil
如果出现问题:decoder jpeg not available
卸载你的PIL
下载libjpeg:http://www.ijg.org/files/jpegsrc.v7.tar.gz
安装libjpeg:
$ tar zxvf jpegsrc.v7.tar.gz
$ cd jpeg-7
$ ./configure --enable-shared --enable-static
$ make
$ sudo make install
下载PIL: http://effbot.org/downloads/Imaging-1.1.7.tar.gz
编辑setup.py,设置:
JPEG_ROOT = libinclude("/usr/local")
ZLIB_ROOT = libinclude("/usr/local")
安装PIL
$ python setup.py build
$ sudo python setup.py install --optimize=1
$ python selftest.py - Run the selftest to confirm PIL is installed ok
$ sudo python setup.py install
重启IDLE
使用例可参考:
详细了解PIL,请参考PIL官方文档:
http://effbot.org/imagingbook/
Qrcode:生成二维码
安装 qrcode:sudo easy_install qrcode
测试代码(在命令行输入):qr "test" > test.png
python代码:
import Image
import qrcode qr = qrcode.QRCode(
version=2,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=1
)
qr.add_data("http://wx341.vshangtong.com/wxapi.php?ac=photo&tid=341&from=timeline&isappinstalled=0")
qr.make(fit=True) img = qr.make_image()
img = img.convert("RGBA") icon = Image.open("pvg.png") img_w, img_h = img.size
factor = 4
size_w = int(img_w / factor)
size_h = int(img_h / factor) icon_w, icon_h = icon.size
if icon_w > size_w:
icon_w = size_w
if icon_h > size_h:
icon_h = size_h
icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS) w = int((img_w - icon_w) / 2)
h = int((img_h - icon_h) / 2)
img.paste(icon, (w, h), icon) img.save("dhqme_qrcode.png")
结果:
最新文章
- xamarin绑定原生库的一些坑
- The .NET of Tomorrow
- 前端小知识点---html换行被解析为空格的相关知识
- fasicon.js (无二维码版,如需要js创建二维码版本可联系我:770959294@qq.com)
- STM32重映射(PinRemap)的使用,注意!
- Scrum Meeting 3-20151203
- svn 403 Forbidden
- mac os x用macport安装redis
- C#设计模式——单件模式(Singleton Pattern)
- 【iBoard电子学堂】【iCore双核心板】资料光盘A盘更新,版本号为A6
- Oracle 手动收集统计信息
- poj 1236 Network of Schools【强连通求孤立强连通分支个数&;&;最少加多少条边使其成为强连通图】
- SQL Developer 4.1.3
- 学习web前端技术的笔记,仅供自己查阅备忘,移动对font-size的控制(并非原创)
- sshpass 实现shell脚本直接加载密登录服务器
- nginx php上传大文件的设置(php-fpm)
- DOM事件阶段以及事件捕获与事件冒泡先后执行顺序
- [ 转载 ] Android JNI(一)——NDK与JNI基础
- VMware的存储野心(下):虚拟卷和闪存缓存
- STN1110 Multiprotocol OBD to UART Interpreter