精华内容
下载资源
问答
  • 听说 python 还能画画?是的,今天亲手一朵玫瑰给你。turtle 是 python 中一个强大的绘制图像的库,可以用来绘制各种图像,使用起来很方便。但是需要计算各种角度、距离等,所以要想完成一幅复杂的图像还是有一定...

    1460000021762552

    听说 python 还能画画?是的,今天亲手画一朵玫瑰给你。

    turtle 是 python 中一个强大的绘制图像的库,可以用来绘制各种图像,使用起来很方便。

    但是需要计算各种角度、距离等,所以要想完成一幅复杂的图像还是有一定的难度的。

    今天我们来画一朵玫瑰试试,有兴趣的可以自己去调整下源码的参数,或者可以尝试画出你心中不一样的玫瑰花。

    由于代码量比较多,而且大多数都是类似的功能,所以这里就不贴完整的源码了,挑一些主要的函数说明一下即可。

    写这些文章主要目的还是为了说明 python 还是挺好玩的,希望能激发大家学习 python 的兴趣。

    对了,想要完整源码的同学可以在公众号后台回复关键字 【玫瑰花】 即可。

    turtle.speed(5) #画笔移动的速度

    turtle.penup()  #提起画笔,移动画笔但并不会绘制图形

    turtle.pendown()  #放下画笔,移动画笔即开始绘制

    turtle.left(90)  #逆时针转动画笔90度

    turtle.right(90)  #顺时针时针转动画笔90度

    turtle.fd(25) #向前移动指定距离 fd=forward

    turtle.bd(30)   #向后移动指定距离 bd=backward

    turtle.fillcolor("red")  #填充颜色

    turtle.begin_fill()  #开始填充

    turtle.end_fill()  #结束填充

    turtle.circle(200,50)  #画一个圆 200 是半径,50 是弧度

    #在程序的最后一行加一行#不然画画结束后会自动退出turtle.done()

    一朵玫瑰花其实主要就是上面这几个函数来实现的,不过是参数不同罢了,在实现的过程中也可以调整参数然后看下具体的效果。

    再提醒一个小问题,也是我自己在写这篇文章时遇到的问题,由于我把这个 python 脚本也命名为 turtle.py ,结果在运行程序的时候就报错找不到 turtle 对应模块之类的。

    后来才发现这样写的话,import turtle 这个语句其实是会导入脚本自身的,并没有导入 turtle 这个库,所以把脚本重命名一下就好了。

    觉得玫瑰花好看的话,顺便也给文章点个赞哇。

    1460000021762551

    展开全文
  • When creating an animation using matplotlib.animation and saving it, an error appears when trying to close the figure window via plt.close:Python version:Python 2.7.12 |Anaconda custom (64-bit)| (defa...

    When creating an animation using matplotlib.animation and saving it, an error appears when trying to close the figure window via plt.close:

    Python version:

    Python 2.7.12 |Anaconda custom (64-bit)| (default, Jul 2 2016, 17:42:40)

    IPython 4.1.2 -- An enhanced Interactive Python

    Currently I switched to using PyCharm 2017.1 Community Edition. The error message can be reproduced both directly in IPython and within PyCharm when running from %cpaste or %paste in IPython or running in PyCharm's interactive console using Shift+Alt+E. The movie encoder used is mencoder as integrated in mplayer, since this is the default one installed at my work place.

    Note:

    in IPython use plt.ion() first to turn on interactive mode (already switched on in PyCharm by default)

    code exits without error in IPython when pasted using the middle mouse button directly into the IPython screen

    code exits without error in IPython or PyCharm when typing all commands separately and also when pasting (%cpaste, %paste) all commands except for plt.close() and then typing plt.close() manually

    code exits without error when replacing plt.close() with plt.clf(), but I need plt.close(), e.g. for creating animations in a loop with different parameters where the graph needs to be recreated from scratch

    code exits without error when running the code in a script from start to end (non-interactive mode)

    code exits without error when closing the figure window clicking with the mouse on the window button

    code exits with error when inserting time.sleep(1) before plt.close(), so the problem likely does not relate to time conflicts in the code

    A minimal, complete and (hopefully) verifiable example is given below:

    import numpy as np

    import matplotlib.pyplot as plt

    import matplotlib.animation as animation

    # animation function for random image data

    def animate_random_data(i):

    new_data = np.random.rand(10, 10)

    # update the data

    im.set_data(new_data)

    # initialize the graph

    first_data = np.random.rand(10,10)

    im = plt.imshow(first_data,interpolation='none')

    myfig = plt.gcf()

    # create the animation and save it

    ani = animation.FuncAnimation(myfig, animate_random_data, range(10),

    interval=100)

    ani.save('animation_random_data.mpg', writer='mencoder')

    plt.close()

    Error traceback (from PyCharm):

    Traceback (most recent call last):

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/backends/backend_qt5agg.py", line 176, in __draw_idle_agg

    FigureCanvasAgg.draw(self)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 474, in draw

    self.figure.draw(self.renderer)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/artist.py", line 61, in draw_wrapper

    draw(artist, renderer, *args, **kwargs)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/figure.py", line 1165, in draw

    self.canvas.draw_event(renderer)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 1809, in draw_event

    self.callbacks.process(s, event)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/cbook.py", line 563, in process

    proxy(*args, **kwargs)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/cbook.py", line 430, in __call__

    return mtd(*args, **kwargs)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/animation.py", line 652, in _start

    self.event_source.add_callback(self._step)

    AttributeError: 'NoneType' object has no attribute 'add_callback'

    Although the program continues without error when closing the windows manually as written in the list above, it is an annoying bug (think of multiple animations in a loop). The error appears also for e.g. 1D line plots. Thanks for any help (and clarification on what this error message exactly means)!

    解决方案

    The error comes from the animation still running while closing the figure. While in most cases it is automatically taken care of to stop the animation when closing the figure, it seems not to be the case in interactive mode.

    A solution can be to explicitely stop the animation and delete it before closing the figure.

    ani = animation.FuncAnimation(...)

    ani.save(...)

    ani.event_source.stop()

    del ani

    plt.close()

    展开全文
  • When creating an animation using matplotlib.animation and saving it, an error appears when trying to close the figure window via plt.close:Python version:Python 2.7.12 |Anaconda custom (64-bit)| (defa...

    1586010002-jmsa.png

    When creating an animation using matplotlib.animation and saving it, an error appears when trying to close the figure window via plt.close:

    Python version:

    Python 2.7.12 |Anaconda custom (64-bit)| (default, Jul 2 2016, 17:42:40)

    IPython 4.1.2 -- An enhanced Interactive Python

    Currently I switched to using PyCharm 2017.1 Community Edition. The error message can be reproduced both directly in IPython and within PyCharm when running from %cpaste or %paste in IPython or running in PyCharm's interactive console using Shift+Alt+E. The movie encoder used is mencoder as integrated in mplayer, since this is the default one installed at my work place.

    Note:

    in IPython use plt.ion() first to turn on interactive mode (already switched on in PyCharm by default)

    code exits without error in IPython when pasted using the middle mouse button directly into the IPython screen

    code exits without error in IPython or PyCharm when typing all commands separately and also when pasting (%cpaste, %paste) all commands except for plt.close() and then typing plt.close() manually

    code exits without error when replacing plt.close() with plt.clf(), but I need plt.close(), e.g. for creating animations in a loop with different parameters where the graph needs to be recreated from scratch

    code exits without error when running the code in a script from start to end (non-interactive mode)

    code exits without error when closing the figure window clicking with the mouse on the window button

    code exits with error when inserting time.sleep(1) before plt.close(), so the problem likely does not relate to time conflicts in the code

    A minimal, complete and (hopefully) verifiable example is given below:

    import numpy as np

    import matplotlib.pyplot as plt

    import matplotlib.animation as animation

    # animation function for random image data

    def animate_random_data(i):

    new_data = np.random.rand(10, 10)

    # update the data

    im.set_data(new_data)

    # initialize the graph

    first_data = np.random.rand(10,10)

    im = plt.imshow(first_data,interpolation='none')

    myfig = plt.gcf()

    # create the animation and save it

    ani = animation.FuncAnimation(myfig, animate_random_data, range(10),

    interval=100)

    ani.save('animation_random_data.mpg', writer='mencoder')

    plt.close()

    Error traceback (from PyCharm):

    Traceback (most recent call last):

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/backends/backend_qt5agg.py", line 176, in __draw_idle_agg

    FigureCanvasAgg.draw(self)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 474, in draw

    self.figure.draw(self.renderer)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/artist.py", line 61, in draw_wrapper

    draw(artist, renderer, *args, **kwargs)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/figure.py", line 1165, in draw

    self.canvas.draw_event(renderer)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 1809, in draw_event

    self.callbacks.process(s, event)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/cbook.py", line 563, in process

    proxy(*args, **kwargs)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/cbook.py", line 430, in __call__

    return mtd(*args, **kwargs)

    File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/animation.py", line 652, in _start

    self.event_source.add_callback(self._step)

    AttributeError: 'NoneType' object has no attribute 'add_callback'

    Although the program continues without error when closing the windows manually as written in the list above, it is an annoying bug (think of multiple animations in a loop). The error appears also for e.g. 1D line plots. Thanks for any help (and clarification on what this error message exactly means)!

    解决方案

    The error comes from the animation still running while closing the figure. While in most cases it is automatically taken care of to stop the animation when closing the figure, it seems not to be the case in interactive mode.

    A solution can be to explicitely stop the animation and delete it before closing the figure.

    ani = animation.FuncAnimation(...)

    ani.save(...)

    ani.event_source.stop()

    del ani

    plt.close()

    展开全文
  • 又到一年一度的国际儿童节,今天我们来学习一下用 Python 的 Turtle 库绘制童年的卡通人物,一起做回年轻的那个少年。Turtle图形库,又称海龟库,是 Python 语言中一个很流行的绘制图像的函数库。Turtle 库一般 ...

    又到一年一度的国际儿童节,今天我们来学习一下用 Python 的 Turtle 库绘制童年的卡通人物,一起做回年轻的那个少年。

    c83d70cf3bc79f3da5dea721944f0d17738b29dc.png?token=82346389a1003b5f915262ccc660fd57

    Turtle图形库,又称海龟库,是 Python 语言中一个很流行的绘制图像的函数库。

    Turtle 库一般 Python 环境会自带,如果没有这个库查询一下安装方法,自行安装。

    常用的函数

    1、画笔控制函数

    penup:抬起画笔;pendown:落下画笔;pensize(width):画笔宽度;pencolor(color):画笔颜色;color为颜色字符串或者rgb值2、运动控制函数

    forward(d)/fd(d):直行d个像素;circle(r, extent = None):绘制半径为r,角度为extent的弧形,圆心默认在海龟左侧距离r的位置;3、方向控制函数

    setheading(angle)/seth(angle):改变前进方向;left(angle):海龟左转;right(angle):海龟右转;

    代码演示

    下面用海龟库完成小黄人的绘制,下面是部分源码,完整源码的卡通图案绘制源码可以私信我。

    import turtle as t

    t.pensize(4)

    t.speed(10)

    # =======头======

    def head():

    t.penup()

    t.fillcolor("#FFEE26")

    t.goto(-130, 10)

    t.pendown()

    t.begin_fill()

    t.seth(81)

    t.fd(90)

    t.seth(100)

    t.circle(-500, 3)

    t.circle(-100, 10)

    t.circle(-200, 25)

    t.circle(-110, 20)

    t.circle(-140, 30)

    t.circle(-180, 30)

    t.circle(-200, 20)

    t.circle(-140, 10)

    t.circle(-160, 50)

    t.seth(85)

    t.fd(-148)

    t.seth(-112)

    t.circle(-250, 14)

    t.fd(200)

    t.right(80)

    t.fd(190)

    t.seth(110)

    t.circle(-200, 7)

    t.circle(-130, 30)

    t.end_fill()

    绘制效果图:

    562c11dfa9ec8a136abcda825ee85189a1ecc024.png?token=6b8269977327548fce39ceff6fa5b733

    编制by伟森leung 喜欢本文章的可以请关注伟森leung,后续有更多python方面的知识分享。

    标签: python 、Linux、vim编译器

    #python#

    展开全文
  • 本文目录:小猪佩奇-落叶树-魔法少女本文作者:小赖同学​最近在浏览Python文章时,无意中发现了一个好玩的画图工具turtle,看到各路大神竟然用Python画了一个图片出来,无法用言语来形容心中感概,只能重复默念...
  • Python绘制漫画人物

    2021-03-04 10:28:29
    fillcolor(’#00a0de’) begin_fill() circle(150, 280) end_fill() 哆啦A梦 def Doraemon(): # 头部 head() # 围脖 scarf() # 脸 face() # 红鼻子 nose() # 嘴巴 mouth() # 胡须 beard() # 身体 my_goto(0, 0) ...
  • 微信公众号:AI算法与图像处理如果你觉得对你有帮助,欢迎关注、转发以及点赞哦~( ̄▽ ̄~)~Turtle库 简介什么是Turtle首先,turtle库是一个点线面的简单图像库,能够完成一些比较简单的几何图像可视化。......
  • f = open("t.jpg", "wb") img = (response.json()["image"]) f.write(base64.b64decode(img)) f.close() 到此这篇关于python实现人像动漫化的示例代码的文章就介绍到这了,更多相关python 人像动漫化内容请搜索云海天...
  • )图如下:代码如下:1 import turtle as t2 import time...01868相关推荐2019-12-24 09:01 −用turtle函数空心圆 turtle.circle是从下方开始画圆的,所以要同心圆的话,每一次都要将画笔移动到下一个圆的底部...
  • 我默默的用python画了下面这张图。 依稀记得女同事看了之后,立刻转身离去,并留下了一句”哼,臭屌丝“,留下懵逼的我凌乱在风中。凌乱的我对这张图左看、右看、上看、下看,终于明白在不懂技术的妹子眼中,这种...
  • 广告关闭腾讯云11.11云上盛惠 ,精选热门... 2“不务正业”的python更可怕的是,某些实用主义者将python的灵活性发挥到了极致。 有的人想节省房租,用python爬出了全上海7万多套房源,只为了挑选出自己喜欢的房子。...
  • 最近在浏览Python文章时,无意中发现了一个好玩的画图工具turtle,看到各路大神竟然用Python画了一个图片出来,无法用言语来形容心中感概,只能重复默念“卧槽卧槽厉害”。为了丰富大家的娱乐时间,我进行了汇总,...
  • python画素描画

    2020-04-23 16:05:55
    废话不多说,直接上代码 from PIL import Image, ImageFilter, ImageOps img = Image.open('/Users/demo/Desktop/b.png') # 原图路径 def dodge(a,b,alpha): return min(int(a*255/(256-b*alpha)),255) ...
  • 效果展示 turtle库是python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上...
  • python画梵高星空-python画画梵高

    千次阅读 2020-11-01 13:20:31
    广告关闭腾讯云11.11云上盛惠 ,精选热门... 2“不务正业”的python更可怕的是,某些实用主义者将python的灵活性发挥到了极致。 有的人想节省房租,用python爬出了全上海7万多套房源,只为了挑选出自己喜欢的房子。...
  • 微信公众号:AI算法与图像处理如果你觉得对你有帮助,欢迎关注、转发以及点赞哦~( ̄▽ ̄~)~Turtle库 简介什么是Turtle首先,turtle库是一个点线面的简单图像库,能够完成一些比较简单的几何图像可视化。......
  • python 素描

    2020-05-12 09:26:27
    最近无聊,玩一玩python 我的环境是py3 依赖包PIL 关于PIL的安装问题可以看我的另一篇文字。 废话多了 直接上代码 from PIL import Image, ImageFilter, ImageOps img = Image.open(‘D:\xl\lp2.jpg’) def dodge(a,...
  • 使用python turtle库实现了动漫人物史迪仔的绘制,画图过程尽量细分将每一个拐角都反复调整角度,直到合适为止,大概耗费了五六个小时的时间绘制成功(Using the python turtle library to achieve the drawing of ...
  • 作者 | 张同学来源 | 凹凸数据大家好,我是张同学。又到一年一度的国际儿童节,作为逢节必过的程序猿,怎么可以放过这个学习技能的机会呢?于是,今天我们来学习 Python 的 Turtl...
  • 利用python画社交网络图使用的库是 networkx,更多关于networkx的介绍与使用大家可以参考这篇文章:https://www.jb51.net/article/159743.htm下面开始本文的正文:import networkx as nximport matpl...
  • 也收到了读者想用 python 各种图的各种需求。 和一些读者沟通后才知道是学校布置了相关的作业,或者是自己想用这个来做毕业设计。 关于这个问题我想说的是,大家需要的是静下来心来学习 turtle 这个库,熟悉基本的...
  • 主要给大家介绍了关于如何使用python画社交网络图的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
  • 如何让Python画笔转弯

    千次阅读 2020-04-20 13:05:04
    上几期教大家如何Python画笔如何直线并且改变画笔颜色: ''' 首先还是导入turtle库 ''' import turtle t = turtle.Turtle() t.color("red")#将画笔颜色设置为红色 t.fd(100)#画笔前进100(像素) 之后我们学习...
  • 目录tkinter库画图基本思路虚线定义原始人物的函数点击眨眼运动四肢色彩变化 我们知道Python之所以强大,很大一方面都是因为它具有很多强大的第三方库。 而且Python很突出的一方面就是代码用户交互界面的实现,...
  • python程序出一些简单的动漫人物

    万次阅读 多人点赞 2019-12-23 15:30:05
    小猪佩奇 # coding:utf-8 import turtle as t t.pensize(4) # 设置画笔的大小 t.colormode(255) # 设置GBK颜色范围为0-255 t.color((255,155,192),"pink") # 设置画笔颜色和填充颜色...t.speed(10) # 设置...
  • network模块是一个用python语言开发的图论和复杂网络建模工具,模块内置了常用的图与复杂网络分析算法。network模块有四种图:Graph、DiGraph、MultiGraph、MultiDigraph,分别为无多重边无向图、无多重边有向图、有...
  • 不怕,不怕,虽然现实中没有,但是我们可以画一只哟~素描什么的都弱爆了,我们来用Python画一只。经过一整天的苦战,从未接触过turtle库的文摘菌实现了从0到1,成就感满满。 先看下效果: 基本思路:选好画板大小,...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 6,103
精华内容 2,441
关键字:

python画人物

python 订阅