“Python” How to Make Japanese Lottery Game Called AMIDA

Python
higashi
higashi

Hi, I’m higashi.

 

This time, I introduce how to make “AMIDA” movie by using python.

“AMIDA” is often use to choose someone else in japan.

Below is a movie of “AMIDA”.

In “AMIDA”, the marker trace the vertical line basically, and should slide if horizontal line is exist as shown above.

 

By making horizontal line randomly, the election result is also changed randomly.

Please use it at the workplace lottery and so on.

Of course you can change the number of vertical line and horizontal line easily.

 

So, let’s get started!!

 

Sponsored Links

Sample Program of Making AMIDA

Below program is the sample to make movie that is introduced at the beginning of this page.

#import library
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import numpy as np

#inoput information
list=['AA','BB','CC','DD','EE']
bar_horizontal=12
#setting of font size and color
font_size=50
font_color=255
#setting line width
line_w=5
#setting image size
h=750
w=1000
#you should choose font in your computer
font_path="C:\Windows\Fonts\HGRPP1.TTC"
font = ImageFont.truetype(font_path, font_size) 
bar_vertical=len(list)
width_h=int((h-400)/bar_horizontal)
width_w=int(w/bar_vertical)

#making base image
img=Image.new("L",(w+width_w,h+width_h))
#convert pillow to numpy
base_img=np.array(img)

#discribe lines for amida
for i in range(bar_vertical):
    base_img[width_h+100:-width_h-100,(i+1)*width_w-line_w:(i+1)*width_w+line_w]=150
for i in range(bar_horizontal):
    pos=np.random.randint(1,bar_vertical)
    base_img[width_h+200+int((i+1)*width_h-line_w):width_h+200+int((i+1)*width_h+line_w),pos*width_w:(pos+1)*width_w]=150

#convert numpy to pillow
img=Image.fromarray(base_img)
#discribe candidates from list
for i in range(bar_vertical):
    ImageDraw.Draw(img).text(((i+1)*width_w-12*len(list[i]), 50), list[i], font = font , fill = font_color)
election=np.random.randint(0,bar_vertical)
ImageDraw.Draw(img).text(((election+1)*width_w-25, h-100), '★', font = font , fill = font_color)
base_img=np.array(img)

pictures=[]
#function of moving markaer
def show_pic(base_img,hpos,wpos,n):
    img=Image.fromarray(base_img)
    ImageDraw.Draw(img).text((wpos-25, hpos-25), '★', font = font , fill = font_color)
    pictures.append(img.convert('P'))

#moving tracer
wpos=int((election+1)*width_w)
spos=30
n=0
for i in range(h-width_h-100+25,width_h+100,-line_w*2):
    hpos=i
    #trace above
    if base_img[i,wpos-spos]<100 and base_img[i,wpos+spos]<100:
        show_pic(base_img,hpos,wpos,n) 
        n+=1 
    #trace left
    elif base_img[i,wpos-spos]>100:
        j=0
        while base_img[i,wpos-j]>100:
            j+=1
            if j%(line_w*2)==0:
                show_pic(base_img,hpos,wpos-j,n)
                n+=1
        i+=10
        wpos=wpos-j+line_w
    #trace right
    else:
        j=0
        while base_img[i,wpos+j]>100:
            j+=1
            if j%(line_w*2)==0:
                show_pic(base_img,hpos,wpos+j,n)
                n+=1
        wpos=wpos+j-line_w
#making gif animation
pictures[0].save('anime.gif',save_all=True, append_images=pictures[1:],
optimize=False, duration=20, loop=0)

 

You can change the candidates by 8 line.

 

higashi
higashi

Anyway, let’s conduct the program.

 

Sponsored Links

Result of Sample Program

At first, I conduct above program as it is.

 

Below GIF movie is produced.

I think it is no problem.

 

Next, I conduct below setting.

list=[‘AA’,’BB’,’CC’,’DD’,’EE’,’FF’,’GG’,’HH’,’II’] : list of candidates

bar_horizontal=40: number of horizontal lines

line_w=3 : line width of amida

h=1000 : horizontal image size of amida

The result is below.

higashi
higashi

Too complex. But no problem.

In my office, we select a winner of baseball game ticket that given from boss by using this method at lunch break.

 

That’s all. Thank you!!

コメント