龙空技术网

Pixelhouse:一个精美的、极简主义的python绘图库

易三一世 1853

前言:

目前看官们对“python画图函数库”大约比较关切,兄弟们都需要了解一些“python画图函数库”的相关知识。那么小编也在网摘上收集了一些有关“python画图函数库””的相关知识,希望看官们能喜欢,小伙伴们快快来了解一下吧!

1 说明:

=====

1.1 Pixelhouse:一个极简主义的绘图库,用于在python中制作精美的动画。

1.2 可以移动任何可以绘制的东西。配有漂亮的渐变,类似instagram的滤镜和弹性变换。

1.3 官网英文:

A minimalist drawing library for making beautiful animations in python.

Anything that can be drawn can be moved.

Comes with beautiful gradients, instagram-like filters, and elastic transforms.

2 准备:

=====

2.1 环境:

华为笔记本电脑、深度deepin-linux操作系统、python3.8和微软vscode编辑器。

2.2 传送门:

2.3 安装:

pip install pixelhouse#本机安装#sudo pip3.8 install pixelhouse#推荐安装#sudo pip3.8 install -i  pixelhouse

===以下代码为注释讲解版===

3 Hello world:

==========

3.1 代码:

import pixelhouse as ph#C = ph.Canvas() #采用默认#默认#Canvas(width=200, height=200, extent=4.0, bg='black', img=None, name='pixelhouseImage', shift=8)#指定参数值C = ph.Canvas(800, 800,bg='pink', name='win one')  #中文乱码#在C中加入文本文字#C+=ph.text('Hello World')  #默认设置,不可以显示中文#自定义:字体(需自行下载中文字体:hwfs=华文仿宋,放在根目录下),字体大小设置C+=ph.text('Hello World;你好世界',color='green',font='hwfs.ttf',font_size=0.5)  #保存#C.save("/home/xgj/Desktop/Pixelhouse/Helloworld.png")#显示C.show()

3.2 图:

4 palette(调色板):

===============

4.1 注释版调色板和颜色列表代码:

#pixelhouse=像素屋import pixelhouse as phpal=ph.palette(5)#print(pal[:])#[[232, 221, 203, 255], [205, 179, 128, 255], [3, 101, 100, 255], [3, 54, 73, 255], [3, 22, 52, 255]]#pal[2],得到:[3, 101, 100, 255] #草绿色'''C=ph.Canvas(1000,1000,extent=8.0,bg=pal[3],)C.show()

4.2 注意:复习和总结

#当≥5时,列表仍是5组,但是列表内的颜色值发生改变

#打印每个像素点的颜色RGBA的值(r,g,b,alpha)

#我们常见的RGB图像通常只有R、G、B三个通道

#A通道=alpha通道,是指给图像添加透明度

rgba表示颜色方式为rgba(a,b,c,d)

其中abc用来表示颜色,d用来表示透明度

(r, g, b, a)有4个值,每个都是0到255,RGBA的a(d值)就代表透明度。

5 palette_blocks=调色板块:

====================

5.1 代码:

#palette_blocks=调色板块'''#方法一import pixelhouse as ph#默认ph.palette_blocks(range(20)).show()''''''#方法二,与上面效果相同import pixelhouse as phph_win=ph.palette_blocks(range(20))ph_win.show()'''import pixelhouse as ph#自定义columns为4ph_win=ph.palette_blocks(range(20),columns=4)#默认#palette_numbers, columns=2, show_number=True, width=400ph_win.show()

5.2 图:

6 图片展示:

=========

import pixelhouse as phC = ph.Canvas(800, 800,bg='pink', name='win one')#C.load('/home/xgj/Desktop/Pixelhouse/20.jpeg')  #加载图片,jpeg#C.load('/home/xgj/Desktop/Pixelhouse/blue_woods.jpg')  #加载图片,jpgC.load('/home/xgj/Desktop/Pixelhouse/blue_woods.png')  #加载图片,png#C.load('/home/xgj/Desktop/Pixelhouse/yytd.gif')  #gif格式不行#保存#C.save("/home/xgj/Desktop/Pixelhouse/picx.png")#显示C.show()

===高级绘图===

7 代码:

import numpy as npimport pixelhouse as ph#定义函数def logo_animation(logo_text):    #作者提示取色法    #pal = ph.palette(3)    #我自己打印出来看看什么情况    #print(pal)    #[[85, 98, 112, 255], [78, 205, 196, 255], [199, 244, 100, 255], [255, 107, 107, 255], [196, 77, 88, 255]]    #我喜欢自定义取色法    pal=['red','blue','green','pink','orange']    A = ph.Animation(fps=24, duration=1.5, width=800, height=800, bg=pal[2])    #颜色渐变从red到blue    lg = ph.gradient.linear([pal[0], pal[1]], theta=-np.pi / 4)    #画圈    A += ph.circle(color=pal[3])    A += ph.filters.gaussian_blur()    A += ph.circle(color=pal[3])    y = ph.motion.easeInOutQuad(0, 1, flip=True)    for i in np.arange(-6, 6, 1.0):        #gradient=lg,文字有渐变颜色        A += ph.text(logo_text, y=i * y, gradient=lg, font_size=1.0)    return Aif __name__ == '__main__':    logo_text = "pixelhouse"    save_name = "/home/xgj/Desktop/Pixelhouse/logo_pixelhouse_animated5.gif"    ph.canvas2gif(logo_animation(logo_text), save_name, gifsicle=True)

图:

8 代码:

import pixelhouse as phfrom pixelhouse import Animation,circle,canvas2gifimport pixelhouse.motion as motiondef rotating_circles():    A = Animation(width=500, height=500, duration=1.5, fps=20, extent=4)    x = motion.easeInOutQuad(-1, 1, flip=True)    A(circle(x, 1, r=2.5, color=[0, 250, 150], mode="add"))    A(circle(-x, -1, r=2.5, color=[255, 5, 100], mode="add"))    return Aif __name__ == "__main__":    canvas2gif(rotating_circles(), "/home/xgj/Desktop/Pixelhouse/moving_circles6.gif",palettesize=32,gifsicle=True)

图:

9 pacman代码:

#*args:发送一个非键值对的可变数量的参数列表给函数。(顺带复习python的魔法语法)

import pixelhouse as phfrom pixelhouse import Animation,ellipse, canvas2gifimport pixelhouse.motion as motionimport numpy as np#保留下面的注释,主要是了解python的魔法语法#canvas_args = {"width": 500, "height": 500, "extent": 4}#animation_args = {"fps": 20, "duration": 1.5}#animation_args.update(canvas_args)#gif_args = {"palettesize": 32, "gifsicle": True}def pacman():    #args = animation_args.copy()    #args["duration"] = 0.5    #A = Animation(**args)    A = Animation(width=500, height=500, duration=0.5, fps=20, extent=4)    pac_color = (253, 255, 0)    dp = np.pi / 4    n = len(A.timepoints)    t0 = A.timepoints[: n // 2]    t1 = A.timepoints[n // 2 :]    x0 = motion.easeOutQuad(0, dp)(t0)    x1 = motion.easeInQuad(dp, 0)(t1)    z = np.hstack([x0, x1])    #椭圆形    A += ellipse(a=2, b=2, angle_start=z, angle_end=2 * np.pi - z, color=pac_color)    return Aif __name__ == "__main__":    #canvas2gif(pacman(), "/home/xgj/Desktop/Pixelhouse/pacman7.gif", **gif_args)    canvas2gif(pacman(), "/home/xgj/Desktop/Pixelhouse/pacman7.gif", palettesize=32,gifsicle=True)

图:

10 checkerboard代码:

import pixelhouse as phfrom pixelhouse import Canvas, Animation,circle, canvas2gifimport pixelhouse.motion as motion#itertools用于高效循环的迭代函数集合import itertoolsdef checkerboard():    A = Animation(width=500, height=500, duration=1.5, fps=20, extent=4)    z = motion.easeInOutQuad(0, 1, True)    r = 0.40    c = [150, 250, 0]    coord = [-2, 0, 2]    #定义参数    args = {"r": r, "color": c, "mode": "add"}    for dx, dy in itertools.product(coord, repeat=2):        #引用参数,画圈        A += circle(z + dx, z + dy, **args)        A += circle(z + dx, -z + dy, **args)        A += circle(-z + dx, -z + dy, **args)        A += circle(-z + dx, z + dy, **args)        A += circle(dx, z + dy, **args)        A += circle(z + dx, dy, **args)        A += circle(dx, -z + dy, **args)        A += circle(-z + dx, dy, **args)    return Aif __name__ == "__main__":    canvas2gif(checkerboard(), "/home/xgj/Desktop/Pixelhouse/checkerboard8.gif", palettesize=32,gifsicle=True)

图:

11 高级欢迎的代码:

import pixelhouse as phfrom pixelhouse import *from pixelhouse.filters import *from pixelhouse.motion import *from pixelhouse.transform import *import numpy as npdef main():    #pal = palette(15) #自带颜色列表    #我喜欢自定义颜色    pal=['black','green','blue','pink','orange']    C = Animation(500, 500, duration=1.5, fps=20, extent=4,bg=pal[0])    y = easeInOutQuad(-2, 2, flip=True)    def draw_circles(C):        C += circle(-2, y, r=0.40, color=pal[1])        C += circle(-1, -y, r=0.40, color=pal[2])        C += circle(1, y, r=0.40, color=pal[1])        C += circle(2, -y, r=0.40, color=pal[2])    draw_circles(C)    C += gaussian_blur(1.0, 1.0)    draw_circles(C)    with C.layer() as L:        theta = easeInOutQuad(0, 2 * np.pi)        L += rectangle(-0.5, -0.5, 0.5, 0.5, color=pal[3])        L += rotate(theta)    #导入本地根目录下的中文字体:hwfs    f_font = "hwfs.ttf"    #颜色渐变效果,从2~3(green→blue)    g = ph.gradient.linear([pal[2], pal[3]])    C += text("欢迎光临", y=3, font_size=1.0, font=f_font, gradient=g)    C += text("pixelhouse", y=-3, font_size=1.0, font=f_font, gradient=g)    return C  #没有ta,是不能保存为gif    #C.show() #没有ta,是不能显示gifif __name__ == "__main__":    canvas2gif(main(),"/home/xgj/Desktop/Pixelhouse/pic_gif_start9.gif",  palettesize=32,gifsicle=True,)

图:

===自己整理并分享出来===

喜欢的就点赞、评论、转发、关注和收藏。

标签: #python画图函数库