summaryrefslogtreecommitdiff
path: root/ricky
diff options
context:
space:
mode:
Diffstat (limited to 'ricky')
-rw-r--r--ricky/config.py1
-rw-r--r--ricky/param/__init__.py17
-rw-r--r--ricky/param/bool.py6
-rw-r--r--ricky/param/color.py5
-rw-r--r--ricky/param/constrainednumber.py4
-rw-r--r--ricky/param/enum.py6
-rw-r--r--ricky/params.py36
-rw-r--r--ricky/pbbreaker/probability.json35
-rw-r--r--ricky/pbgradient/probabilities.json120
-rw-r--r--ricky/pbgradient/probabilities.py102
-rw-r--r--ricky/pbgrid/probabilities.json87
11 files changed, 62 insertions, 357 deletions
diff --git a/ricky/config.py b/ricky/config.py
index 819def3..9d0ddfa 100644
--- a/ricky/config.py
+++ b/ricky/config.py
@@ -10,3 +10,4 @@ IMGRID_URL = "http://localhost:8999/im/api/imgrid"
IMGRADIENT_URL = "http://localhost:8999/im/api/imgradient"
IMBREAK_URL = "http://localhost:8999/im/api/imbreak"
OFFLINE = False
+PROBABILITIES_DIR = "share/probabilities"
diff --git a/ricky/param/__init__.py b/ricky/param/__init__.py
index cbacf6f..4ec8204 100644
--- a/ricky/param/__init__.py
+++ b/ricky/param/__init__.py
@@ -1,4 +1,5 @@
import pprint
+import random
class Param(object):
@@ -53,7 +54,21 @@ class Param(object):
default = property(default_get, default_set)
- def randomize(self):
+ def _choose_from_probabilities(self, probabilities):
+ """
+ if using weights, considers all weights as a percentage of
+ 100
+ """
+ choice = random.randint(0, 100)
+ position = 0
+ for elem in probabilities:
+ position += elem["weight"]
+ if position >= choice:
+ self.value = elem["value"]
+ return True
+ return True
+
+ def randomize(self, probabilities=None):
pass
def from_normalized(self, value):
diff --git a/ricky/param/bool.py b/ricky/param/bool.py
index 2b25bbd..4f75dc6 100644
--- a/ricky/param/bool.py
+++ b/ricky/param/bool.py
@@ -32,8 +32,10 @@ class Bool(Param):
raise ValueError("Bad Value for Bool %s" % value)
return value
- def randomize(self):
- self.value_set(random.choice([True, False]))
+ def randomize(self, probabilities=None):
+ if probabilities and self._choose_from_probabilities(probabilities):
+ return
+ self.value = random.choice([True, False])
def from_normalized(self, value):
value_as_int = int(round(value, 0))
diff --git a/ricky/param/color.py b/ricky/param/color.py
index 5da05ed..7abc23a 100644
--- a/ricky/param/color.py
+++ b/ricky/param/color.py
@@ -35,10 +35,11 @@ class Color(Param):
value = int(self.as_hex(), 16)
return decimal.Decimal(value)/decimal.Decimal(maximum)
- def randomize(self):
+ def randomize(self, probabilities=None):
+ if probabilities and self._choose_from_probabilities(probabilities):
+ return
self.value = "rgb(%s,%s,%s)" % (
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255)
)
- self._is_rgb = True
diff --git a/ricky/param/constrainednumber.py b/ricky/param/constrainednumber.py
index 3563d22..3af3bf9 100644
--- a/ricky/param/constrainednumber.py
+++ b/ricky/param/constrainednumber.py
@@ -73,7 +73,9 @@ class ConstrainedNumber(Param):
value = int(value)
self.value = value
- def randomize(self, tries=0):
+ def randomize(self, tries=0, probabilities=None):
+ if probabilities and self._choose_from_probabilities(probabilities):
+ return
tries_max = 30
try:
self._generate_random()
diff --git a/ricky/param/enum.py b/ricky/param/enum.py
index d4a839a..137c58e 100644
--- a/ricky/param/enum.py
+++ b/ricky/param/enum.py
@@ -30,8 +30,10 @@ class Enum(Param):
value = property(value_get, value_set)
- def randomize(self):
- self.value_set(random.choice(self._options))
+ def randomize(self, probabilities=None):
+ if probabilities and self._choose_from_probabilities(probabilities):
+ return
+ self.value = random.choice(self._options)
def as_dict(self):
my_dict = super(Enum, self).as_dict()
diff --git a/ricky/params.py b/ricky/params.py
index 4f91776..476e0d2 100644
--- a/ricky/params.py
+++ b/ricky/params.py
@@ -1,5 +1,9 @@
"""base class for all params"""
import pprint
+import simplejson as json
+import sys
+import os
+from ricky.config import PROBABILITIES_DIR
class Params(object):
@@ -18,6 +22,28 @@ class Params(object):
"""string representation"""
return pprint.pformat(self.as_dict())
+ def _load_probabilities_json(self, probabilities_file=None):
+ if probabilities_file:
+ filepath = probabilities_file
+ else:
+ filepath = os.path.join(
+ PROBABILITIES_DIR,
+ "%s.json" % (self.api.__class__.__name__)
+ )
+ try:
+ f = open(filepath, 'r')
+ data = f.read()
+ f.close()
+ return json.loads(data)
+ except json.scanner.JSONDecodeError as e:
+ sys.stderr.write("Invalid Json - Problem decoding %s\n" % filepath)
+ sys.stderr.write("%s\n" % e)
+ sys.exit(1)
+ except IOError:
+ sys.stderr.write(
+ "Could not find probabilities file %s\n" % filepath)
+ sys.exit(1)
+
def randomize(
self,
probabilities=None,
@@ -27,13 +53,13 @@ class Params(object):
if using a probabilities.json file, weight is taken
into account"""
if probabilities:
- probabilities = self._load_probabilities(probabilities)
- else if probabilities_local:
- probabilities = self._load_probabilities(probabilities_local)
+ probabilities_dict = self._load_probabilities_json(probabilities)
+ elif probabilities_local:
+ probabilities_dict = self._load_probabilities_json()
else:
- probabilities = {}
+ probabilities_dict = {}
for param in self._params:
- param.randomize(probability=probabilities.get(param.name))
+ param.randomize(probabilities=probabilities_dict.get(param.name))
@property
def api(self):
diff --git a/ricky/pbbreaker/probability.json b/ricky/pbbreaker/probability.json
deleted file mode 100644
index 9c28e6d..0000000
--- a/ricky/pbbreaker/probability.json
+++ /dev/null
@@ -1,35 +0,0 @@
-{
- "breaktype" :[
- {"value": "CLASSIC", "weight": 1},
- {"value": "REDUX", "weight": 1},
- {"value": "BLURRY_BREAK", "weight": 1},
- {"value": "BLURRY_BREAK_2", "weight": 1},
- {"value": "SWIPE", "weight": 1},
- {"value": "RGB_WASH", "weight": 1},
- {"value": "RGB_WASH_2", "weight": 1},
- {"value": "NOISY_BREAK", "weight": 1},
- {"value": "BROKEN_VIGNETTE", "weight": 1},
- {"value": "FAX_MACHINE", "weight": 1},
- {"value": "STRIPES", "weight": 1},
- {"value": "PHOTOCOPY", "weight": 1}
- ],
- "breakmode" :[
- {"value": "extreme", "weight": 1},
- {"value": "subtle", "weight": 1}
- ],
- "finalformat" :[
- {"value": "png", "weight": 5},
- {"value": "jpg", "weight": 2},
- {"value": "gif", "weight": 2}
- ],
- "breakangle" :[
- {"value": 0, "weight": 9},
- {"value": 90, "weight": 2},
- {"value": -180, "weight": 2},
- {"value": 180, "weight": 2}
- ],
- "expanded" :[
- {"value": "", "weight": 11},
- {"value": 1, "weight": 2}
- ]
-}
diff --git a/ricky/pbgradient/probabilities.json b/ricky/pbgradient/probabilities.json
deleted file mode 100644
index 0c6c0df..0000000
--- a/ricky/pbgradient/probabilities.json
+++ /dev/null
@@ -1,120 +0,0 @@
-{
- "width" : [
- {"value": 40, "weight": 10}
- ],
- "height" : [
- {"value": 400, "weight": 100}
- ],
- "color1" : [
- {"value": "", "weight": 0},
- {"value": "black", "weight": 1},
- {"value": "white", "weight": 2}
- ],
- "color2" : [
- {"value": "", "weight": 0},
- {"value": "black", "weight": 2},
- {"value": "white", "weight": 1}
- ],
- "stripes" : [
- {"value": "true", "weight": 3},
- {"value": "false", "weight": 1}
- ],
- "stripenumber" : [
- {"value": 3, "weight": 10},
- {"value": 10, "weight": 10},
- {"value": 20, "weight": 10},
- {"value": 100, "weight": 10},
- {"value": 40, "weight": 10},
- {"value": 1, "weight": 50},
- {"value": 2, "weight": 50},
- {"value": 2, "weight": 50}
- ],
- "stripeintensity" : [
- {"value": 1000, "weight": 10},
- {"value": 4, "weight": 10}
- ],
- "contrast" : [
- {"value": "", "weight": 0},
- {"value": "", "weight": 300}
- ],
- "brightness" : [
- {"value": "", "weight": 0},
- {"value": "", "weight": 300}
- ],
- "saturation" : [
- {"value": "", "weight": 0},
- {"value": "", "weight": 300}
- ],
- "hue" : [
- {"value": "", "weight": 0},
- {"value": "", "weight": 300}
- ],
- "halftone" : [
- {"value": "", "weight": 60},
- {"value": "checkeredfade", "weight": 10},
- {"value": "etchedtransition", "weight": 10},
- {"value": "bendaydots", "weight": 10},
- {"value": "smallerdots1", "weight": 10},
- {"value": "smallerdots2", "weight": 10},
- {"value": "flatstripes", "weight": 10}
- ],
- "bevel" : [
- {"value": "", "weight": 4},
- {"value": "flatout", "weight": 1},
- {"value": "flatinner", "weight": 0},
- {"value": "evenlyframed", "weight": 1},
- {"value": "biginner", "weight": 1},
- {"value": "bigouter", "weight": 1},
- {"value": "dramaticflatout", "weight": 1},
- {"value": "dramaticflatinner", "weight": 1}
- ],
- "blurriness" : [
- {"value": 30, "weight": 10},
- {"value": 10, "weight": 10},
- {"value": 5, "weight": 10},
- {"value": 20, "weight": 10},
- {"value": 25, "weight": 10},
- {"value": 7, "weight": 10},
- {"value": "", "weight": 1}
- ],
- "percentbeveled" : [
- {"value": 30, "weight": 10},
- {"value": 10, "weight": 10},
- {"value": 5, "weight": 10},
- {"value": 20, "weight": 10},
- {"value": 25, "weight": 10},
- {"value": 7, "weight": 10},
- {"value": "", "weight": 1}
- ],
- "rotate" : [
- {"value": 0, "weight": 200},
- {"value": 90, "weight": 2},
- {"value": 180, "weight": 2},
- {"value": 270, "weight": 2}
- ],
- "tilt" : [
- {"value": 0, "weight": 200},
- {"value": 90, "weight": 2},
- {"value": 180, "weight": 2},
- {"value": 270, "weight": 2}
- ],
- "flop_probabilities : flip" : [
- {"value": "", "weight": 1},
- {"value": "true", "weight": 1}
- ],
- "filetype" : [
- {"value": "png", "weight": 10},
- {"value": "jpg", "weight": 2},
- {"value": "gif", "weight": 2}
- ],
- "gradienttype" : [
- {"value": "canvas", "weight": 1},
- {"value": "gradient", "weight": 5},
- {"value": "radial", "weight": 1},
- {"value": "colorspace", "weight": 1},
- {"value": "plasmawash", "weight": 2},
- {"value": "gradientwash", "weight": 1},
- {"value": "mirrored", "weight": 0},
- {"value": "noise", "weight": 1}
- ]
-}
diff --git a/ricky/pbgradient/probabilities.py b/ricky/pbgradient/probabilities.py
deleted file mode 100644
index d77739f..0000000
--- a/ricky/pbgradient/probabilities.py
+++ /dev/null
@@ -1,102 +0,0 @@
-from ricky.param.probabilities import Probabilities
-
-width_probabilities = Probabilities.from_dict(
- {"value": 40, "weight": 10},
-)
-height_probabilities = Probabilities.from_dict(
- {"value": 400, "weight": 100},
-)
-color1_probabilities = Probabilities.from_dict(
- {"value": "", "weight": 0},
-# {"value": "black", "weight": 1},
-# {"value": "white", "weight": 2},
-)
-color2_probabilities = Probabilities.from_dict(
- {"value": "", "weight": 0},
-# {"value": "black", "weight": 2},
-# {"value": "white", "weight": 1},
-)
-stripes_probabilities = Probabilities.from_dict(
- {"value": "true", "weight": 3},
- {"value": "false", "weight": 1},
-)
-stripenumber_probabilities = Probabilities.from_dict(
- {"value": 3, "weight": 10},
- {"value": 10, "weight": 10},
- {"value": 20, "weight": 10},
- {"value": 100, "weight": 10},
- {"value": 40, "weight": 10},
-# {"value": 1, "weight": 50},
-# {"value": 2, "weight": 50},
-# {"value": 2, "weight": 50},
-
-)
-stripeintensity_probabilities = Probabilities.from_dict(
- {"value": 1000, "weight": 10},
- {"value": 4, "weight": 10},
-)
-# contrast_probabilities = \
-brightness_probabilities = \
- saturation_probabilities = \
- hue_probabilities = \
- Probabilities.from_dict(
- {"value": "", "weight": 0},
-# {"value": "", "weight": 300},
-)
-halftone_probabilities = Probabilities.from_dict(
- {"value": "", "weight": 60},
- {"value": "checkeredfade", "weight": 10},
- {"value": "etchedtransition", "weight": 10},
- {"value": "bendaydots", "weight": 10},
- {"value": "smallerdots1", "weight": 10},
- {"value": "smallerdots2", "weight": 10},
- {"value": "flatstripes", "weight": 10},
-)
-bevel_probabilities = Probabilities.from_dict(
- {"value": "", "weight": 4},
- {"value": "flatout", "weight": 1},
- {"value": "flatinner", "weight": 0},
- {"value": "evenlyframed", "weight": 1},
-# {"value": "biginner", "weight": 1},
- {"value": "bigouter", "weight": 1},
- {"value": "dramaticflatout", "weight": 1},
-# {"value": "dramaticflatinner", "weight": 1},
-)
-
-blurriness_probabilities = \
- percentbeveled_probabilities = Probabilities.from_dict(
- {"value": 30, "weight": 10},
- {"value": 10, "weight": 10},
- {"value": 5, "weight": 10},
- {"value": 20, "weight": 10},
- {"value": 25, "weight": 10},
- {"value": 7, "weight": 10},
- {"value": "", "weight": 1},
-)
-rotate_probabilities = \
- tilt_probabilities = Probabilities.from_dict(
- {"value": 0, "weight": 200},
- {"value": 90, "weight": 2},
- {"value": 180, "weight": 2},
- {"value": 270, "weight": 2},
-)
-flop_probabilities = flip_probabilities = Probabilities.from_dict(
- {"value": "", "weight": 1},
- {"value": "true", "weight": 1},
-)
-
-filetype_probabilities = Probabilities.from_dict(
- {"value": "png", "weight": 10},
- {"value": "jpg", "weight": 2},
- {"value": "gif", "weight": 2},
-)
-gradienttype_probabilities = Probabilities.from_dict(
- {"value": "canvas", "weight": 1},
- {"value": "gradient", "weight": 5},
- {"value": "radial", "weight": 1},
- {"value": "colorspace", "weight": 1},
- {"value": "plasmawash", "weight": 2},
- {"value": "gradientwash", "weight": 1},
- {"value": "mirrored", "weight": 0},
- {"value": "noise", "weight": 1},
-)
diff --git a/ricky/pbgrid/probabilities.json b/ricky/pbgrid/probabilities.json
deleted file mode 100644
index f9defe8..0000000
--- a/ricky/pbgrid/probabilities.json
+++ /dev/null
@@ -1,87 +0,0 @@
-{
- "format" : [
- { "weight": 20, "value": "png" },
- { "weight": 0, "value": "gif" },
- { "weight": 0, "value": "jpg" }
- ],
- "transition" : [
- { "value" : "background", "weight": 1 },
- { "value" : "dither", "weight": 1 },
- { "value" : "random", "weight": 1 },
- { "value" : "tile", "weight": 1 },
- { "value" : "edge", "weight": 1 }
- ],
- "skycolor" : [
- { "value" : "white", "weight" : 1 },
- { "value" : "silver", "weight" : 1 },
- { "value" : null, "weight" : 10 }
- ],
- "bgcolor" : [
- { "value" : "white", "weight" : 1 },
- { "value" : "silver", "weight" : 1 },
- { "value" : null, "weight" : 10 }
- ],
- "planebgcolor" : [
- { "value" : "white", "weight" : 1 },
- { "value" : "silver", "weight" : 1 },
- { "value" : null, "weight" : 10 }
- ],
-
- "linecolor" : [
- { "value" : "black", "weight" : 1 },
- { "value" : "white", "weight" : 1 },
- { "value" : "silver", "weight" : 1 }
- ],
- "swing" : [
- {"value": ", "weight": 2},
- {"value": 30, "weight": 1},
- {"value": -30, "weight": 1}
- ],
- "tilt" : [
- {"value": ", "weight": 2},
- {"value": 30, "weight": 1},
- {"value": -30, "weight": 1}
- ],
- "roll" : [
- {"value": ", "weight": 2},
- {"value": 30, "weight": 1},
- {"value": -30, "weight": 1}
- ],
- "width" : "height" : [
- { "value" : 400, "weight" : 1 },
- { "value" : 600, "weight" : 1 }
- ],
- "linethickness" : [
- {"value":1, "weight": 2},
- {"value":2, "weight": 1}
- ],
- "opacity" : [
- {"value":1, "weight": 2},
- {"value":0.5, "weight": 1}
- ],
- "spacing" : [
- {"value":10, "weight": 1},
- {"value":15, "weight": 1}
- ],
- "vlines" : [
- {"value":", "weight": 2},
- {"value":"true", "weight": 1}
- ],
- "hlines" : [
- {"value":", "weight": 2},
- {"value":"true", "weight": 1}
- ],
- "shadow" : [
- {"value":", "weight": 1},
- {"value":"true", "weight": 1}
- ],
- "zoom" : [
- {"value": 0, "weight": 3},
- {"value": 1.2, "weight": 1},
- {"value": -1.2, "weight": 1}
- ],
- "trim" : [
- {"value":", "weight": 1},
- {"value":"true", "weight": 1}
- ]
-}