#!/usr/bin/python
import sys
import os
import random
import math
from PIL import Image

# get all backgrounds filenames in 'backgrounds' directory
bkgDir = os.path.join(os.getcwd(), './backgrounds')
bkgFilenames = [f for f in os.listdir(bkgDir) if os.path.isfile(os.path.join(bkgDir, f))]

# get all spheres filenames in 'spheres' directory
spheresDir = os.path.join(os.getcwd(), './spheres')
spheresFilenames = [f for f in os.listdir(spheresDir) if os.path.isfile(os.path.join(spheresDir, f))]

# create 'dataset' directory, if it doesn't exist
datasetDir = os.path.join(os.getcwd(), './dataset')
if not os.path.exists(datasetDir):
    os.makedirs(datasetDir)

# generate N dataset images
for x in range(10):
    try:
        # choose a random background
        bkgFilename = random.choice(bkgFilenames)
        background = Image.open(os.path.join(bkgDir, bkgFilename))

        # create new greyscale image
        result = Image.new('L', (200, 200))

        # copy a 100x100px patch from background and paste it into result
        bkgPatch = background.crop((0, 0, 100, 100))
        result.paste(bkgPatch, (50, 50, 150, 150))

        # with probability 50% add a sphere to the image
        if random.random() < 0.5:
            # choose a random sphere
            sphereFilename = random.choice(spheresFilenames)
            sphere = Image.open(os.path.join(spheresDir, sphereFilename))

            # copy and resize patch from sphere and paste it into result
            spherePatch = sphere.crop((5, 5, 205, 205))
            sphereDiameter = int(random.random() * 90 + 10) # random integer value in [10, 100]
            sphereCenter = (random.random() * 100 + 50, random.random() * 100 + 50)
            spherePatch = spherePatch.resize((sphereDiameter, sphereDiameter))
            pasteBox = (int(sphereCenter[0] - math.floor(sphereDiameter / 2.0)), int(sphereCenter[1] - math.floor(sphereDiameter / 2.0)), \
                        int(sphereCenter[0] + math.ceil(sphereDiameter / 2.0)), int(sphereCenter[1] + math.ceil(sphereDiameter / 2.0))) 
            result.paste(spherePatch, \
                         pasteBox,
                         spherePatch) # the second spherePatch is used as alpha mask

        # crop to 100x100
        result = result.crop((50, 50, 150, 150))

        # save result
        result.save(os.path.join(datasetDir, 'data%d.png' % x))
    except IOError as e:
        print('I/O Error(%d): %s' % (e.errno, e.strerror))


@@ -964,117 +964,498 @@
  # c
-opy a 100x100px patch from background and paste it into result%0A        bkgPatch = background.crop((0, 0, 100,
+heck that the background image is large enough%0A        if (background.size%5B0%5D %3C 100 or background.size%5B1%5D %3C 100):%0A            print('Warning: background must be at least 100x100px. %25s skipped.' %25 bkgFilename)%0A            continue%0A%0A        # copy a 100x100px patch from background and paste it into result%0A        left, top = int(random.random() * (background.size%5B0%5D - 100)), int(random.random() * (background.size%5B1%5D - 100))%0A        bkgPatch = background.crop((left, top, left + 100, top +
  100

Copy random patches from backgrounds.