Created
February 28, 2021 05:14
-
-
Save hlorand/d2c3e918558b308fdb3a53488ead6c22 to your computer and use it in GitHub Desktop.
Draw image pixel by pixel in Python (turtle module)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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