2020年度卒研セミナー(2020/06/18)

関連サイトと資料

描画プログラムを使ってみよう

mcpi-img-full.py
import math
from PIL import Image
from mcpi.minecraft import Minecraft
from mcpi.block import *
   
# 羊毛ブロックのRGBの値を格納した辞書
toRGB = {
      WOOL_BLACK: (26, 22, 22),
      WOOL_BLUE: (46, 57, 142),
      WOOL_BROWN: (79, 51, 31),
      WOOL_CYAN: (47, 111, 137),
      WOOL_GRAY: (64, 64, 64),
      WOOL_GREEN: (53, 71, 27),
      WOOL_LIGHT_BLUE: (107, 138, 201),
      WOOL_LIGHT_GRAY: (155, 161, 161),
      WOOL_LIME: (66, 174, 57),
      WOOL_MAGENTA: (180, 81, 189),
      WOOL_ORANGE: (219, 125, 63),
      WOOL_PINK: (208, 132, 153),
      WOOL_PURPLE: (127, 62, 182),
      WOOL_RED: (151, 52, 49),
      WOOL_WHITE: (222, 222, 222),
      WOOL_YELLOW: (177, 166, 39),
    }
  
#引数で与えられたRGBの近いRGB値を持った羊毛ブロックを抜き出す
def getNearRgbBlockId(mr,mg,mb):
    distant_dict = {} 
    for blockid in toRGB:
        r,g,b = toRGB[blockid]
        distance = math.sqrt((mr-r)*(mr-r)+(mg-g)*(mg-g)+(mb-b)*(mb-b))
        distant_dict[blockid] = distance
    
    srt_distance_dict = sorted(distant_dict.items(), key=lambda x:x[1])
    key,value = srt_distance_dict[0]
    
    return key
   
mc = Minecraft.create()
pos = mc.player.getTilePos()
   
img = Image.open("img.JPG") #画像の読み込み
img = img.resize( (100,100) ) #画像のサイズ変更
   
#RGBへの変換
if img.mode != "RGB":
    img = img.convert("RGB")
    
width, height = img.size #画像サイズの取得
  
#読み込んだ画像の描画
for wx in range(width):
    for hy in range(height):
        r, g, b = img.getpixel((wx, hy))
        mc.setBlock(pos.x+wx,pos.y,pos.z+hy,getNearRgbBlockId(r,g,b))
    

img.jpg

描画プログラムを作成する

mcpi-img.py
import math
from PIL import Image
from mcpi.minecraft import Minecraft
from mcpi.block import *
   
#羊毛ブロックのRGB情報を格納した辞書
toRGB = {
      WOOL_BLACK: (26, 22, 22),
      WOOL_BLUE: (46, 57, 142),
      WOOL_BROWN: (79, 51, 31),
      WOOL_CYAN: (47, 111, 137),
      WOOL_GRAY: (64, 64, 64),
      WOOL_GREEN: (53, 71, 27),
      WOOL_LIGHT_BLUE: (107, 138, 201),
      WOOL_LIGHT_GRAY: (155, 161, 161),
      WOOL_LIME: (66, 174, 57),
      WOOL_MAGENTA: (180, 81, 189),
      WOOL_ORANGE: (219, 125, 63),
      WOOL_PINK: (208, 132, 153),
      WOOL_PURPLE: (127, 62, 182),
      WOOL_RED: (151, 52, 49),
      WOOL_WHITE: (222, 222, 222),
      WOOL_YELLOW: (177, 166, 39),
    }
   
mc = Minecraft.create()
pos = mc.player.getTilePos()