我有(N,3)一系列numpy值:
>>> vals = numpy.array([[1,2,3],[4,5,6],[7,8,7],[0,4,5],[2,2,1],[0,0,0],[5,4,3]])
>>> vals
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 7],
[0, 4, 5],
[2, 2, 1],
[0, 0, 0],
[5, 4, 3]])
Run Code Online (Sandbox Code Playgroud)
我想从数组中删除具有重复值的行.例如,上面数组的结果应该是:
>>> duplicates_removed
array([[1, 2, 3],
[4, 5, 6],
[0, 4, 5],
[5, 4, 3]])
Run Code Online (Sandbox Code Playgroud)
我不知道如何在没有循环的情况下有效地使用numpy(数组可能非常大).谁知道我怎么能这样做?
我有一个大型字典,其中包含一些大型数据:
d = {'something': {'else': 'x'}, 'longnumbers': [1,2,3,4,54,6,67,7,7,8,8,8,6,4,3,3,5,6,7,4,3,5,6,54]}
Run Code Online (Sandbox Code Playgroud)
真正的字典有更多的键和嵌套的结构.当我json.dump没有使用时indent,我得到一个不可读的紧凑的单行输出.当我设置时indent,它会在每个分隔符(包括数组)之后添加换行符.
数值数组很长,最终结果如下:
"longnumbers": [
1,
2,
3,
4,
54,
6,
67,
7,
7,
8,
8,
8,
6,
4,
3,
3,
5,
6,
7,
4,
3,
5,
6,
54
],
Run Code Online (Sandbox Code Playgroud)
有没有办法得到具有缩进级别的漂亮打印的JSON,但没有在数组元素后面添加换行符?对于上面的例子,我想要这样的事情:
{
"longnumbers": [1, 2, 3, 4, 54, 6, 67, 7, 7, 8, 8, 8, 6, 4, 3, 3, 5, 6, 7, 4, 3, 5, 6, 54],
"something": {
"else": "x"
}
}
Run Code Online (Sandbox Code Playgroud) 我希望我的程序有一个参数,它有一些必需的参数和一些可选参数.像这样的东西:
[--print text [color [size]]
Run Code Online (Sandbox Code Playgroud)
所以你可以传递任何这些:
mycommand --print hello
mycommand --print hello blue
mycommand --print hello red 12
Run Code Online (Sandbox Code Playgroud)
可能有多个这样的,所以它必须是一个add_argument.例如:
[--print text [color]] [--output filename [overwrite]]
Run Code Online (Sandbox Code Playgroud)
我可以获得接近我想要的论点:
>>> parser = argparse.ArgumentParser()
>>> act = parser.add_argument('--foo', nargs=3, metavar=('x','y','z'))
>>> act = parser.add_argument('--bar', nargs='?')
>>> act = parser.add_argument('--baz', nargs='*')
>>> parser.print_help()
usage: [-h] [--foo x y z] [--bar [BAR]] [--baz [BAZ [BAZ ...]]]
optional arguments:
-h, --help show this help message and exit
--foo x y z
--bar [BAR]
--baz [BAZ [BAZ ...]] …Run Code Online (Sandbox Code Playgroud) 我用PIL创建一个图像:

我需要填补空白区域(描绘为黑色).我可以很容易地用静态颜色填充它,但我想做的是用附近的颜色填充像素.例如,边界之后的第一个像素可能是填充像素的高斯模糊.或者可能是Lumigraph,Gortler等人所述的推挽式算法..
我需要一些不太慢的东西,因为我必须在许多图像上运行它.我可以访问其他库,比如numpy,你可以假设我知道边界或外部区域或区域内的掩码.有关如何处理此问题的任何建议?
更新:
正如belisarius所建议的那样,opencv的inpaint方法是完美的.这里有一些使用opencv来实现我想要的python代码:
import Image, ImageDraw, cv
im = Image.open("u7XVL.png")
pix = im.load()
#create a mask of the background colors
# this is slow, but easy for example purposes
mask = Image.new('L', im.size)
maskdraw = ImageDraw.Draw(mask)
for x in range(im.size[0]):
for y in range(im.size[1]):
if pix[(x,y)] == (0,0,0):
maskdraw.point((x,y), 255)
#convert image and mask to opencv format
cv_im = cv.CreateImageHeader(im.size, cv.IPL_DEPTH_8U, 3)
cv.SetData(cv_im, im.tostring())
cv_mask = cv.CreateImageHeader(mask.size, cv.IPL_DEPTH_8U, 1)
cv.SetData(cv_mask, mask.tostring())
#do …Run Code Online (Sandbox Code Playgroud) python文档生成器Sphinx的输出产生大量HTML文件.每个人都有一个包含大量JavaScript和CSS的标题包括:
<link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="stylesheet" type="text/css" href="../_static/custom.css" />
<link rel="stylesheet" type="text/css" href="../_static/colorbox/colorbox.css" />
<script type="text/javascript" src="../_static/colorbox/jquery.colorbox-min.js"></script>
Run Code Online (Sandbox Code Playgroud)
其中大部分是单独缩小的,但这仍然不是最理想的,因为当客户端的缓存为空时,它需要单独的请求到Web服务器.是否有像YUI Compressor或Closure Compiler这样的工具将HTML文件作为输入,压缩所有单独的外部链接脚本,然后重写输出?这与django_compressor的作用类似.
假设我有一些32位数字和一些64位数字:
>>> import numpy as np
>>> w = np.float32(2.4)
>>> x = np.float32(4.555555555555555)
>>> y = np.float64(2.4)
>>> z = np.float64(4.555555555555555)
Run Code Online (Sandbox Code Playgroud)
我可以打印出来%f但它有额外的,不需要的小数:
>>> '%f %f %f %f' % (w, x, y, z)
'2.400000 4.555555 2.400000 4.555556'
Run Code Online (Sandbox Code Playgroud)
我可以使用,%g但似乎有一个小的默认精度:
>>> '%g %g %g %g' % (w, x, y, z)
'2.4 4.55556 2.4 4.55556'
Run Code Online (Sandbox Code Playgroud)
我在想我应该使用类似于.732位值和.1564位值的东西:
>>> '%.7g %.7g %.15g %.15g' % (w, x, y, z)
'2.4 4.555555 2.4 4.55555555555556'
Run Code Online (Sandbox Code Playgroud)
这似乎工作得相当好,但精度数也用于小数位前面的数字,例如34567.375768.
总之,将浮点值序列化为文本的正确方法是什么,以便为32位和64位值保留适当的精度,但不使用任何不必要的空间?
更新:
我 …
这按预期工作:
>>> class Foo(object):
... @classmethod
... def hello(cls):
... print 'hello, foo'
...
>>> class Bar(Foo):
... @classmethod
... def hello(cls):
... print 'hello, bar'
... super(Bar, cls).hello()
...
>>> b = Bar()
>>> b.hello()
hello, bar
hello, foo
Run Code Online (Sandbox Code Playgroud)
我也可以显式调用基类:
>>> class Bar(Foo):
... @classmethod
... def hello(cls):
... print 'hello, bar'
... Foo.hello()
...
>>> b = Bar()
>>> b.hello()
hello, bar
hello, foo
Run Code Online (Sandbox Code Playgroud)
我想知道为什么我不能省略第一个参数super,像这样:
>>> class Bar(Foo):
... @classmethod
... def hello(cls):
... print 'hello, …Run Code Online (Sandbox Code Playgroud) 我有一个numpy数组的向量,我需要乘以一组标量.例如:
>>> import numpy
>>> x = numpy.array([0.1, 0.2])
>>> y = numpy.array([[1.1,2.2,3.3],[4.4,5.5,6.6]])
Run Code Online (Sandbox Code Playgroud)
我可以将这样的单个元素相乘:
>>> x[0]*y[0]
array([ 0.11, 0.22, 0.33])
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将整个数组彼此相乘时,我得到:
>>> x*y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape
Run Code Online (Sandbox Code Playgroud)
我认为这与广播规则有关.将这两个数组元素与numpy相乘的最快方法是什么?
我有两个PIL图像和两组相应的2D点,它们构成一个三角形.
例如:
image1:
100x100 pixels
points = [(10,10), (20,20), (10,20)]
image2:
250x250 pixels
points = [(35,30), (75,19), (50,90)]
Run Code Online (Sandbox Code Playgroud)
我想从image1复制三角形区域并将其转换为适合image2的相应三角形区域.有没有办法用PIL做这个而不必逐个像素地复制并自己计算转换?
python image-processing affinetransform python-imaging-library
我一直在研究Javascript 3d编程.因此,我已经了解了THREE.js框架,并且已经取得了一些成功.之后,我将我创建的Google Sketchup文件(.skp)导出到Collada(.dae),并在线发现了一些讨论ColladaLoader.js的教程.
这些例子说明如下:
<script src="three.js"></script>
<script src="ColladaLoader.js"></script>
Run Code Online (Sandbox Code Playgroud)
这是否意味着有一个ColladaLoader.js文件可供下载?就像有一个three.js文件和一个jquery.js文件?
我已经执行了每次谷歌搜索,但没有找到这个问题或适用的源代码文件的答案.我真的很感激答案.谢谢
python ×8
numpy ×4
argparse ×1
class-method ×1
collada ×1
css ×1
html ×1
inheritance ×1
javascript ×1
json ×1
minify ×1
performance ×1
precision ×1
python-2.x ×1
python-3.x ×1
three.js ×1