|
1 2 3 4 5 6 7 8 9 |
from PIL import Image im = Image.open("xxx.jpeg") pix = im.load() width = im.size[0] height = im.size[1] for x in range(width): for y in range(height): r, g, b = pix[x, y] print(r, g, b) |
分类: Python
Python(KK 英语发音:/ˈpaɪθən/), 是一种面向对象、直译式计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够很轻松的把用其他语言制作的各种模块(尤其是C/C++)轻松地联结在一起。
Python 2.7在调用自定义类函数时候报错"exceptions.TypeError: 'int' object is not callable"
Python 2.7中按照如下方式定义类
|
1 2 3 4 5 6 7 8 9 10 11 |
class Rectangle: def __init__(self,l): self.left = l def left(self): return self.left if __name__ == '__main__': rect = Rectangle(1) print rect.left() |
在执行脚本的时候会报告如下错误
|
1 2 3 4 |
Traceback (most recent call last): File "test.py", line 11, in <module> print rect.left() TypeError: 'int' object is not callable |
造成错误的原因在于Python 2.7中当类的成员变量跟方法名重名的时候,默认是使用类的成员变量,而成员变量是无法被调用的,因而导致报错。
解决方法就是避免方法名跟成员变量同名即可,比如上面的定义修改成如下方式即可
|
1 2 3 4 5 6 7 8 9 10 11 |
class Rectangle: def __init__(self,l): self.l = l def left(self): return self.l if __name__ == '__main__': rect = Rectangle(1) print rect.left() |
Python 2.7使用multiprocessing报错PicklingError: Can't pickle : attribute lookup __builtin__.instancemethod failed
Python 2.7中使用下面代码的时候
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import multiprocessing class examplePool: def __init__(self): self.pool = multiprocessing.Pool() def run(self,args): print args def runProcessPool(self): self.pool.apply(self.run,(60,)) self.pool.close() self.pool.join() if __name__ == '__main__': pool = examplePool() pool.runProcessPool() |
会报告如下错误:
|
1 2 3 4 5 6 7 8 9 10 |
Traceback (most recent call last): File "text.py", line 18, in <module> pool.runProcessPool() File "text.py", line 12, in runProcessPool self.pool.apply(self.run,(60,)) File "/usr/lib/python2.7/multiprocessing/pool.py", line 244, in apply return self.apply_async(func, args, kwds).get() File "/usr/lib/python2.7/multiprocessing/pool.py", line 567, in get raise self._value cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed |
这种错误发生的原因是Python 2.7版本中的multiprocessing模块的进程池部分存在BUG,无法正常处理这种写法的代码,据说在Python 3.4版本之后解决了此问题。
目前测试来看,不用Python 2.7中的进程池,而是自己创建进程,自己管理进程的方式,可以比较简单的解决此类问题。
比如代码修改成如下样子,即可正常运行
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import multiprocessing class exampleProcess: def __init__(self): self.process = multiprocessing.Process(target=self.run,args=(60,)) def run(self,args): print args def runProcess(self): self.process.start() self.process.join() if __name__ == '__main__': process = exampleProcess() process.runProcess() |
至于多进程的管理,则只需要使用
|
1 |
from multiprocessing import Process, Queue |
引入multiprocessing模块的Queue,实现一个简单的生产者,消费者模型即可。
参考链接
WARNING:libav.swscaler:Warning: data is not aligned! This can lead to a speedloss
在Python中使用libav视频解码的时候,如果需要更改最后输出的视频的宽高,比如如下代码:
|
1 2 3 4 5 6 7 8 9 |
width = 240 container = av.open(url) video_stream = next(s for s in container.streams if s.type == 'video') for packet in container.demux(video_stream): for frame in packet.decode(): if frame is None: break frame = frame.reformat(width,height) img = frame.to_image() |
可能会收到一条警告信息
|
1 |
WARNING:libav.swscaler:Warning: data is not aligned! This can lead to a speedloss |
导致警告的原因是swscaler的缩放的目标尺寸不合适,它预期的大小是16的倍数,这个倍数可以保证swscaler以最高效的方式进行图片的缩放处理。
解决警告的方式就是保证宽高都是16的倍数即可。
参考链接
[swscaler] Warning: data is not aligned! This can lead to a speedloss 的解决方法【FFmpeg】
Ubuntu 16.04下Python打印正在运行的进程的栈信息
Python程序在运行时候,可能由于某些原因导致进程卡住在某行代码上,此时我们需要输出进程中各个线程的栈信息。
此时我们需要使用Python栈工具pstack的协助,项目的工程地址https://github.com/wooparadog/pstack/
具体的用法如下:
|
1 2 |
$ sudo pip install pstack $ sudo pstack pid |
cProfile——Python性能分析工具
Python自带了几个性能分析的模块:profile,cProfile和hotshot,使用方法基本都差不多,无非模块是纯Python还是用C写的。本文介绍cProfile。
继续阅读cProfile——Python性能分析工具
python直接下载图片到内存
1. 使用requests(推荐)
|
1 2 3 4 |
from PIL import Image from StringIO import StringIO import requests Image.open(StringIO(requests.get(url, stream=True).raw.read())) |
2. 使用StringIO
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from PIL import Image from StringIO import StringIO import requests r = requests.get("http://a/b/c") im = Image.open(StringIO(r.content)) im.size # ======================= from PIL import Image import urllib2 as urllib from StringIO import StringIO fd = urllib.urlopen("http://a/b/c") im = Image.open(StringIO(fd.read())) im.size |
3. 使用io.BytesIO
|
1 2 3 4 5 6 7 |
from PIL import Image import urllib2 as urllib import io fd = urllib.urlopen("http://a/b/c") image_file = io.BytesIO(fd.read()) im = Image.open(image_file) |
参考链接
获取当前Python中site-packages的具体存放路径
很多时候,我们系统上安装了好几个版本的Python, 此时,我们往往没办法确定通过pip安装的包会存放到那个目录下的site-packages中,可以通过如下代码获取:
|
1 |
$ python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" |
在Ubuntu 16.04 LTS系统上,这个输出是存在问题的,执行命令后输出的目录是:
|
1 |
/usr/lib/python2.7/dist-packages |
实际上,通过pip命令安装的目录有很大一部分被安装到了
|
1 |
/usr/local/lib/python2.7/dist-packages |
目录下。
Ubuntu 16.04 LTS上使用Python3版本的PIP
Ubuntu 16.04 LTS上使用Python2与Python3是共存的,而且默认使用Python2,如果使用Python3则需要明确指定。
1.安装Python3版本的PIP
|
1 2 3 |
$ sudo apt-get install python3-pip $ sudo pip3 install --upgrade pip |
2.安装Python3版本的NumPy
|
1 |
$ pip3 install numpy |
3.安装Python3版本的OpenCV
|
1 |
$ sudo pip3 install opencv-python |
注意,目前的Python3版本的OpenCV是不支持cv2.imshow()的,具体查看https://pypi.python.org/pypi/opencv-python,可以看到如下信息:
|
1 2 3 |
**Q: Why I can't open GUI windows (``cv2.imshow()``) on GNU/Linux distribution X or on macOS?** A: Like above, OpenCV was not compiled against GTK or Carbon. Support for these might be added in the future. |
Python 2.7 基于twisted实现简单的web服务器
Python 2.7自带的SimpleHTTPServer默认是HTTP/1.0,导致在投放简单的视频的时候,一般是无法通过FFMPEG进行播放的,主要是HTTP/1.0不支持Content-Range导致无法快进以及视频的Seek操作。并且比较悲剧的是,如该修改成HTTP/1.1协议,默认只有一个连接在处理,导致只要第一个用户不断开,第二个连接基本上连接不上。
网上搜索了以下,找到了用twisted代码来实现比较简单,并且性能还不错的服务器。
本文实例讲述了Python基于twisted实现简单的web服务器,分享给大家供大家参考。具体方法如下:
1.首先是通过PIP安装twisted
|
1 |
$ sudo pip install twisted |
2. 新建htm文件夹,在这个文件夹中放入显示的网页文件
3. 在htm文件夹的同级目录下,建立web.py,代码如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
from twisted.web.resource import Resource from twisted.web import server from twisted.web import static from twisted.internet import reactor PORT = 1234 ######################################################################## class ReStructed(Resource): """""" #---------------------------------------------------------------------- def __init__(self, filename, *a): """Constructor""" self.rst = open(filename).read() def render(self, request): return self.rst resource = static.File('htm/') resource.processors = {'.html':ReStructed} resource.indexNames = ['index.html'] reactor.listenTCP(PORT, server.Site(resource)) reactor.run() |
执行脚本
|
1 |
$ python web.py |
然后浏览器访问:http://127.0.0.1:1234/就可以看到内容了。