Exemplo n.º 1
0
import golly as g
from bitstring import BitArray

try:
	from PIL import Image
	from PIL import ImageSequence
except:
	g.exit("You need to install PIL (Python Imaging Library).")
	 
fname = g.opendialog("Open File", "*.gif", "", "")
if not len(fname):
  g.exit("")
  
outfile = g.savedialog("Save RAW Data File", "*.*", "", "ROM.dat")
if not len(outfile):
	g.exit("")
	
image = Image.open(fname)
buff = BitArray()

for frame in ImageSequence.Iterator(image):
	frame = list(frame.convert("1").getdata())
	for i in range(len(frame)):
		buff.append('0b0' if frame[i] & 0x1 else '0b1')

# normalize to 256k
if buff.length < 1 << 18:
	for i in range ((1 << 18 - 3) - (buff.length >> 3)):
		buff.append('0x00')
elif buff.length > 1 << 18:
	del buff[(1 << 18) + 1 : buff.length]
import golly as g
import os.path

# default base path as the Desktop, change it if needed
default_path = os.path.expanduser('~/Desktop')
# default name is the current pattern name without extension
pattern_name = g.getname().split('.')[0]
# getting an output path
export_path = g.savedialog('Choose the directory and name', 'All files (*)|*',
                           default_path, pattern_name)
if export_path == "":
    export_path = default_path

# number of generations
num_generations = 10

# has a selection been made?
selected = 1 if len(g.getselrect()) > 0 else 0

for i in range(num_generations):
    # advance by one generation (checking if something is selected)
    if selected == 1:
        g.advance(0, 1)
    else:
        g.run(1)
    # export to a new file
    file_path = '%s-%d.rle' % (export_path, i)
    g.show(file_path)
    g.save(file_path, 'rle')
Exemplo n.º 3
0
savename = g.getdir("data") + "save-image.ini"
try:
    # try to get the directory saved by an earlier run
    f = open(savename, 'r')
    initdir = f.readline()
    f.close()
except:
    # this should only happen the very 1st time
    initdir = g.getdir("data")

# remove any existing extension from layer name and append .png
initfile = g.getname().split('.')[0] + ".png"

# prompt user for output file (image type depends on extension)
outfile = g.savedialog(
    "Save image file",
    "PNG (*.png)|*.png|BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif" +
    "|TIFF (*.tif)|*.tif|JPEG (*.jpg)|*.jpg", initdir, initfile)
if len(outfile) > 0:
    im.save(outfile)
    g.show("Image saved as " + outfile)

    # remember file's directory for next time
    try:
        f = open(savename, 'w')
        f.write(os.path.dirname(outfile))
        f.close()
    except:
        g.warn("Unable to save directory to file:\n" + savename)

    # on Mac OS X this opens the image file in Preview
    # but it causes an error on Windows
Exemplo n.º 4
0
# Get catalyst in various positions
if g.getselrect() != []:
    g.shrink()
pattern = g.getcells(r)
patrlelist = []
translist =[(1,0,0,1), (1,0,0,-1), (-1,0,0,1), (-1,0,0,-1),
            (0,1,1,0), (0,-1,1,0), (0,1,-1,0), (0,-1,-1,0)]
# Get unique transformed patterns
for trans in translist:
    g.new('')
    g.putcells(g.transform(pattern, 0, 0, *trans))
    g.select(g.getrect())
    g.copy()
    patrle = ''.join(g.getclipstr().split('\n')[1:])
    if patrle not in patrlelist:
        patrlelist.append(patrle)
patrles = '\n'.join(patrlelist)

# Save data to file
catpath = g.savedialog('Catalyst File', 'all files(*.*)|*', catdir)
try:
    with open(catpath, 'w') as catfile:
        catfile.write(patrles)
    g.exit('Catalyst file successfully generated at {}'.format(catpath))
except:
    if catpath == '':
        g.setclipstr(patrles)
        g.exit('No filename specified, saved it to clipboard instead.')
    else:
        raise
Exemplo n.º 5
0
    return rle_res + "!"


# --------------------------------------------------------------------
glider = g.parse("3o$o$bo!")

# backup way for the script to work:  have user choose a text file containing constellations
if constellation_fname == "/YOUR/PATH/HERE/constellations.txt":  # don't edit the path in this line
    constellation_fname = g.opendialog("Open constellation list", "Text files (*.txt)|*.txt", \
                                       defaultfolder, "constellations.txt", False)
    if constellation_fname == "":
        g.exit("No constellation list found.  Script has exited.")

if output_fname == "/YOUR/PATH/HERE/output-collisions.rle":  # don't edit the path in this line
    output_fname = g.savedialog("Pick a place for the output file", "RLE files (*.rle)|*.rle", \
                                       defaultfolder, "output-collisions.rle", False)
    if output_fname == "":
        g.exit("No output filename provided.  Script has exited.")

# create an empty output file to append results to
with open(output_fname, "w") as outf:
    outf.write("x = 0, y = 0, rule = B3/S23\n")

x, y, count, match = 0, 0, 0, 0

with open(constellation_fname, "r") as f:
    rledata = f.readlines()
for rlestr in rledata:
    g.setlayer(0)
    pat = g.parse(rlestr.replace("\n", ""))
    mindiag, maxdiag = 99999, -99999
import golly as g
import os.path

# default base path as the Desktop, change it if needed
default_path = os.path.expanduser('~/Desktop')
# default name is the current pattern name without extension
pattern_name = g.getname().split('.')[0]
# getting an output path
export_path = g.savedialog('Choose the directory and name',
                           'All files (*)|*',
                           default_path,
                           pattern_name)
if export_path == "":
    export_path = default_path

# number of generations
num_generations = 10

# has a selection been made?
selected = 1 if len(g.getselrect()) > 0 else 0

for i in range(num_generations):
    # advance by one generation (checking if something is selected)
    if selected == 1:
        g.advance(0, 1)
    else:
        g.run(1)
    # export to a new file
    file_path = '%s-%d.rle' % (export_path, i)
    g.show(file_path)
    g.save(file_path, 'rle')
Exemplo n.º 7
0
savename = g.getdir("data") + "save-image.ini"
try:
   # try to get the directory saved by an earlier run
   f = open(savename, 'r')
   initdir = f.readline()
   f.close()
except:
   # this should only happen the very 1st time
   initdir = g.getdir("data")

# remove any existing extension from layer name and append .png
initfile = g.getname().split('.')[0] + ".png"

# prompt user for output file (image type depends on extension)
outfile = g.savedialog("Save image file",
                       "PNG (*.png)|*.png|BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif" +
                       "|TIFF (*.tif)|*.tif|JPEG (*.jpg)|*.jpg",
                       initdir, initfile)
if len(outfile) > 0:
   im.save(outfile)
   g.show("Image saved as " + outfile)
   
   # remember file's directory for next time
   try:
      f = open(savename, 'w')
      f.write(os.path.dirname(outfile))
      f.close()
   except:
      g.warn("Unable to save directory to file:\n" + savename)
   
   # on Mac OS X this opens the image file in Preview
   # but it causes an error on Windows
Exemplo n.º 8
0
def golly_grills():

    bounding_box = g.getrect()
    celllist = g.getcells(bounding_box)

    g.show('Creating problem...')
    gr = interpret_problem(celllist)

    if gr.easy_unsat():
        g.exit('Problem is trivially unsatisfiable')

    gr.update_golly()

    solve_problem = yn2bool(
        g.getstring('Would you like to run the SAT solver now?', 'yes'))
    save_dimacs = solve_problem or yn2bool(
        g.getstring('Would you like to save a DIMACS file to solve later?',
                    'yes'))
    solution_file = None
    dimacs_out = None

    if save_dimacs:
        dimacs_out = g.savedialog('Save DIMACS file',
                                  'DIMACS CNF files (*.cnf)|*.cnf',
                                  g.getdir('data'), 'problem.cnf')

    if solve_problem:
        ss = g.getstring(
            'Please specify maximum duration (in seconds) for lingeling optimisation',
            '300')
        dimacs_dir = dimacs_out[:-4] + '_dir'
        g.show('Invoking lingeling for at most %s seconds...' % ss)

        simp_status = gr.write_and_simplify(dimacs_dir=dimacs_dir,
                                            dimacs_out=dimacs_out,
                                            simptime=int(ss))

        if (simp_status == 'SAT'):
            g.show('Problem is satisfiable')
            solution_file = os.path.join(dimacs_dir, 'solution.txt')
        elif (simp_status == 'UNSAT'):
            g.exit('Problem is unsatisfiable')
        else:
            g.show('Generated head and tail files.')

            if yn2bool(
                    g.getstring(
                        'Would you like to run this in single-CPU iglucose?',
                        'yes')):
                g.show('Running iglucose in single-CPU mode...')
                solution_file = os.path.join(dimacs_dir, 'solution.txt')
                runbash(os.path.join(scriptdir, 'scripts',
                                     'iglucose.sh'), '-stop-at-sat',
                        os.path.join(dimacs_dir, 'full.icnf'), solution_file)

    elif dimacs_out:

        gr.write_dimacs(dimacs_out)

    if solution_file is None:
        if yn2bool(
                g.getstring('Would you like to load a solution file?', 'yes')):
            solution_file = g.opendialog('Load SAT solution',
                                         'All files (*)|*', g.getdir('data'))

    if solution_file:
        gr.load_solution(solution_file)
        gr.update_golly()