-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathimage.py
More file actions
194 lines (172 loc) · 6.86 KB
/
image.py
File metadata and controls
194 lines (172 loc) · 6.86 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from PIL import Image, ImageOps, ImageEnhance
from PyQt6 import QtWidgets
import os
from copy import copy
from ..component import Component
from ..toolkit.frame import BlankFrame, addShadow
from ..toolkit.visualizer import createSpectrumArray
class Component(Component):
name = "Image"
version = "2.1.0"
def widget(self, *args):
super().widget(*args)
# cache a modified image object in case we are rendering beyond frame 1
self.existingImage = None
self.page.pushButton_image.clicked.connect(self.pickImage)
self.page.comboBox_resizeMode.addItem("Scale")
self.page.comboBox_resizeMode.addItem("Cover")
self.page.comboBox_resizeMode.addItem("Stretch")
self.page.comboBox_resizeMode.setCurrentIndex(0)
self.trackWidgets(
{
"imagePath": self.page.lineEdit_image,
"scale": self.page.spinBox_scale,
"rotate": self.page.spinBox_rotate,
"color": self.page.spinBox_color,
"xPosition": self.page.spinBox_x,
"yPosition": self.page.spinBox_y,
"resizeMode": self.page.comboBox_resizeMode,
"mirror": self.page.checkBox_mirror,
"respondToAudio": self.page.checkBox_respondToAudio,
"sensitivity": self.page.spinBox_sensitivity,
"shadow": self.page.checkBox_shadow,
},
presetNames={
"imagePath": "image",
"xPosition": "x",
"yPosition": "y",
},
relativeWidgets=["xPosition", "yPosition", "scale"],
)
def update(self):
self.page.spinBox_sensitivity.setEnabled(
self.page.checkBox_respondToAudio.isChecked()
)
self.page.spinBox_scale.setEnabled(
self.page.comboBox_resizeMode.currentIndex() == 0
)
def previewRender(self):
return self.drawFrame(self.width, self.height, None)
def properties(self):
props = ["pcm" if self.respondToAudio else "static"]
if not os.path.exists(self.imagePath):
props.append("error")
return props
def error(self):
if not self.imagePath:
return "There is no image selected."
if not os.path.exists(self.imagePath):
return "The image selected does not exist!"
def preFrameRender(self, **kwargs):
super().preFrameRender(**kwargs)
if not self.respondToAudio:
return
# Trigger creation of new base image
self.existingImage = None
self.spectrumArray = createSpectrumArray(
self,
self.completeAudioArray,
self.sampleSize,
0.08,
0.8,
self.sensitivity,
self.progressBarUpdate,
self.progressBarSetText,
)
def frameRender(self, frameNo):
return self.drawFrame(
self.width,
self.height,
(
None
if not self.respondToAudio
else self.spectrumArray[frameNo * self.sampleSize]
),
)
def drawFrame(self, width, height, dynamicScale):
frame = BlankFrame(width, height)
if self.imagePath and os.path.exists(self.imagePath):
if dynamicScale is not None and self.existingImage:
image = self.existingImage
else:
image = Image.open(self.imagePath)
# Modify static image appearance
if self.color != 100:
image = ImageEnhance.Color(image).enhance(float(self.color / 100))
if self.mirror:
image = image.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
if self.resizeMode == 1: # Cover
image = ImageOps.fit(
image, (width, height), Image.Resampling.LANCZOS
)
elif self.resizeMode == 2: # Stretch
image = image.resize((width, height), Image.Resampling.LANCZOS)
elif self.scale != 100: # Scale
newHeight = int((image.height / 100) * self.scale)
newWidth = int((image.width / 100) * self.scale)
image = image.resize(
(newWidth, newHeight), Image.Resampling.LANCZOS
)
self.existingImage = image
# Respond to audio
resolutionFactor = height / 1080
shadX = int(resolutionFactor * 1)
shadY = int(resolutionFactor * -1)
shadBlur = resolutionFactor * 3.50
scale = 0
if dynamicScale is not None:
scale = dynamicScale[36 * 4] / 4
shadX += int((scale / 4) * resolutionFactor)
shadY += int((scale / 2) * resolutionFactor)
shadBlur += (scale / 8) * resolutionFactor
image = ImageOps.contain(
image,
(
image.width + int(scale / 2),
image.height + int(scale / 2),
),
Image.Resampling.LANCZOS,
)
# Paste image at correct position
frame.paste(
image,
box=(
self.xPosition - (0 if not self.respondToAudio else int(scale / 2)),
self.yPosition - (0 if not self.respondToAudio else int(scale / 2)),
),
)
if self.rotate != 0:
frame = frame.rotate(self.rotate)
if self.shadow:
frame = addShadow(frame, shadBlur, shadX, shadY)
return frame
def postFrameRender(self):
self.existingImage = None
def pickImage(self):
imgDir = self.settings.value("componentDir", os.path.expanduser("~"))
filename, _ = QtWidgets.QFileDialog.getOpenFileName(
self.page,
"Choose Image",
imgDir,
"Image Files (%s)" % " ".join(self.core.imageFormats),
)
if filename:
self.settings.setValue("componentDir", os.path.dirname(filename))
self.mergeUndo = False
self.page.lineEdit_image.setText(filename)
self.mergeUndo = True
def command(self, arg):
if "=" in arg:
key, arg = arg.split("=", 1)
if key == "path" and os.path.exists(arg):
try:
Image.open(arg)
self.page.lineEdit_image.setText(arg)
self.page.checkBox_stretch.setChecked(True)
return
except OSError as e:
print("Not a supported image format")
quit(1)
super().command(arg)
def commandHelp(self):
print("Load an image:\n path=/filepath/to/image.png")