-
Notifications
You must be signed in to change notification settings - Fork 0
/
minecraft_image.py
60 lines (54 loc) · 1.7 KB
/
minecraft_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Script By Ferran Fabregas ([email protected])
import picamera
import Image, sys, math
sys.path.append("./mcpi/api/python/mcpi")
from mcpi.minecraft import Minecraft
# COLOR MAPPING
def colormap(pixel):
white=(221,221,221)
orange=(219,125,62)
magenta=(179,80,188)
lightblue=(107,138,201)
yellow=(177,166,39)
lime=(65,174,56)
pink=(208,132,153)
gray=(64,64,64)
lightgray=(154,161,161)
cyan=(46,110,137)
purple=(126,61,181)
blue=(46,56,141)
brown=(79,50,31)
green=(53,70,27)
red=(150,52,48)
black=(25,22,22)
colors=(white,orange,magenta,lightblue,yellow,lime,pink,gray,lightgray,cyan,purple,blue,brown,green,red,black)
thecolor=0
finalresult=256*256*256
for idx,color in enumerate(colors):
result=math.fabs(color[0]-pixel[0])+math.fabs(color[1]-pixel[1])+math.fabs(color[2]-pixel[2])
if result<finalresult:
finalresult=result
thecolor=idx
return thecolor
# FETCH FILE FROM THE CAMERA
savefile="/home/pi/tmp_image.jpg"
size= 200,200
camera=picamera.PiCamera()
camera.start_preview()
i=raw_input() # PRESS ANY KEY TO FETCH THE PICTURE
camera.capture(savefile)
camera.stop_preview()
im=Image.open(savefile)
im.thumbnail(size,Image.ANTIALIAS)
# LOAD PIXELS OF IMAGE
pixels=im.load()
print im.size
# INIT MINECRAFT WORLD
mc=Minecraft.create()
mc.postToChat("Welcome to Minecraft Image Render")
for x in range (-(im.size[0]/2),(im.size[0]/2)):
for y in range (-(im.size[1]/2),(im.size[1]/2)):
mc.setBlock(x,29,y,35,colormap(pixels[x+(im.size[0]/2),y+(im.size[1]/2)]))
print "Print position:(%i,%i)"%(x+(im.size[0]/2),y+(im.size[1]/2))
mc.player.setTilePos(0,30,0)
print "RENDER FINISHED!!"