我正在写一个python spirograph程序,我需要一些帮助将它的一部分转换成一个函数.代码试图重现我在这里找到的视频中说明的结果.一条线围绕原点旋转,然后另一条线从其末端旋转,等等.
通过对(我认为是)三角学的一点研究,我把一个函数放在一起rotate(point, angle, center=(0, 0)).用户输入要旋转的点,旋转的角度(顺时针),以及旋转的中心点.
然后,我实施了初始测试,其中一条线围绕另一条线旋转.第二行的结尾绘制就像握笔一样.代码有点乱,但看起来像这样.
x, y = 0, 0
lines = []
while 1:
point1 = rotate((0,50), x)
point2 = map(sum,zip(rotate((0, 50), y), point1))
if x == 0:
oldpoint2 = point2
else:
canvas.create_line(oldpoint2[0], oldpoint2[1], point2[0], point2[1])
lines.append( canvas.create_line(0, 0, point1[0], point1[1]) )
lines.append( canvas.create_line(point1[0], point1[1], point2[0], point2[1]) )
oldpoint2 = point2
tk.update()
x += 5
if x > 360 and y > 360:
x -= 360
canvas.delete("all")
time.sleep(1)
y += 8.8
if y …Run Code Online (Sandbox Code Playgroud) 我有以下代码用于图像阈值处理,使用Bradley-Roth图像阈值处理方法.
from PIL import Image
import copy
import time
def bradley_threshold(image, threshold=75, windowsize=5):
ws = windowsize
image2 = copy.copy(image).convert('L')
w, h = image.size
l = image.convert('L').load()
l2 = image2.load()
threshold /= 100.0
for y in xrange(h):
for x in xrange(w):
#find neighboring pixels
neighbors =[(x+x2,y+y2) for x2 in xrange(-ws,ws) for y2 in xrange(-ws, ws) if x+x2>0 and x+x2<w and y+y2>0 and y+y2<h]
#mean of all neighboring pixels
mean = sum([l[a,b] for a,b in neighbors])/len(neighbors)
if l[x, y] < threshold*mean:
l2[x,y] …Run Code Online (Sandbox Code Playgroud) python numpy image-processing python-imaging-library python-2.7
我正在尝试获得一种缩进样式,如下所示
H1
Content here
H2
Any content after the H2, paragraphs, images, etc
H3
Any content after the H2, paragraphs, images, etc
H2
content
H1 another top level heading
etc
Run Code Online (Sandbox Code Playgroud)
示例 html:
<h1>heading 1</h1>
<p>content</p>
<h2>heading 2</h2>
<p>content</p>
<p>content</p>
<h3>heading 2</h3>
<p>content</p>
<img src="something.png" />
<p>content</p>
<h1>heading 1</h1>
<p>content</p>
Run Code Online (Sandbox Code Playgroud)
我试过以下
h2, h2 + * {
margin-left: 30px;
}
h3, h3 + * {
margin-left: 60px;
}
Run Code Online (Sandbox Code Playgroud)
但这只会在标题后的第一个元素上设置边距,我需要所有后续标签,直到下一个。
有任何问题请追问。
我想指出的是,我无法重写 hml,因为我将它应用到已经创建了许多页面的网站。
我可以在端点查询字符串中包含多个“where”子句(或 AND 运算符)吗?我想做这样的事情:
http://localhost:5000/regions?where=name=="El Salvador"&where=lang=="es"
Run Code Online (Sandbox Code Playgroud)
我尝试了几种不同的语法,但我无法让它工作。
这可能吗?我知道我可以使用 MongoDB 语法来做到这一点,但我想使用 Python 语法。
注意:我不是在尝试使用 python 连接参数列表,而是在尝试使用原生 python 语法来使用Eve 的过滤功能。
我希望我的模块中的一个函数能够访问和更改导入它的脚本的本地命名空间.这将启用这样的功能:
>>> import foo
>>> foo.set_a_to_three()
>>> a
3
>>>
Run Code Online (Sandbox Code Playgroud)
这在Python中可行吗?
我正在使用Next.js,并且希望在链接到的页面与 url 匹配时将活动类添加到导航链接。但我也希望当 url 比页面更深时它处于活动状态。
例如,导航链接对于以下/register内容将是“活动的”:
/register/register/sign-up我需要使用或从源代码安装Pillow 。easy_install由于我在学校使用的计算机的限制,我无法访问 python.org 或 pip。
我从 effbot.org 解压该文件,并将其中的 PIL 文件夹放入 site-packages 中。它抱怨_imaging未安装 C 模块。如何使用 easy_install 或从源代码安装 Pillow?
给定索引列表(任意长度)的“路径”和嵌套的 Python list/dict对象,如何写入列表中的对象部分?
例如,这可能是我的对象(它是从 JSON 文件加载的):
data = {"dogs":[{"tail": True, "properties":{"test":1}}]}
Run Code Online (Sandbox Code Playgroud)
我的索引列表可能看起来像["dogs", 0, "properties"].
如果我想检索路径中的值,我可以执行以下操作:
>>> data = {"dogs":[{"tail": True, "properties":{"test":1}}]}
>>> path = ["dogs", 0, "properties"]
>>> mydata = data
>>> for i in path:
... mydata = mydata[i]
...
>>> mydata
{'test': 1}
Run Code Online (Sandbox Code Playgroud)
但是说我想在整个结构内的路径上修改对象?上面的代码是按值传递,而不是按引用传递,所以我认为我不能重用它。我能做什么?
我有一个由脚本生成的多行字符串,该脚本从图像创建 ASCII 艺术。它创建一条线,然后添加\r并继续。如何在\r不使用正则表达式的情况下获得第一行或之前的长度?最好代码具有相当的可读性。
我正在尝试使用Python中的skimage将RGB图像转换为灰度.这是我做的:
for im_path in glob.glob(os.path.join(pos_raw, "*")):
im = imread(im_path)
im = color.rgb2gray(im)
image_name = os.path.split(im_path)[1].split(".")[0] + ".pgm"
image_path = os.path.join(pos_img_path, image_name)
imwrite(image_path, im)
Run Code Online (Sandbox Code Playgroud)
对于一堆图像文件.我的输入图像如下所示:
输出图像如下所示:
预期的输出是这样的:
这可能是什么问题?
如果我不知道密钥存在,检查字典中某个键/值对是否存在的最短方法是什么?
到目前为止,我想出了:
if 'key' in my_dict and my_dict['key'] == 'value':
do_something()
Run Code Online (Sandbox Code Playgroud)
对于更长的变量名称或更长的键/值名称,这实际上很长,例如:
if 'X-Powered-By' in self.request.headers and self.request.headers['X-Powered-By'] == 'NodeBB':
do_something()
Run Code Online (Sandbox Code Playgroud)
检查密钥和相应值的存在的简短方法是什么?
我正在尝试将缩放设置为画布,以便适应高于原始画布高度的对象。这是一把小提琴。如果您注意到,如果您取消注释这些行,我希望画布按原样缩放:
//canvas.zoomToPoint(new fabric.Point(10, 10), 0.75);
//canvas.renderAll();
Run Code Online (Sandbox Code Playgroud)
我想0.75根据画布上的对象以编程方式定义正确的缩放(在本例中为 )。
我希望列表按照排序(列表)方法排序,但需要按字符串排序.我的意思是,不是按'abcdefghijklmnopqrstuvwxyz'排序,而是能够控制它.比如,按照'zyabxwcdvueftsghrqijpoklmn'的顺序排序,但是根据您输入的任何26个字母的字符串进行排序.我该怎么做?
sorted(list)方法将列表按字母顺序排序.如果我想要反向字母顺序,那也是相当简单的.但是说我想要一个完全自定义的字母优先权?默认值按顺序对列表进行排序'abcdefghijklmnopqrstuvwxyz'.如果我可以输入字符串,如果我可以'zyabxwcdvueftsghrqijpoklmn'根据此优先级对列表进行排序,该怎么办?所以,给定['moose', 'apple', 'zebra', 'penguin']和字符串'zyabxwcdvueftsghrqijpoklmn',我会得到['zebra', 'apple', 'penguin', 'moose'],因为字母出现在字符串中的顺序.
python ×10
python-2.7 ×3
dictionary ×2
javascript ×2
canvas ×1
css ×1
eve ×1
fabricjs ×1
frontend ×1
function ×1
image ×1
list ×1
namespaces ×1
nested ×1
next.js ×1
numpy ×1
reactjs ×1
restriction ×1
rotation ×1
scikit-image ×1
sorting ×1
string ×1
tkinter ×1