赞
踩
- from PIL import Image
- import numpy as np
- import turtle as te
-
-
- picture_path = "C:/..." # 照片文件路径
-
- picture = Image.open(picture_path) # 读取照片
- data = np.array(picture) # 转为数组
-
- te.setup(1.0, 1.0) # 画布大小
- te.colormode(255) # 颜色模式【RGB】模式
- te.speed(0) # 画笔速度
- te.delay(0) # 画布刷新延时
-
- # 画笔坐标
- x = 0
- y = 0
-
- for i in data:
- for j in i:
- te.penup() # 抬起画笔
- te.setpos(x, -y) # 设置画笔位置
- te.pendown() # 放下画笔
- te.dot(2, *j) # 画点,第一个参数为点的大小,后面为颜色【RGB】
- x += 1
- y += 1
- x = 0
- te.done()

大一的时候老师布置的turtle大作业,为了偷懒,突发奇想,直接把照片按像素的在turtle画布上用点表示,近似还原照片,效果还不错,但画图速度贼慢!
另外,如果照片没有显示在屏幕中央,可以按照“上加下减,左加右减”的规则,像te.setpos(x-300, -y+300)这样调。图片过大则等比例索小图片, 像这样改te.setpos(x/2-300, -y/2+300)或者用其他修图软件直接将图片缩小(减少像素点)也行。如果卡死,则删掉te.delay(0)。
效果展示:
原照片:
turtle画图结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。