Create Optical illusion in Python | Create Optical illusion with Python | How to Create Optical illusions in Python
In this tutorial I will show you how easy it is to create optical illusion Images using Python. I am going to use Python Turtle and saving it as PNG. The whole process will be automated. If you don't know how to Export Python Turtle Design in to PNG images Check out this tutorial.
This Scripts will create Transparent PNG images to which you can add any kind of background color using photoshop.
More Python Resources
- Run Ghost Script with Python
- Create Python EXE (Full Course)
- Python Pyramid (With Examples)
- Python Web Scraping with Json
- Python Web Scraping + HACKING
- How to add Python Code to your Blog or webpage?
Here are some Turtle generated Images along with the code.
Code for Bulge Illusion - #python4fun
import turtle screen = turtle.Screen() screen.setup(800,800) screen.setworldcoordinates(-8,-8,8,8) screen.tracer(0,0) screen.title('Bulge Illusion - #python4fun') turtle.hideturtle() turtle.speed(0) color1 = 'light sky blue' color2 = 'firebrick' screen.bgcolor(color2) def draw_square(x,y,size,c): turtle.up() turtle.goto(x-size/2,y-size/2) turtle.seth(0) turtle.color(c) turtle.begin_fill() for _ in range(4): turtle.fd(size) turtle.left(90) turtle.end_fill() def draw_board(): for x in range(-7,8,2): for y in range(-7,8,2): draw_square(x,y,1,color1) for x in range(-6,8,2): for y in range(-6,8,2): draw_square(x,y,1,color1) def draw_diag(x,y): c = color2 if (x+y)%2 == 0 else color1 if x*y > 0: draw_square(x-0.3,y+0.3,0.3,c) draw_square(x+0.3,y-0.3,0.3,c) elif x*y < 0: draw_square(x+0.3,y+0.3,0.3,c) draw_square(x-0.3,y-0.3,0.3,c) def draw_straight(x,y): c = color2 if (x+y)%2 == 0 else color1 if y>0: draw_square(x-0.3,y-0.3,0.3,c) draw_square(x+0.3,y-0.3,0.3,c) elif y<0: draw_square(x-0.3,y+0.3,0.3,c) draw_square(x+0.3,y+0.3,0.3,c) elif x>0: draw_square(x-0.3,y-0.3,0.3,c) draw_square(x-0.3,y+0.3,0.3,c) elif x<0: draw_square(x+0.3,y-0.3,0.3,c) draw_square(x+0.3,y+0.3,0.3,c) def draw_bulge(): for x in range(-6,7): for y in range(-6,7): if abs(x)+abs(y)<=7: draw_diag(x,y) if x==0 or y==0: draw_straight(x,y) x,y = -5,-3 for i in range(3): draw_diag(x,y) draw_diag(-x,-y) draw_diag(x,-y) draw_diag(-x,y) x += 1 y -= 1 draw_board() draw_bulge() screen.update() ### This will create .eps file #### import turtle from tkinter import * from turtle import * from PIL import Image import os import time # keeping eps file name as image eps_file = "temp.eps" # generating eps file ts = turtle.getscreen() turtle.hideturtle() ts.getcanvas().postscript(file=eps_file) # get current python file name as string without ".py" fname = os.path.basename(__file__) filename = fname.replace('.py','.png') # Converting eps file to jpg | with filename #im.save(str(filename)+".jpg", "JPEG") # Convert eps to transparent png using ghost script os.popen("gswin64c -dSAFER -dBATCH -dNOPAUSE -r300 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile="+filename+" -dEPSCrop temp.eps") # Merge transparent png on top of given background png time.sleep(2) # Get transparent image file height / width img = Image.open(filename) h, w = img.size print(h,w) print("Click on the Turtle Screen To Close") ts.exitonclick()
Code for Penrose Triangle - #python4fun
import turtle turtle.speed(1) turtle.setup(1000,1000) turtle.title('Penrose Triangle - #python4fun') def draw_penrose_triangle(x,y,size,tilt): turtle.pencolor((0.2,0.2,0.2)) turtle.up() turtle.goto(x,y) turtle.seth(tilt) turtle.down() turtle.fd(size*2) p1x = turtle.xcor() p1y = turtle.ycor() p1a = turtle.heading() turtle.left(120) turtle.fd(size*2) p2x = turtle.xcor() p2y = turtle.ycor() p2a = turtle.heading() turtle.left(120) turtle.fd(size*2) p3x = turtle.xcor() p3y = turtle.ycor() p3a = turtle.heading() # gray turtle.up() turtle.goto(p1x,p1y) turtle.seth(p1a) turtle.down() turtle.fillcolor('dark gray') turtle.begin_fill() turtle.fd(size) turtle.left(120) turtle.fd(size*5) turtle.left(120) turtle.fd(size*6) turtle.left(60) turtle.fd(size) turtle.left(120) turtle.fd(size*5) turtle.right(120) turtle.fd(3*size) turtle.end_fill() #black turtle.up() turtle.goto(p2x,p2y) turtle.seth(p2a) turtle.down() turtle.fillcolor('black') turtle.begin_fill() turtle.fd(size) turtle.left(120) turtle.fd(5*size) turtle.left(120) turtle.fd(6*size) turtle.left(60) turtle.fd(size) turtle.left(120) turtle.fd(5*size) turtle.right(120) turtle.fd(3*size) turtle.end_fill() #white turtle.up() turtle.goto(p3x,p3y) turtle.seth(p3a) turtle.down() turtle.fd(size) turtle.left(120) turtle.fd(5*size) turtle.left(120) turtle.fd(6*size) turtle.left(60) turtle.fd(size) turtle.left(120) turtle.fd(5*size) turtle.right(120) turtle.fd(3*size) draw_penrose_triangle(-80,-50,80,0) ### This will create .eps file #### import turtle from tkinter import * from turtle import * from PIL import Image import os import time # keeping eps file name as image eps_file = "temp.eps" # generating eps file ts = turtle.getscreen() turtle.hideturtle() ts.getcanvas().postscript(file=eps_file) # get current python file name as string without ".py" fname = os.path.basename(__file__) filename = fname.replace('.py','.png') # Converting eps file to jpg | with filename #im.save(str(filename)+".jpg", "JPEG") # Convert eps to transparent png using ghost script os.popen("gswin64c -dSAFER -dBATCH -dNOPAUSE -r300 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile="+filename+" -dEPSCrop temp.eps") # Merge transparent png on top of given background png time.sleep(2) # Get transparent image file height / width img = Image.open(filename) h, w = img.size print(h,w) print("Click on the Turtle Screen To Close") ts.exitonclick()
Code for Bulge Illusion - #python4fun
import turtle import math screen = turtle.Screen() screen.setup(1100,1000) screen.setworldcoordinates(-620,-550,680,550) screen.tracer(0,0) screen.title('Roller Illusion - #python4fun') turtle.hideturtle() turtle.speed(0) n=100 # parametric equation to draw ellipses def ellipse(cx,cy,a,b,c1,c2,c3): t = -math.pi/2 x = cx+a*math.cos(t) y = cy+b*math.sin(t) turtle.up() turtle.goto(x,y) turtle.down() turtle.pencolor(c1) turtle.fillcolor(c3) turtle.begin_fill() for i in range(n//2): x = cx+a*math.cos(t) y = cy+b*math.sin(t) turtle.goto(x,y) t += 2*math.pi/n turtle.pencolor(c2) for i in range(n//2): x = cx+a*math.cos(t) y = cy+b*math.sin(t) turtle.goto(x,y) t += 2*math.pi/n turtle.end_fill() def rolling_column(x,size): for y in range(-400,500,100): ellipse(x,y,size,35,'white','black','dark orange') def rolling_column2(x,size): for y in range(-400,500,100): ellipse(x,y,size,35,'black','white','dark orange') def rolling(): rolling_column(-450,10) rolling_column(-410,13) rolling_column(-360,16) rolling_column(-300,19) rolling_column(-240,16) rolling_column(-190,13) rolling_column(-150,10) rolling_column2(-130,10) rolling_column2(-90,13) rolling_column2(-40,16) rolling_column2(20,19) rolling_column2(80,16) rolling_column2(130,13) rolling_column2(170,10) rolling_column(190,10) rolling_column(230,13) rolling_column(280,16) rolling_column(340,19) rolling_column(400,16) rolling_column(450,13) rolling_column(490,10) turtle.color('steel blue') turtle.up() turtle.goto(-1000,-1000) turtle.down() turtle.begin_fill() turtle.seth(0) for _ in range(4): turtle.fd(2000) turtle.left(90) turtle.end_fill() turtle.pensize(3) rolling() screen.update() ### This will create .eps file #### import turtle from tkinter import * from turtle import * from PIL import Image import os import time # keeping eps file name as image eps_file = "temp.eps" # generating eps file ts = turtle.getscreen() turtle.hideturtle() ts.getcanvas().postscript(file=eps_file) # get current python file name as string without ".py" fname = os.path.basename(__file__) filename = fname.replace('.py','.png') # Converting eps file to jpg | with filename #im.save(str(filename)+".jpg", "JPEG") # Convert eps to transparent png using ghost script os.popen("gswin64c -dSAFER -dBATCH -dNOPAUSE -r300 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile="+filename+" -dEPSCrop temp.eps") # Merge transparent png on top of given background png time.sleep(2) # Get transparent image file height / width img = Image.open(filename) h, w = img.size print(h,w) print("Click on the Turtle Screen To Close") ts.exitonclick()
Code for Hermann Grid Illusion - #python4fun
import turtle screen = turtle.Screen() screen.setup(500,500) screen.title('Hermann Grid Illusion - #python4fun') screen.tracer(0) turtle.hideturtle() turtle.speed(0) turtle.bgcolor('black') turtle.pensize(4) turtle.color('light gray') for x in range(-502,500,25): turtle.up() turtle.goto(x,-500) turtle.down() turtle.seth(90) turtle.fd(1000) for y in range(-502,500,25): turtle.up() turtle.goto(-500,y) turtle.down() turtle.seth(0) turtle.fd(1000) turtle.up() for x in range(-502,500,25): for y in range(-502,500,25): turtle.goto(x,y) turtle.dot('white') screen.update() ### This will create .eps file #### import turtle from tkinter import * from turtle import * from PIL import Image import os import time # keeping eps file name as image eps_file = "temp.eps" # generating eps file ts = turtle.getscreen() turtle.hideturtle() ts.getcanvas().postscript(file=eps_file) # get current python file name as string without ".py" fname = os.path.basename(__file__) filename = fname.replace('.py','.png') # Converting eps file to jpg | with filename #im.save(str(filename)+".jpg", "JPEG") # Convert eps to transparent png using ghost script os.popen("gswin64c -dSAFER -dBATCH -dNOPAUSE -r300 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile="+filename+" -dEPSCrop temp.eps") # Merge transparent png on top of given background png time.sleep(2) # Get transparent image file height / width img = Image.open(filename) h, w = img.size print(h,w) print("Click on the Turtle Screen To Close") ts.exitonclick()
Comments
Post a Comment