Skip to content

Instantly share code, notes, and snippets.

@SDraw
Forked from knowuh/photo.py
Last active February 12, 2018 16:06
Show Gist options
  • Save SDraw/ad353652b78cebe382c6 to your computer and use it in GitHub Desktop.
Save SDraw/ad353652b78cebe382c6 to your computer and use it in GitHub Desktop.
#----------------------------------------------------------
# File photo.py - cubify a named image in project.
# @knowuh (Noah Paessel) http://bit.ly/photoblend
# License: MIT ( http://opensource.org/licenses/MIT )
#----------------------------------------------------------
import bpy
from random import uniform
def make_material(color):
alpha = 1.0
red, green, blue, alpha = color
if alpha != 0.0:
color_name = '{0}_{1}_{2}'.format(red, green, blue)
try:
if bpy.data.materials[color_name]:
color = bpy.data.materials[color_name]
except:
color = bpy.data.materials.new(color_name)
color.use_nodes = True
Diffuse_BSDF = color.node_tree.nodes['Diffuse BSDF']
Diffuse_BSDF.inputs[0].default_value = [red, green, blue, alpha]
return color
def draw_pix(x, y, col):
material = make_material(col)
r, g, b, a = col
if a != 0.0:
size = 16 - ((r + g + b) * 4)
z = size
bpy.ops.mesh.primitive_cube_add(location=(x, y, z))
bpy.ops.transform.resize(value=(0.9,0.9,size))
new_obj = bpy.context.active_object
new_obj.data.materials.append(material)
def cubify(image_name):
myImage = bpy.data.images[image_name]
width, height = myImage.size
for y in range(0, height):
for x in range(0, width):
block_number = (y * width) + x
color = []
for color_index in range(4):
index = (block_number * 4) + color_index
color.append(myImage.pixels[index])
draw_pix(x * 2, y * 2, color)
print(x,y)
cubify('photo.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment