import keyboard
import numpy as np
import time
import cv2
import os
x1=120
y1=4
z1=190
x2=x1
y2=y1
z2=z1
filepath=r"E:\img\bili.jpg" #图片路径
a=cv2.imread(filepath,0) #imread(IRL,FLAGS)载入图片 0:灰度图像
#Flags指定了所读取图片的颜色类型
#CV_LOAD_IMAGE_ANYDEPTH返回图像的深度不变。
#CV_LOAD_IMAGE_COLOR总是返回一个彩色图。
#CV_LOAD_IMAGE_GRAYSCALE总是返回一个灰度图。
time.sleep(3)
abc=0 #用来累计方块的
bl=10 #缩放比例
a = cv2.resize(a,(int(a.shape[1]/bl),int(a.shape[0]/bl)), interpolation=cv2.INTER_AREA)
print(a.shape)
print('高'+str(a.shape[0])) #打印宽和高
print('宽'+str(a.shape[1]))
z2=z1
y1=y1+a.shape[0] #抬高y
#这里还可以加个判断y是否高出256
y2=y1
for row in range(a.shape[0]): #遍历每一行
for col in range(a.shape[1]): #遍历每一列
abc=abc+1 #计数累加
print(a[row][col])
#根据 a[row][col] 的值可以弄更多颜色的羊毛
if a[row][col]==255:
#萤石
keyboard.press('t')
time.sleep(0.1)
keyboard.write('/fill %s %s %s %s %s %s minecraft:glowstone'%(x1+col,y1-row,z1,x2+col,y2-row,z2))
keyboard.press_and_release('Enter')
elif a[row][col]<200:
#黑羊毛
keyboard.press('t')
time.sleep(0.1)
keyboard.write('/fill %s %s %s %s %s %s minecraft:black_wool'%(x1+col,y1-row,z1,x2+col,y2-row,z2))
keyboard.press_and_release('Enter')
else:
#石头
keyboard.press('t')
time.sleep(0.07)
keyboard.write('/fill %s %s %s %s %s %s minecraft:stone'%(x1+col,y1-row,z1,x2+col,y2-row,z2))
keyboard.press_and_release('Enter')
print(abc)
#显示图片:
cv2.namedWindow("Faces")
cv2.imshow("Faces", a)
cv2.waitKey(0)
来源:oschina
链接:https://my.oschina.net/u/4342750/blog/3228788