我正在使用Python Imaging Library使用定义颜色关系的查找表为黑白图像着色.查找表只是一个256元素的RGB元组列表:
>>> len(colors)
256
>>> colors[0]
(255, 237, 237)
>>> colors[127]
(50, 196, 33)
>>>
Run Code Online (Sandbox Code Playgroud)
我的第一个版本使用了getpixel()
和putpixel()
方法:
for x in range(w):
for y in range(h):
pix = img.getpixel((x,y))
img.putpixel((x,y), colors[pix[0]])
Run Code Online (Sandbox Code Playgroud)
这非常缓慢.一份profile
报告指出这些putpixel
和getpixel
方法是罪魁祸首.稍微调查(即阅读文档),我发现" 请注意,这种方法相对较慢. "re : putpixel
. (实际运行时间:对于1024x1024图像为53s in putpixel
和getpixel
50ss)
根据文档中的建议,我使用im.load()
并直接使用像素访问:
pixels = img.load()
for x in range(w):
for y in range(h):
pix = pixels[x, y]
pixels[x, y] = colors[pix[0]]
Run Code Online (Sandbox Code Playgroud)
处理加速了一个数量级,但仍然 …
python image-manipulation image image-processing python-imaging-library
根据The Django Book,Django的模板系统支持嵌套点查找:
点查找可以嵌套多个级别.例如,以下示例使用{{person.name.upper}},它转换为字典查找(person ['name']),然后是方法调用(upper()):'{{person.name.upper是{{person.age}}岁.
文件中没有广泛涵盖这种方法的地精吗?我遇到嵌套点查找问题 - 这是一个最小的例子:
views.py:
test = [{'foo': [1, 2, 3], 'bar': [4, 5, 6]}, {'baz': [7, 8, 9]}]
ndx = 'bar'
t = loader.get_template('meh.html')
c = Context({'test': test,
'ndx': ndx,})
return HttpResponse(t.render(c))
Run Code Online (Sandbox Code Playgroud)
meh.html模板:
<pre>
{{ test }}
{{ test.0 }}
{{ test.0.ndx }}
</pre>
Run Code Online (Sandbox Code Playgroud)
产生的HTML:
<pre>
[{'foo': [1, 2, 3], 'bar': [4, 5, 6]}, {'baz': [7, 8, 9]}]
{'foo': [1, 2, 3], 'bar': [4, 5, 6]}
</pre>
Run Code Online (Sandbox Code Playgroud)
当我期望[4,5,6]时,列表元素中的字典键的嵌套查找不返回任何内容.
JJ