Tag: python

几个关于 python package (objc , PIL )的 bug

最近在处理一个图片需求的时候,竟然遇到数个几年了都没人修正的 bug。。。这个过程值得记录一下。。。

原始需求是这样的:

首先,是用一个 ImageAlpha 的 app ,这个 app 其实做得不错的,但是因为基于 GUI ,如果我要处理 1000 张图片,那会累死人的

但是。。。这个 app 竟然还不能直接使用,需要安装 pyobjc ,至少在我的 MacOSX 上需要安装。

1)第一个 bug ,pip 安装 pyobjc

pip install pyobjc

报错:

 
Downloading/unpacking PyObjC
 
Running setup.py egg_info for package PyObjC
 
Downloading/unpacking pyobjc-core==2.3 (from PyObjC)
 
Running setup.py egg_info for package pyobjc-core
 
/usr/local/Cellar/python/2.7.2/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'use_2to3'
 
warnings.warn(msg)
 
Traceback (most recent call last):
 
File "", line 14, in
 
File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/trashcli/build/pyobjc-core/setup.py", line 452, in
 
**extra_args
 
File "/usr/local/Cellar/python/2.7.2/lib/python2.7/distutils/core.py", line 152, in setup
 
dist.run_commands()
 
File "/usr/local/Cellar/python/2.7.2/lib/python2.7/distutils/dist.py", line 953, in run_commands
 
self.run_command(cmd)
 
File "/usr/local/Cellar/python/2.7.2/lib/python2.7/distutils/dist.py", line 972, in run_command
 
cmd_obj.run()
 
File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/trashcli/build/pyobjc-core/setup.py", line 110, in run
 
orig_egg_info.egg_info.run(self)
 
File "", line 7, in replacement_run
 
AttributeError: class egg_info has no attribute 'iter_entry_points'
 
Complete output from command python setup.py egg_info:
 
/usr/local/Cellar/python/2.7.2/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'use_2to3'
 
warnings.warn(msg)
 
running egg_info
 
Traceback (most recent call last):
 
File "", line 14, in
 
File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/trashcli/build/pyobjc-core/setup.py", line 452, in
 
**extra_args
 
File "/usr/local/Cellar/python/2.7.2/lib/python2.7/distutils/core.py", line 152, in setup
 
dist.run_commands()
 
File "/usr/local/Cellar/python/2.7.2/lib/python2.7/distutils/dist.py", line 953, in run_commands
 
self.run_command(cmd)
 
File "/usr/local/Cellar/python/2.7.2/lib/python2.7/distutils/dist.py", line 972, in run_command
 
cmd_obj.run()
 
File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/trashcli/build/pyobjc-core/setup.py", line 110, in run
 
orig_egg_info.egg_info.run(self)
 
File "", line 7, in replacement_run
 
AttributeError: class egg_info has no attribute 'iter_entry_points'

参考 [1] , [2] 才能完整解决

1、pip 安装失败后,会在当前目录有 build 目录,修改 setup.py 文件,build/pyobjc-core/setup.py

use_2to3 = True   =>    use_2to3 = False

2、chmod 执行权限

chmod a+x build/pyobjc-core/libxml2-src/configure
chmod a+x build/pyobjc-core/libxml2-src/install-sh

3、export MACOSX_DEPLOYMENT_TARGET=10.5 (我也不知道为什么要声明这个系统变量)

4、python build/pyobjc-core/setup.py install ,就可以把 pyobjc-core 安装上了,然后再 pip install pyobjc ,会安装其它的一些包

 

但我不喜欢用 GUI ,我相信用几行 python 就能解决,于是我用 PIL ,并参考 [3] ,但文章中的函数并不能正常运行,原文如下:

from PIL import Image
 
im = Image.open('mouse.png')
# Get the alpha band
alpha = im.split()[3]
# Convert the image into P mode but only use 255 colors in the palette out of 256
im = im.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255)
# Set all pixel values below 128 to 255,
# and the rest to 0
mask = Image.eval(alpha, lambda a: 255 if a <=128 else 0)
# Paste the color of index 255 and use alpha as a mask
im.paste(255, mask)
# The transparency index is 255
im.save('mouse.gif', transparency=255)

会报错:

python pil ‘NoneType’ object has no attribute ‘bands’

2)以上就是第二个 bug … 两个解决方法

1、参考 [4] ,在调用 im.split() 前,先 im.getdata() 一下,如下

from PIL import Image
 
im = Image.open('mouse.png')
# Get the alpha band
 
im.getdata()
 
alpha = im.split()[3]
# Convert the image into P mode but only use 255 colors in the palette out of 256
im = im.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255)
# Set all pixel values below 128 to 255,
# and the rest to 0
mask = Image.eval(alpha, lambda a: 255 if a <=128 else 0)
# Paste the color of index 255 and use alpha as a mask
im.paste(255, mask)
# The transparency index is 255
im.save('mouse.gif', transparency=255)

2、fix 了这个 bug 的 patch ,在这里 [5] http://hg.effbot.org/pil-2009-raclette/changeset/fb7ce579f5f9

 

为了处理 1k 张图片。。。真折腾。。。还以为我需要把 ImageAlpha 的函数抽出来了,它是用 python 调用 objective-c 的接口来做的。

 

Reference:

[1] https://github.com/pypa/pip/issues/11#issuecomment-6485553

[2] http://www.slevenbits.com/2012/05/installing-pyobjc-in-virtualenv-on-mac-os-x-10-7

[3] http://nadiana.com/pil-tips-converting-png-gif

[4] http://bugs.python.org/issue9383

[5] http://hg.effbot.org/pil-2009-raclette/changeset/fb7ce579f5f9

 

 

 

DrawSth

这几天都在玩这个 app,苦于词汇量真的太少。。。真的看出来对方画的是什么,也不懂拼。

随便写了个小程序,原理很简单,将所有的字母全排列一次,然后 spell check 。全排列原来已经有 lib 实现了,spell check 也有 pyenchant ,真是没有技术含量啊。。。

日后考虑加入 Google Translate ,不知道会不会因为太频繁访问被 Google ban 掉。

https://github.com/hellosa/draw-something-helper

当然,一般不用这东西,要不就破坏了游戏性了。

—————

update 2012-03-20:

今天想加入 google translate 的功能。。。发现貌似收费了。。。