5月 09

raspberry pi 测试pypy 切割图片效率

为了方便安装pypy的第三方库,首先安装pip。
$ curl -O http://python-distribute.org/distribute_setup.py
$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ sudo pypy distribute_setup.py
$ sudo pypy get-pip.py

首先找了一个3M(3072×2304)的图片,缩小为300×300的大小
脚本:

# -*- coding:utf-8 -*-
# -------------------------------
# Filename:    .py
# Revision:    1.0
# Date:        2013-05-08
# Author:      simonzhang
# Web:         www.simonzhang.net
# Email:       simon-zzm@163.com
# -------------------------------


from PIL import Image


def main():
    get_data = Image.open('test.jpg')
    tmp = get_data.resize((300, 300),)
    tmp.save('test1.jpg', 'JPEG', quality=75)


if __name__ == '__main__':
    main()

在pypy上装PIL
$ sudo /usr/lib/pypy-upstream/bin/pip install PIL

运行脚本报错如下:

File “/usr/lib/pypy-upstream/site-packages/PIL/Image.py”, line 1290, in resize
self.load()
File “/usr/lib/pypy-upstream/site-packages/PIL/ImageFile.py”, line 189, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File “/usr/lib/pypy-upstream/site-packages/PIL/Image.py”, line 385, in _getdecoder
raise IOError(“decoder %s not available” % decoder_name)
IOError: decoder jpeg not available

不能调用系统库,之前在处理过这种问题http://www.simonzhang.net/?p=435
但是pypy比较复杂,兼容有问题。直接删除PIL,使用pillow。Pillow基础就是PIL只是兼容性强,更利于推广。
$ sudo /usr/lib/pypy-upstream/bin/pip uninstall PIL

$ sudo /usr/lib/pypy-upstream/bin/pip install pillow

注意必须使用
from PIL import Image

否则会报错
File “/usr/lib/pypy-upstream/site-packages/PIL/Image.py”, line 2020, in open
raise IOError(“cannot identify image file”)
IOError: cannot identify image file

在安装完pillow后没有产生PIL.pth文件,直接手动写一个。
$ sudo vim /usr/lib/pypy-upstream/site-packages/PIL.pth
内容是:PIL。

开始测试

time python cut_pic.py

real 0m2.841s
user 0m2.630s
sys 0m0.200s

time pypy cut_pic.py

real 0m5.144s
user 0m4.870s
sys 0m0.230s

图片产生大小如下
-rw-r–r– 1 pi pi 3588203 5月 8 16:02 test.jpg
-rw-r–r– 1 pi pi 21907 5月 8 16:10 test_pypy.jpg
-rw-r–r– 1 pi pi 21907 5月 8 16:09 test_python.jpg

直接使用python的效果更佳,不清楚原因。之后有版本升级了再做测试。

1月 08

python 连接hbase存、取图片

  连接hbase1.0.4需要使用Thrift,我用的是python2.6。
  安装thrift。下载地址https://dist.apache.org/repos/dist/release/thrift/0.9.0/thrift-0.9.0.tar.gz解压后安装命令。
在hbase服务器上,确保hbase服务已经启动。在thrift目录中,用管理员运行一下命令安装。
./configure
make
make install

  安装完毕生成hbase的client代码命令格式如下,
thrift –gen
登陆到hbase的权限进入
$ cd hbase/src/main/resources/org/apache/hadoop/hbase/thrift

生成python的
$ thrift –gen py Hbase.thrift
再生成一个C的学习备用,与本文无关
$ thrift –gen c_glib Hbase.thrift

将gen-py文件夹下的hbase文件夹拷贝到要连接hbase的服务器的python目录下,我用的是python2.6,自己手动安装的。命令如下
cp -R hbase /usr/local/lib/python2.6/site-packages/

拷贝完毕用import导入 hbase成功。开始写代码了。参考hbase里的例子在hbase/src/examples/中。

  我的任务就是把某个目录下,以jpg结尾的图片放到hbase里,因为图片名没有重复,所以用图片名做row name。hbase手动建表’hbase(main):013:0> create ‘img’, ‘data:”。

  首先统计一下照片的数量。这个image目录下只有jpg的图片,使用匹配只是备将来使用。下面只是测试脚本,不关心业务逻辑。

# find /image/ -name \*.jpg -type f |wc -l
13140

# du -s -h /image
303M /image/

  本地共有13140张照片共303M,写入hbase测试脚本如下:

#!/bin/bash 
# -------------------------------
# Revision:
# Date:        2012-12-11 
# Author:      simonzhang 
# Email:       simon-zzm@163.com 
# Web:         www.simonzhang.net 
# -------------------------------

import os
import re

from thrift.transport import TSocket  
from thrift.transport import TTransport  
from thrift.protocol import TBinaryProtocol  
   
from hbase import Hbase  
from hbase.ttypes import *

#### base set
find_path=(r'/image/',
           )

class HbaseWrite():
    def __init__(self):
        self.tableName = 'img'
        self.transport = TSocket.TSocket('192.168.100.100', 9090)
        self.transport = TTransport.TBufferedTransport(self.transport)
        self.transport.open()
        self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
        self.client = Hbase.Client(self.protocol)

    def createTable(self, tableName):
        col1 = ColumnDescriptor(name="data:",maxVersions=1)
        self.client.createTable(tableName,[col1])

    def write(self, PicPath, PicName):
        row = PicName.split('.')[0]
        _data = PicName.split('.')[1]
        PicData = open('%s/%s' % (PicPath, PicName), 'rb').read()
        # 此处需要注意格式,网上的格式报错,少个参数报错如下
        # TypeError: mutateRow() takes exactly 5 arguments (4 given)
        self.client.mutateRow(self.tableName, row, [Mutation(column="data:%s" % _data, value=PicData)], {})

    def read(self, tableName, PicName):
        row = PicName.split('.')[0]
        data_type = PicName.split('.')[1]
        get_data = self.client.get(tableName, row, 'data:%s' % data_type, {})[0]
        if get_data:
            return get_data.value
        else:
            return "Error"


def main(_path):
    WHB = HbaseWrite()
    WHB.createTable()
    find_file=re.compile(r"^[0-9a-z]*.jpg$")
    find_walk=os.walk(_path)
    for path,dirs,files in find_walk:
        for f in files:
            if find_file.search(f):
                path_name=path
                file_name=f
                WHB.write(path_name, file_name)


if __name__ == "__main__":
    for get_path in find_path:
        main(get_path)

开始测试脚本
# time python hbase_test.py

real 1m15.471s
user 0m4.881s
sys 0m2.867s

到hbase里查看写入的数量,证明已经完全写入。
hbase(main):001:0> count ‘img’
:
:
:
13140 row(s) in 10.2780 seconds

2013-5-16. 因为对hadoop理解不足。以下写的有问题,提醒大家注意。

hbase使用hadoop进行存储,查看hadoop的磁盘使用量。
26K namenode1/
298M u01/

  我的内存给namenode可以使用25G。根据以上数据计算结果如下:
((25*1000*1000)/26)*298= 286538461M = 286538G = 286 T

  如果每台服务器有三块1T存储硬盘,此集群可以有95台服务器。共存储此类照片大约为12634615360张。内网测试,写入速度3.9M。

  注:有一点需要注意,写入的数据删除后磁盘空间也不会释放,原理应该改和mongodb一样,但是没有仔细查看。

7月 13

mysql 索引文件错误修复

服务器出现宕机,重启后查看数据库日志报错如下:
120712 9:25:50 [ERROR] /program/mysql/libexec/mysqld: Table ‘./week_lib’ is marked as crashed and should be repaired

在mysql的bin目录中找到myisamchk可执行文件进行修复。
使用的命令如下:
/program/mysql/bin/myisamchk -c -r /program/mysql/data/test/week_lib.

如果还不行,就-f 强制修复

如果有phpmyadmin就方便多了。直接在右则勾选中错误信息中的表,选择下拉菜单(With selected:)中”Repair table”进行修复。
造成此类错误的主要原因是,突然断电、宕机、磁盘损坏导致数据文件出错。

12月 16

python 在linux上处理图片的错误处理

在CentOS 5.4 64位上安装了python2.6和PIL模块处理图片。
遇到问题1:
Traceback (most recent call last):
 File “aimg.py”, line 3, in
  img.save(‘/program/upload/b.jpg’,’JPEG’)
 File “/usr/local/lib/python2.6/site-packages/PIL-1.1.7-py2.6-linux-

x86_64.egg/Image.py”, line 1406, in save
 self.load()
 File “/usr/local/lib/python2.6/site-packages/PIL-1.1.7-py2.6-linux-

x86_64.egg/ImageFile.py”, line 189, in load
  d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
 File “/usr/local/lib/python2.6/site-packages/PIL-1.1.7-py2.6-linux-

x86_64.egg/Image.py”, line 385, in _getdecoder
  raise IOError(“decoder %s not available” % decoder_name)
IOError: decoder jpeg not available

处理方法:
# rm -rf /usr/local/lib/python2.6/site-packages/PIL-1.1.7-py2.6-linux-x86_64.egg
# rm -rf /usr/local/lib/python2.6/site-packages/PIL.pth
# yum install -y libjpeg* freetype* zlib*
# easy_install PIL

遇到问题2:
WARNING: ” not a valid package name; please use only.-separated package names in setup.py
_imaging.c:75:20: error: Python.h: No such file or directory
In file included from libImaging/Imaging.h:14,
from _imaging.c:77:
libImaging/ImPlatform.h:14:2: error: #error Sorry, this library requires support for ANSI prototypes.
libImaging/ImPlatform.h:17:2: error: #error Sorry, this library requires ANSI header files.
libImaging/ImPlatform.h:55:2: error: #error Cannot find required 32-bit integer type
In file included from _imaging.c:77

处理方法:
yum install python-imaging
yum install python-dev
python PIL

再次测试问题解决。

9月 27

linux下用python生成二维码


【张子萌 2011-9-27】
库的官方网站

http://fukuchi.org/works/qrencode/index.en.html

需要使用CythonCython是用来生成 C 扩展到而不是独立的程序的。所有的加速都是针对一个已经存在的 Python

用的一个函数进行的。还需要安装PIL

先下载了qrencode-3.1.1.tar.gz,使用configuremakemake install进行安装,手动生成了一个图,却是可用。

开始制作。

安装Cypthon

# easy_install cython

在官方下载bitly-pyqrencode-1cfb23c.tar.gz

# tar zxvf bitly-pyqrencode-1cfb23c.tar.gz

# cd bitly-pyqrencode-1cfb23c

使用Cpython安装qrencode

# cython qrencode.pyx

阅读README确认系统中有以下文件

you need libqrencode somewhere in your LD path (/usr/local/lib)

you need qrencode.h somewhere on your include path (/usr/local/include)

# python setup.py install

安装完毕测试以下。

脚本如下

from qrencode import Encoder

cre = Encoder()

img = cre.encode(“博客地址:http://simon-zzm.blog.163.com/ n电子邮件:simon-zzm@163.com”, { ‘width’: 300 })

img.save(‘out.png’)

生成图片如下:

linux下用python生成二维码 - simon-zzm - simon个人观点