2017-07-06 12:40:03 -04:00
|
|
|
from PIL import Image, ImageDraw, ImageEnhance
|
2017-06-24 23:12:41 -04:00
|
|
|
from PyQt5 import QtGui, QtCore, QtWidgets
|
2017-06-03 20:39:32 -04:00
|
|
|
import os
|
|
|
|
|
2017-07-02 20:46:48 -04:00
|
|
|
from component import Component
|
2017-07-17 22:07:33 -04:00
|
|
|
from toolkit.frame import BlankFrame
|
2017-06-06 11:14:39 -04:00
|
|
|
|
2017-07-02 20:46:48 -04:00
|
|
|
|
|
|
|
class Component(Component):
|
2017-07-20 20:31:38 -04:00
|
|
|
name = 'Image'
|
|
|
|
version = '1.0.0'
|
2017-06-12 22:34:37 -04:00
|
|
|
|
2017-07-23 01:53:54 -04:00
|
|
|
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,
|
|
|
|
'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',
|
|
|
|
},
|
|
|
|
)
|
2017-06-06 11:14:39 -04:00
|
|
|
|
2017-07-27 17:49:08 -04:00
|
|
|
def previewRender(self):
|
|
|
|
return self.drawFrame(self.width, self.height)
|
2017-06-04 13:00:36 -04:00
|
|
|
|
2017-07-09 21:27:29 -04:00
|
|
|
def properties(self):
|
2017-07-11 06:06:22 -04:00
|
|
|
props = ['static']
|
2017-07-13 17:03:25 -04:00
|
|
|
if not os.path.exists(self.imagePath):
|
2017-07-11 06:06:22 -04:00
|
|
|
props.append('error')
|
|
|
|
return props
|
|
|
|
|
|
|
|
def error(self):
|
2017-07-13 17:03:25 -04:00
|
|
|
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):
|
2017-07-13 17:03:25 -04:00
|
|
|
return "The image selected does not exist!"
|
2017-06-04 13:00:36 -04:00
|
|
|
|
2017-07-27 17:49:08 -04:00
|
|
|
def frameRender(self, frameNo):
|
|
|
|
return self.drawFrame(self.width, self.height)
|
2017-06-06 11:14:39 -04:00
|
|
|
|
2017-06-03 20:39:32 -04:00
|
|
|
def drawFrame(self, width, height):
|
2017-07-02 21:38:19 -04:00
|
|
|
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)
|
2017-07-06 12:40:03 -04:00
|
|
|
|
|
|
|
# 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)
|
2017-06-14 19:37:47 -04:00
|
|
|
if self.stretched and image.size != (width, height):
|
2017-06-03 22:58:40 -04:00
|
|
|
image = image.resize((width, height), Image.ANTIALIAS)
|
2017-06-14 19:37:47 -04:00
|
|
|
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)
|
2017-07-06 12:40:03 -04:00
|
|
|
|
|
|
|
# Paste image at correct position
|
2017-06-14 19:37:47 -04:00
|
|
|
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-07-06 12:40:03 -04:00
|
|
|
|
2017-06-03 22:58:40 -04:00
|
|
|
return frame
|
2017-06-03 20:39:32 -04:00
|
|
|
|
2017-06-03 22:58:40 -04:00
|
|
|
def pickImage(self):
|
2017-06-25 14:27:56 -04:00
|
|
|
imgDir = self.settings.value("componentDir", os.path.expanduser("~"))
|
2017-06-23 23:00:24 -04:00
|
|
|
filename, _ = QtWidgets.QFileDialog.getOpenFileName(
|
2017-06-15 22:15:03 -04:00
|
|
|
self.page, "Choose Image", imgDir,
|
2017-07-23 01:53:54 -04:00
|
|
|
"Image Files (%s)" % " ".join(self.core.imageFormats))
|
2017-06-07 23:22:55 -04:00
|
|
|
if filename:
|
2017-06-25 14:27:56 -04:00
|
|
|
self.settings.setValue("componentDir", os.path.dirname(filename))
|
2017-06-03 22:58:40 -04:00
|
|
|
self.page.lineEdit_image.setText(filename)
|
|
|
|
self.update()
|
2017-06-22 18:40:34 -04:00
|
|
|
|
|
|
|
def command(self, arg):
|
2017-07-23 01:53:54 -04:00
|
|
|
if '=' in arg:
|
2017-06-22 22:23:04 -04:00
|
|
|
key, arg = arg.split('=', 1)
|
|
|
|
if key == 'path' and os.path.exists(arg):
|
2017-06-22 18:40:34 -04:00
|
|
|
try:
|
|
|
|
Image.open(arg)
|
2017-06-22 22:23:04 -04:00
|
|
|
self.page.lineEdit_image.setText(arg)
|
|
|
|
self.page.checkBox_stretch.setChecked(True)
|
|
|
|
return
|
2017-06-22 18:40:34 -04:00
|
|
|
except OSError as e:
|
|
|
|
print("Not a supported image format")
|
|
|
|
quit(1)
|
|
|
|
super().command(arg)
|
|
|
|
|
|
|
|
def commandHelp(self):
|
2017-06-22 22:23:04 -04:00
|
|
|
print('Load an image:\n path=/filepath/to/image.png')
|