Skip to content

Instantly share code, notes, and snippets.

@hlorand
Created February 28, 2021 05:14
Show Gist options
  • Save hlorand/d2c3e918558b308fdb3a53488ead6c22 to your computer and use it in GitHub Desktop.
Save hlorand/d2c3e918558b308fdb3a53488ead6c22 to your computer and use it in GitHub Desktop.
Draw image pixel by pixel in Python (turtle module)
import turtle
def setwindowsize(x=640, y=640):
turtle.setup(x, y)
turtle.setworldcoordinates(0,0,x,y)
def drawpixel(x, y, color, pixelsize = 1 ):
turtle.tracer(0, 0)
turtle.colormode(255)
turtle.penup()
turtle.setpos(x*pixelsize,y*pixelsize)
turtle.color(color)
turtle.pendown()
turtle.begin_fill()
for i in range(4):
turtle.forward(pixelsize)
turtle.right(90)
turtle.end_fill()
def showimage():
turtle.hideturtle()
turtle.update()
################
# Example 1
setwindowsize(200, 200)
drawpixel(100, 100, (255,0,0) )
showimage()
################
# Example 2
from random import *
setwindowsize(300,300)
for x in range(30):
for y in range(30):
color = (randint(0,255),randint(0,255),randint(0,255))
drawpixel(x,y,color,10)
showimage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment