This repository has been archived on 2020-08-22. You can view files and clone it, but cannot push or open issues or pull requests.
pyaudviz/src/components/image.py

127 lines
4.5 KiB
Python
Raw Normal View History

from PIL import Image, ImageDraw, ImageEnhance
2017-06-24 23:12:41 -04:00
from PyQt5 import QtGui, QtCore, QtWidgets
import os
2017-07-02 20:46:48 -04:00
from component import Component
from toolkit.frame import BlankFrame
2017-06-06 11:14:39 -04:00
2017-07-02 20:46:48 -04:00
class Component(Component):
name = 'Image'
version = '1.0.1'
def widget(self, *args):
super().widget(*args)
self.page.pushButton_image.clicked.connect(self.pickImage)
self.trackWidgets({
'imagePath': self.page.lineEdit_image,
'scale': self.page.spinBox_scale,
'stretchScale': self.page.spinBox_scale_stretch,
'rotate': self.page.spinBox_rotate,
'color': self.page.spinBox_color,
'xPosition': self.page.spinBox_x,
'yPosition': self.page.spinBox_y,
'stretched': self.page.checkBox_stretch,
'mirror': self.page.checkBox_mirror,
}, presetNames={
'imagePath': 'image',
'xPosition': 'x',
'yPosition': 'y',
}, relativeWidgets=[
2017-08-03 20:50:22 -04:00
'xPosition', 'yPosition', 'scale'
])
2017-06-06 11:14:39 -04:00
def previewRender(self):
return self.drawFrame(self.width, self.height)
def properties(self):
2017-07-11 06:06:22 -04:00
props = ['static']
if not os.path.exists(self.imagePath):
2017-07-11 06:06:22 -04:00
props.append('error')
return props
def error(self):
if not self.imagePath:
return "There is no image selected."
2017-07-11 06:06:22 -04:00
if not os.path.exists(self.imagePath):
return "The image selected does not exist!"
def frameRender(self, frameNo):
return self.drawFrame(self.width, self.height)
2017-06-06 11:14:39 -04:00
def drawFrame(self, width, height):
frame = BlankFrame(width, height)
2017-06-03 22:58:40 -04:00
if self.imagePath and os.path.exists(self.imagePath):
scale = self.scale if not self.stretched else self.stretchScale
2017-06-03 22:58:40 -04:00
image = Image.open(self.imagePath)
# Modify image's appearance
if self.color != 100:
image = ImageEnhance.Color(image).enhance(
float(self.color / 100)
)
if self.mirror:
image = image.transpose(Image.FLIP_LEFT_RIGHT)
if self.stretched and image.size != (width, height):
2017-06-03 22:58:40 -04:00
image = image.resize((width, height), Image.ANTIALIAS)
if scale != 100:
newHeight = int((image.height / 100) * scale)
newWidth = int((image.width / 100) * scale)
image = image.resize((newWidth, newHeight), Image.ANTIALIAS)
# Paste image at correct position
frame.paste(image, box=(self.xPosition, self.yPosition))
2017-07-05 23:04:09 -04:00
if self.rotate != 0:
frame = frame.rotate(self.rotate)
2017-06-03 22:58:40 -04:00
return frame
2017-06-03 22:58:40 -04:00
def pickImage(self):
imgDir = self.settings.value("componentDir", os.path.expanduser("~"))
2017-06-23 23:00:24 -04:00
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
2017-06-03 22:58:40 -04:00
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')
def savePreset(self):
# Maintain the illusion that the scale spinbox is one widget
scaleBox = self.page.spinBox_scale
stretchScaleBox = self.page.spinBox_scale_stretch
if self.page.checkBox_stretch.isChecked():
scaleBox.setValue(stretchScaleBox.value())
else:
stretchScaleBox.setValue(scaleBox.value())
return super().savePreset()
def update(self):
# Maintain the illusion that the scale spinbox is one widget
scaleBox = self.page.spinBox_scale
stretchScaleBox = self.page.spinBox_scale_stretch
if self.page.checkBox_stretch.isChecked():
scaleBox.setVisible(False)
stretchScaleBox.setVisible(True)
else:
scaleBox.setVisible(True)
stretchScaleBox.setVisible(False)