convert image to grayscale and scale it down. exploit numpy's slice assignment

todo: find out how to pass an array of slices to numpy so we can have it do the i and j loops for us.
This commit is contained in:
David Vierra 2010-10-25 01:14:44 -10:00
parent 45e2e97945
commit e2c93f5c06

33
mce.py
View File

@ -3,6 +3,7 @@ import mclevel
import sys
import os
from box import BoundingBox
import numpy
from numpy import zeros, bincount
import logging
import itertools
@ -905,27 +906,27 @@ class mce(object):
if raw_input(
"This will destroy a large portion of the map and may take a long time. Did you really want to do this?"
).lower() in ("yes", "y", "1", "true"):
from PIL import Image
import datetime
filename = command.pop(0)
imgobj = Image.open(filename)
png = imgobj.load()
width, height = imgobj.size
for x in range(width):
for y in range(height):
if png[x,y][0] >= 128:
print "Pixel value out of range!"
raise UsageError;
if png[x,y][0] != png[x,y][1] or png[x,y][1] != png[x,y][2]:
print "You want a grayscale image here!"
raise UsageError;
print "PNG checked"
greyimg = imgobj.convert("L") #luminance
ychunks = height/16 if (height % 16 == 0) else (height/16) + 1
xchunks = width/16 if (width % 16 == 0) else (width/16) + 1
imgarray = numpy.asarray(greyimg)
width, height = greyimg.size
imgarray = imgarray / 2; #scale to 0-127
ychunks = (height+15)/16
xchunks = (width+15)/16
start = datetime.datetime.now()
for cx in range(xchunks):
@ -938,8 +939,10 @@ class mce(object):
for i in range(16):
for j in range(16):
if i+(cx*16) < width-1 and j+(cy*16) < height-1:
for z in range(png[i+(cx*16),j+(cy*16)][0]):
c.Blocks[i,j,z] = 3 #dirt
#for z in range(png[i+(cx*16),j+(cy*16)][0]):
h = imgarray[i+(cx*16),j+(cy*16)]
c.Blocks[i,j,0:h+1] = 3 #dirt
c.chunkChanged()
print "%s Just did chunk %d,%d" % (datetime.datetime.now().strftime("[%H:%M:%S]"),cx,cy)