Skip to content

Instantly share code, notes, and snippets.

@hronecviktor
Created December 11, 2013 11:07
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 hronecviktor/7908588 to your computer and use it in GitHub Desktop.
Save hronecviktor/7908588 to your computer and use it in GitHub Desktop.
Python - painted crossword generator. Takes BW BMP and outputs a blank or filled map with number groups.
__author__ = 'zamr'
from PIL import Image
#GETS A GRID
def getPixels(pix,w,h):
lines=[]
for _h in range(h):
line=[]
for _w in range(w):
if pix[_w,_h]==0:
line.append("[ ]")
else:
line.append("[X]")
lines.append(line)
return lines
#GETS AN ARRAY OF HORIZONTAL OR VERTICAL GROUPS
def getNums(lines):
hornums=[]
for line in lines:
hor_line=[0]*len(lines)
groupcnt=0
for place in line:
if place=="[ ]" and hor_line[groupcnt] == 0:
continue
if place=="[ ]" and hor_line[groupcnt] > 0:
groupcnt=groupcnt+1
if place=="[X]" and hor_line[groupcnt] > -1:
hor_line[groupcnt]=hor_line[groupcnt]+1
hor_line=[ x for x in hor_line if x!=0]
hornums.append(hor_line)
return hornums
#REVERTS DIMENSIONS OF VERTICAL GROUP ARRAY
def revArray(arr):
x=[]
for _ in range(len(arr)):
line=[]
for _inner in range(len(arr)):
line.append(arr[_inner][_])
x.append(line)
return x
#PRINTS PROPERLY FORMATTED OUTPUT OF VERTICAL NUMS
def formatVerOutput(vnums, hnums,imagedim, maxlen):
verticalmax=int((len(imagedim)/2)+1)
horizontalmax=int((len(imagedim[0])/2)+1)
vout=[str()]*len(imagedim[0])
#CREATE 'nn|' GROUPS
for _ in range(len(imagedim[0])):
voutline=str()
for num in vnums[_]:
vnumposition=str(num).rjust(2,' ')
vnumposition+="|"
voutline+=vnumposition
vout[_]=" |"*(verticalmax-len(vnums[_]))+voutline
#ARRANGE THE ARRAY FOR PRINT
formattedvout=[str()]*verticalmax
line=[str()]*verticalmax
for piece in range(0,verticalmax*3,3):
testline=str()
for row in range(len(imagedim[0])):
testline+=(vout[row][piece:piece+3])
formattedvout[int(piece/3)]=testline
#PRINT ONLY LINES WHICH CONTAIN GROUPS
for _ in formattedvout:
if str(_.rjust((horizontalmax*4)+len((imagedim[0])*3),' ')).replace("|"," ").replace(" ","").isnumeric():
print(_.rjust(maxlen,' '))
#RETURNS PROPERLY FORMATTED OUTPUT OF HORIZONTAL GROUPS
def formatHorOutput(vnums, hnums,imagedim):
verticalmax=int((len(imagedim)/2)+1)
horizontalmax=int((len(imagedim[0])/2)+1)
output=[]
hout=[str()]*len(imagedim)
for _ in range(len(hnums)):
for num in hnums[_]:
numposition=str(num).rjust(2,' ')
hout[_]+=numposition.center(4,'|')
hout[_]=hout[_].rjust(horizontalmax*4,' ')
return hout
def main(imgpath, blank=True):
#INIT
im = Image.open(imgpath)
pix = im.load()
w , h = im.size
#GETTING PIXEL MAP
imagemap=getPixels(pix,w,h)
#GETTING GROUPS
verticalnumbers=getNums(revArray(imagemap))
horizontalnumbers=getNums(imagemap)
#GETTING OUTPUT OF HORIZONTAL GROUPS
horoutput=formatHorOutput(verticalnumbers,horizontalnumbers,imagemap)
#IF BLANK=TRUE - RENDER THE PIXELMAP EMPTY
if blank:
for _ in range(len(imagemap)):
for __ in range(len(imagemap[_])):
imagemap[_][__]=imagemap[_][__].replace("X"," ")
#DETERMINE THE WIDTH OF OUTPUT
maxlen=0
for _ in range(len(imagemap)):
if maxlen<len((("{}"*(len(imagemap[0])+1)).format(horoutput[_],*imagemap[_])).strip(" ")):
maxlen=len((("{}"*(len(imagemap[0])+1)).format(horoutput[_],*imagemap[_])).strip(" "))
#PRINT VERTICAL GROUPS WITH PROPER INDENTATION
formatVerOutput(verticalnumbers,horizontalnumbers,imagemap,maxlen)
#PRINT HORIZONTAL GROUPS AND IMAGEMAP PROPERLY INDENTED
for _ in range(len(imagemap)):
print((("{}"*(len(imagemap[0])+1)).format(horoutput[_],*imagemap[_])).strip(" ").rjust(maxlen," "))
main("C:/testPython/testbmp25x25.bmp", False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment