turtle 画照片

2023-03-10,

# -*- coding: utf-8 -*-

import turtle as t
import cv2 def draw_img(img_path, scale=1):
"""
画图片里的内容
:param img_path: 图片路径
:param scale: 缩放比例,比如1,0.5,0.25
:return:
"""
t.getscreen().colormode(255)
img = cv2.imread(img_path)
img1 = img[::int(1/scale)] # 取图片一半(隔行取)的行数据
width = len(img1[0])/int(1/scale) # 取图片的宽度
height = len(img1) # 取图片的高度 t.setup(width=width+100, height=height+100) # 设置画布大小, 600, 370
t.pu()
t.goto(-width/2 + 10, height/2 - 10) # 移动到指定坐标的位置 -140,
t.pd()
t.tracer(3000) # 加快作图速度, 0 or False 时图形一次性画好 for k1, i in enumerate(img1):
for j in i[::int(1/scale)]:
t.pencolor((j[2], j[1], j[0])) # 设置画笔颜色
t.fd(1) # 沿着海龟的前方向运行1
t.pu() # 画笔抬起,不留下痕迹
t.goto(-width/2, height/2 -k1) # 移动画笔到下一行最左侧位置
t.pd() # 画笔落下,留下痕迹
t.done() if __name__ == '__main__':
path = r'./2.jpg'
draw_img(path, 0.5)

turtle 画照片的相关教程结束。

《turtle 画照片.doc》

下载本文的Word格式文档,以方便收藏与打印。