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

141 lines
5.2 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 frame import BlankFrame
2017-06-06 11:14:39 -04:00
2017-07-02 20:46:48 -04:00
class Component(Component):
'''Image'''
modified = QtCore.pyqtSignal(int, dict)
def widget(self, parent):
self.parent = parent
2017-07-13 00:05:11 -04:00
self.settings = self.parent.core.settings
2017-06-24 23:12:41 -04:00
page = self.loadUi('image.ui')
2017-06-06 11:14:39 -04:00
2017-06-03 22:58:40 -04:00
page.lineEdit_image.textChanged.connect(self.update)
page.pushButton_image.clicked.connect(self.pickImage)
page.spinBox_scale.valueChanged.connect(self.update)
2017-07-05 23:04:09 -04:00
page.spinBox_rotate.valueChanged.connect(self.update)
page.spinBox_color.valueChanged.connect(self.update)
page.checkBox_stretch.stateChanged.connect(self.update)
page.checkBox_mirror.stateChanged.connect(self.update)
page.spinBox_x.valueChanged.connect(self.update)
page.spinBox_y.valueChanged.connect(self.update)
2017-06-06 11:14:39 -04:00
self.page = page
return page
def update(self):
2017-06-03 22:58:40 -04:00
self.imagePath = self.page.lineEdit_image.text()
self.scale = self.page.spinBox_scale.value()
2017-07-05 23:04:09 -04:00
self.rotate = self.page.spinBox_rotate.value()
self.color = self.page.spinBox_color.value()
self.xPosition = self.page.spinBox_x.value()
self.yPosition = self.page.spinBox_y.value()
self.stretched = self.page.checkBox_stretch.isChecked()
self.mirror = self.page.checkBox_mirror.isChecked()
self.parent.drawPreview()
super().update()
2017-06-06 11:14:39 -04:00
def previewRender(self, previewWorker):
2017-07-13 00:05:11 -04:00
width = int(self.settings.value('outputWidth'))
height = int(self.settings.value('outputHeight'))
return self.drawFrame(width, height)
def properties(self):
2017-07-11 06:06:22 -04:00
props = ['static']
2017-07-13 00:05:11 -04:00
if self.imagePath and not os.path.exists(self.imagePath):
2017-07-11 06:06:22 -04:00
props.append('error')
return props
def error(self):
if not os.path.exists(self.imagePath):
2017-07-13 00:05:11 -04:00
return "The image selected on " \
"layer %s does not exist!" % str(self.compPos)
def frameRender(self, layerNo, frameNo):
2017-07-13 00:05:11 -04:00
width = int(self.settings.value('outputWidth'))
height = int(self.settings.value('outputHeight'))
return self.drawFrame(width, 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):
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 self.scale != 100:
newHeight = int((image.height / 100) * self.scale)
newWidth = int((image.width / 100) * self.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
def loadPreset(self, pr, presetName=None):
super().loadPreset(pr, presetName)
2017-06-03 22:58:40 -04:00
self.page.lineEdit_image.setText(pr['image'])
self.page.spinBox_scale.setValue(pr['scale'])
self.page.spinBox_color.setValue(pr['color'])
2017-07-05 23:04:09 -04:00
self.page.spinBox_rotate.setValue(pr['rotate'])
self.page.spinBox_x.setValue(pr['x'])
self.page.spinBox_y.setValue(pr['y'])
self.page.checkBox_stretch.setChecked(pr['stretched'])
self.page.checkBox_mirror.setChecked(pr['mirror'])
2017-06-06 11:14:39 -04:00
def savePreset(self):
2017-06-03 22:58:40 -04:00
return {
'preset': self.currentPreset,
2017-06-06 11:14:39 -04:00
'image': self.imagePath,
'scale': self.scale,
'color': self.color,
2017-07-05 23:04:09 -04:00
'rotate': self.rotate,
'stretched': self.stretched,
'mirror': self.mirror,
'x': self.xPosition,
'y': self.yPosition,
2017-06-03 22:58:40 -04:00
}
2017-06-06 11:14:39 -04:00
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))
2017-06-03 22:58:40 -04:00
self.page.lineEdit_image.setText(filename)
self.update()
def command(self, arg):
if not arg.startswith('preset=') and '=' 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')