1
1
from PIL import Image , ImageOps
2
2
from xiaolinWusLineAlgorithm import draw_line
3
- from queue import PriorityQueue
4
3
import numpy as np
5
4
6
-
7
5
class StringArt :
8
- def __init__ (self , nails , input_image , thickness = 2 ):
6
+ def __init__ (self , nails , input_image , resolution = 0.7 ):
9
7
if type (input_image ) == str :
10
8
self .image = self .load_image (input_image )
11
9
else :
12
10
self .image = input_image
13
- self .scale = 1 - ( thickness - 1 ) / self . image . width
11
+ self .scale = resolution
14
12
self .image = self .image .resize ((round (self .image .width * self .scale ), round (self .image .height * self .scale )), Image .Resampling .LANCZOS )
15
13
self .nails = nails
16
14
self .radius = min (self .image .height , self .image .width )* 0.49
@@ -30,46 +28,35 @@ def nailToCoordinate(self, nail):
30
28
def getLine (self , start , end ):
31
29
p0 = self .nailToCoordinate (start )
32
30
p1 = self .nailToCoordinate (end )
33
- if p1 == p0 :
34
- return 2000
35
31
sum = [0.0 , 0.1 ]
36
32
def pixel (img , p , color , alpha_correction , transparency ):
37
33
sum [0 ] += transparency * img .getpixel (p )
38
34
sum [1 ] += transparency
39
35
draw_line (self .image , p0 , p1 , 0 , 1.0 , pixel )
40
36
return sum [0 ]/ sum [1 ]
41
37
42
- def drawLine (self , start , end , color = 200 , alpha_correction = 1 ):
38
+ def drawLine (self , start , end , color = 200 , alpha_correction = 1 , function = None ):
43
39
p0 = self .nailToCoordinate (start )
44
40
p1 = self .nailToCoordinate (end )
45
- draw_line (self .image , p0 , p1 , color , alpha_correction )
41
+ if function is None :
42
+ draw_line (self .image , p0 , p1 , color , alpha_correction )
43
+ else :
44
+ draw_line (self .image , p0 , p1 , color , alpha_correction , function )
46
45
self .operations .append ((start , end ))
47
46
48
- def tryChange (self , start , end , color = 200 , alpha_correction = 1 ):
47
+ def tryChange (self , start , end , color = 200 , alpha_correction = 1 , function = None ):
49
48
self .pending_img = self .image .copy ()
50
- p0 = self .nailToCoordinate (start )
51
- p1 = self .nailToCoordinate (end )
52
- draw_line (self .pending_img , p0 , p1 , color , alpha_correction )
49
+ draw_line (self .pending_img , start , end , color , alpha_correction , function )
53
50
self .pending_operation = (start ,end )
54
51
55
52
return self .pending_img
56
53
57
54
def acceptChange (self ):
58
55
self .image = self .pending_img
59
56
self .operations .append (self .pending_operation )
60
-
61
- def printOperations (self , file = None ):
62
- pass
63
-
57
+
64
58
def invert (self ):
65
59
self .image = ImageOps .invert (self .image )
66
-
67
60
68
-
69
- def random_pattern (nails , thread ):
70
- import random
71
- print (nails )
72
- for i in range (thread ):
73
- a = random .randint (0 ,nails )
74
- b = random .randint (0 ,nails )
75
- print (f"{ a } { b } " )
61
+ def printOperations (self , file = None ):
62
+ pass # TODO implement
0 commit comments