Skip to content

Instantly share code, notes, and snippets.

@ice09
Last active February 5, 2024 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ice09/026b74da4973d05d3cb08431c929f8d9 to your computer and use it in GitHub Desktop.
Save ice09/026b74da4973d05d3cb08431c929f8d9 to your computer and use it in GitHub Desktop.
Python script for MCPI to create a 2 dimensional 8 color blocks based on a Python array.
import sys
import mcpi.block as block
from mcpi.minecraft import Minecraft
from math import *
# Data structure representing icon as a 2D grid
# Copy the generated array from the BlockCraft chat
image = [[7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 2, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 6, 0, 0, 2, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 0, 0, 2, 0, 1, 6, 7, 7, 7, 7, 7, 7, 7, 7],
[7, 7, 7, 7, 7, 7, 7, 7, 5, 0, 0, 4, 7, 3, 0, 1, 6, 7, 7, 7, 7, 7, 7, 7],
[7, 7, 7, 7, 7, 7, 7, 6, 0, 0, 3, 5, 5, 5, 2, 0, 1, 7, 7, 7, 7, 7, 7, 7],
[7, 7, 7, 7, 7, 7, 6, 0, 0, 4, 6, 1, 2, 2, 6, 2, 0, 1, 7, 7, 7, 7, 7, 7],
[7, 7, 7, 7, 7, 5, 0, 0, 3, 7, 6, 3, 5, 3, 7, 6, 2, 0, 1, 6, 7, 7, 7, 7],
[7, 7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
mc = Minecraft.create(address,4711)
gradient_blocks = {
7: 0, # Air Block for white
6: 82, # Clay for light gray
5: 4, # Cobblestone for gray
4: 1, # Stone for medium gray
3: 98, # Stone Bricks for dark gray
2: 112, # Nether Brick for darker gray
1: 173, # Coal Block for nearly black
0: 49 # Obsidian for black
}
def put(x, y, z, block_id):
# Placeholder function to simulate placing a block in Minecraft.
# Replace this with the actual command to place a block in your Minecraft setup.
print(f"Placing block at ({x}, {y}, {z}) with block ID {block_id}")
mc.setBlock(x, y, z, block_id)
def build_image_from_data(center_x, center_y, center_z, data):
height = len(data)
width = len(data[0])
for y in range(height):
for x in range(width):
# Convert grid coordinates to Minecraft world coordinates
world_x = center_x + x - width // 2
world_y = center_y - y + height // 2 # In Minecraft, the Y coordinate is height
world_z = center_z
put(world_x, world_y, world_z, gradient_blocks[data[y][x]])
pos = mc.player.getPos()
center_x, center_y, center_z = map(floor, [pos.x, pos.y, pos.z]) # Starting coordinates for the "G"
build_image_from_data(center_x, center_y, center_z, image)
from PIL import Image
def pixel_to_gray_scale_value(pixel):
"""Convert a pixel to grayscale using the luminosity method."""
# If the image is in RGBA, pixel will be a tuple of 4 values (R, G, B, A).
# If the image is in RGB, pixel will be a tuple of 3 values (R, G, B).
# We convert it to grayscale assuming RGB or RGBA:
if len(pixel) == 4: # RGBA
r, g, b, _ = pixel
else: # RGB
r, g, b = pixel
# Luminosity method:
return int(0.3 * r + 0.59 * g + 0.11 * b)
def map_gray_scale_to_range(value):
"""Map grayscale value (0-255) to 0-7 scale."""
# Divide the 256 grayscale levels into 8 segments (0-7).
# Adjust the range as needed to map grayscale values to 0-7.
return value // 32
def convert_image_to_array(filename):
# Load the image
img = Image.open(filename)
img_resized = img.resize((24, 24), Image.LANCZOS)
# Convert the image to RGB if it's in a different mode (e.g., palette)
if img_resized.mode != 'RGB' and img_resized.mode != 'RGBA':
img_resized = img_resized.convert('RGB')
width, height = img_resized.size
# Initialize the 2D array
array = [[0 for _ in range(width)] for _ in range(height)]
# Iterate through each pixel
for y in range(height):
for x in range(width):
# Get the pixel value
pixel = img_resized.getpixel((x, y))
gray_value = pixel_to_gray_scale_value(pixel)
mapped_value = map_gray_scale_to_range(gray_value)
# Assign the mapped value to the array
array[y][x] = mapped_value
return array
# Example usage:
filename = "c:/temp/house_b.png"
array = convert_image_to_array(filename)
print(array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment