Skip to content

Instantly share code, notes, and snippets.

@hronecviktor
Created December 11, 2013 11:05
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/7908567 to your computer and use it in GitHub Desktop.
Save hronecviktor/7908567 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.
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.3.3 (C:/Python33/python.exe)" project-jdk-type="Python SDK" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/krizovky.iml" filepath="$PROJECT_DIR$/.idea/krizovky.iml" />
</modules>
</component>
</project>
<component name="DependencyValidationManager">
<state>
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</state>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<option name="TRACKING_ENABLED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="ChangesViewManager" flattened_view="true" show_ignored="false" />
<component name="CreatePatchCommitExecutor">
<option name="PATCH_PATH" value="" />
</component>
<component name="DaemonCodeAnalyzer">
<disable_hints />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectReloadState">
<option name="STATE" value="0" />
</component>
<component name="RunManager">
<list size="0" />
</component>
<component name="ShelveChangesManager" show_recycled="false" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task" />
<servers />
</component>
<component name="VcsContentAnnotationSettings">
<option name="myLimit" value="2678400000" />
</component>
<component name="VcsManagerConfiguration">
<option name="myTodoPanelSettings">
<TodoPanelSettings />
</option>
</component>
<component name="XDebuggerManager">
<breakpoint-manager />
</component>
</project>
__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