-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground_generator.py
More file actions
88 lines (71 loc) · 2.56 KB
/
background_generator.py
File metadata and controls
88 lines (71 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import cv2
import math
import os
import random
import numpy as np
from PIL import Image, ImageDraw, ImageFilter
def gaussian_noise(height, width):
"""
Create a background with Gaussian noise (to mimic paper)
"""
# We create an all white image
image = np.ones((height, width)) * 255
# We add gaussian noise
cv2.randn(image, 235, 10)
return Image.fromarray(image).convert('RGBA')
def plain_white(height, width):
"""
Create a plain white background
"""
return Image.new("L", (width, height), 255).convert('RGBA')
def quasicrystal(height, width):
"""
Create a background with quasicrystal (https://en.wikipedia.org/wiki/Quasicrystal)
"""
image = Image.new("L", (width, height))
pixels = image.load()
frequency = random.random() * 30 + 20 # frequency
phase = random.random() * 2 * math.pi # phase
rotation_count = random.randint(10, 20) # of rotations
for kw in range(width):
y = float(kw) / (width - 1) * 4 * math.pi - 2 * math.pi
for kh in range(height):
x = float(kh) / (height - 1) * 4 * math.pi - 2 * math.pi
z = 0.0
for i in range(rotation_count):
r = math.hypot(x, y)
a = math.atan2(y, x) + i * math.pi * 2.0 / rotation_count
z += math.cos(r * math.sin(a) * frequency + phase)
c = int(255 - round(255 * z / rotation_count))
pixels[kw, kh] = c # grayscale
return image.convert('RGBA')
def picture(height, width):
"""
Create a background with a picture
"""
print('asdfzxxxxxxxxxxxxxxxx')
pictures = os.listdir('./pictures')
if len(pictures) > 0:
picture = Image.open('./pictures/' + pictures[random.randint(0, len(pictures) - 1)])
if picture.size[0] < width:
picture = picture.resize([width, int(picture.size[1] * (width / picture.size[0]))], Image.ANTIALIAS)
elif picture.size[1] < height:
picture.thumbnail([int(picture.size[0] * (height / picture.size[1])), height], Image.ANTIALIAS)
if (picture.size[0] == width):
x = 0
else:
x = random.randint(0, picture.size[0] - width)
if (picture.size[1] == height):
y = 0
else:
y = random.randint(0, picture.size[1] - height)
return picture.crop(
(
x,
y,
x + width,
y + height,
)
)
else:
raise Exception('No images where found in the pictures folder!')