-
Notifications
You must be signed in to change notification settings - Fork 2
/
img_to_thread.py
63 lines (50 loc) · 2.37 KB
/
img_to_thread.py
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
from PIL import Image, ImageOps
import math
class StringArt:
def __init__(self, nails, input_image, resolution=0.7):
if type(input_image) == str:
self.image = self.load_image(input_image)
else:
self.image = input_image
self.scale = resolution
self.image = self.image.resize((round(self.image.width*self.scale), round(self.image.height*self.scale)), Image.Resampling.LANCZOS)
self.nails = nails
self.radius = min(self.image.height, self.image.width)*0.49
self.midx = self.image.width / 2
self.midy = self.image.height / 2
self.operations = []
def load_image(self, path):
image = Image.open(path).convert("L") # Convert to grayscale
return image
def nailToCoordinate(self, nail):
#from polar coordinates
return round(self.midx + self.radius*math.cos(2*math.pi*(nail/self.nails))), round(self.midy + self.radius*math.sin(2*math.pi*nail/self.nails))
def getLine(self, start, end):
p0 = self.nailToCoordinate(start)
p1 = self.nailToCoordinate(end)
sum = [0.0, 0.1]
def pixel(img, p, color, transparency):
sum[0] += transparency*img.getpixel(p)
sum[1] += transparency
self.bresenham(p0, p1, 20, 1,pixel)
return sum[0]/sum[1]
def drawLine(self, start, end, color=20, alpha_correction=1, function=None):
p0 = self.nailToCoordinate(start)
p1 = self.nailToCoordinate(end)
self.bresenham(p0, p1, color, alpha_correction, function)
self.operations.append((start, end))
def tryChange(self, start, end, color=20, transparency=1, function=None):
self.pending_img = self.image.copy()
self.bresenham(start, end, color, transparency, function)
self.pending_operation = (start,end)
return self.pending_img
def acceptChange(self):
self.image = self.pending_img
self.operations.append(self.pending_operation)
def invert(self):
self.image = ImageOps.invert(self.image)
def bresenham(self, p0, p1, color=20, transparency=1, function=None):
pass # Implement anti aliasing
def printOperations(self, file=None):
pass # the first line should contain the number of nails
# The other line should be start_nail whitespace end_nail of a line