From e32ba958cb95146728d4985221b08c7e01b35470 Mon Sep 17 00:00:00 2001 From: tassaron Date: Sat, 24 Jun 2017 23:12:41 -0400 Subject: [PATCH] fixing bugs --- src/components/__base__.py | 5 ++++- src/components/color.py | 7 +++---- src/components/image.py | 5 ++--- src/components/original.py | 5 ++--- src/components/text.py | 5 ++--- src/components/video.py | 27 +++++++++++++++++---------- src/core.py | 21 +++++++++++---------- src/presetmanager.py | 3 ++- src/preview_thread.py | 15 +++++++++++++-- 9 files changed, 56 insertions(+), 37 deletions(-) diff --git a/src/components/__base__.py b/src/components/__base__.py index 9b7b958..84d41c8 100644 --- a/src/components/__base__.py +++ b/src/components/__base__.py @@ -1,4 +1,4 @@ -from PyQt5 import QtGui, QtCore, QtWidgets +from PyQt5 import uic, QtGui, QtCore, QtWidgets from PIL import Image import os @@ -114,6 +114,9 @@ class Component(QtCore.QObject): except: return (255, 255, 255) + def loadUi(self, filename): + return uic.loadUi(os.path.join(self.core.componentsPath, filename)) + ''' ### Reference methods for creating a new component ### (Inherit from this class and define these) diff --git a/src/components/color.py b/src/components/color.py index f1fb2b2..253ac83 100644 --- a/src/components/color.py +++ b/src/components/color.py @@ -1,5 +1,5 @@ from PIL import Image, ImageDraw -from PyQt5 import uic, QtGui, QtCore, QtWidgets +from PyQt5 import QtGui, QtCore, QtWidgets from PyQt5.QtGui import QColor from PIL.ImageQt import ImageQt import os @@ -13,8 +13,7 @@ class Component(__base__.Component): def widget(self, parent): self.parent = parent - page = uic.loadUi(os.path.join( - os.path.dirname(os.path.realpath(__file__)), 'color.ui')) + page = self.loadUi('color.ui') self.color1 = (0, 0, 0) self.color2 = (133, 133, 133) @@ -177,7 +176,7 @@ class Component(__base__.Component): self.sizeWidth, self.sizeHeight ) painter.end() - imBytes = image.bits().asstring(image.numBytes()) + imBytes = image.bits().asstring(image.byteCount()) return Image.frombytes('RGBA', (width, height), imBytes) def loadPreset(self, pr, presetName=None): diff --git a/src/components/image.py b/src/components/image.py index 3517af6..143ae59 100644 --- a/src/components/image.py +++ b/src/components/image.py @@ -1,5 +1,5 @@ from PIL import Image, ImageDraw -from PyQt5 import uic, QtGui, QtCore, QtWidgets +from PyQt5 import QtGui, QtCore, QtWidgets import os from . import __base__ @@ -12,8 +12,7 @@ class Component(__base__.Component): def widget(self, parent): self.parent = parent self.settings = parent.settings - page = uic.loadUi(os.path.join( - os.path.dirname(os.path.realpath(__file__)), 'image.ui')) + page = self.loadUi('image.ui') self.imagePath = '' self.x = 0 self.y = 0 diff --git a/src/components/original.py b/src/components/original.py index 0d5001c..0185e0d 100644 --- a/src/components/original.py +++ b/src/components/original.py @@ -1,6 +1,6 @@ import numpy from PIL import Image, ImageDraw -from PyQt5 import uic, QtGui, QtCore, QtWidgets +from PyQt5 import QtGui, QtCore, QtWidgets from PyQt5.QtGui import QColor import os from . import __base__ @@ -17,8 +17,7 @@ class Component(__base__.Component): self.parent = parent self.visColor = (255, 255, 255) - page = uic.loadUi(os.path.join( - os.path.dirname(os.path.realpath(__file__)), 'original.ui')) + page = self.loadUi('original.ui') page.comboBox_visLayout.addItem("Classic") page.comboBox_visLayout.addItem("Split") page.comboBox_visLayout.addItem("Bottom") diff --git a/src/components/text.py b/src/components/text.py index 76961c9..7f4659f 100644 --- a/src/components/text.py +++ b/src/components/text.py @@ -1,6 +1,6 @@ from PIL import Image, ImageDraw from PyQt5.QtGui import QPainter, QColor, QFont -from PyQt5 import uic, QtGui, QtCore, QtWidgets +from PyQt5 import QtGui, QtCore, QtWidgets from PIL.ImageQt import ImageQt import os import io @@ -29,8 +29,7 @@ class Component(__base__.Component): self.xPosition = width / 2 - fm.width(self.title)/2 self.yPosition = height / 2 * 1.036 - page = uic.loadUi(os.path.join( - os.path.dirname(os.path.realpath(__file__)), 'text.ui')) + page = self.loadUi('text.ui') page.comboBox_textAlign.addItem("Left") page.comboBox_textAlign.addItem("Middle") page.comboBox_textAlign.addItem("Right") diff --git a/src/components/video.py b/src/components/video.py index 70247e1..3e87d2e 100644 --- a/src/components/video.py +++ b/src/components/video.py @@ -1,6 +1,7 @@ from PIL import Image, ImageDraw -from PyQt5 import uic, QtGui, QtCore, QtWidgets +from PyQt5 import QtGui, QtCore, QtWidgets import os +import math import subprocess import threading from queue import PriorityQueue @@ -79,9 +80,18 @@ class Video: self.frameNo += 1 # If we run out of frames, use the last good frame and loop. - if len(self.currentFrame) == 0: - self.frameBuffer.put((self.frameNo-1, self.lastFrame)) - continue + try: + if len(self.currentFrame) == 0: + self.frameBuffer.put((self.frameNo-1, self.lastFrame)) + continue + except AttributeError as e: + self.parent.showMessage( + msg='%s couldn\'t be loaded.' % os.path.basename( + self.videoPath + ), + detail=str(e) + ) + self.parent.stopVideo() self.currentFrame = pipe.stdout.read(self.chunkSize) if len(self.currentFrame) != 0: @@ -97,10 +107,7 @@ class Component(__base__.Component): def widget(self, parent): self.parent = parent self.settings = parent.settings - page = uic.loadUi(os.path.join( - os.path.dirname(os.path.realpath(__file__)), - 'video.ui' - )) + page = self.loadUi('video.ui') self.videoPath = '' self.x = 0 self.y = 0 @@ -243,9 +250,9 @@ def scale(scale, width, height, returntype=None): width = (float(width) / 100.0) * float(scale) height = (float(height) / 100.0) * float(scale) if returntype == str: - return (str(int(width)), str(int(height))) + return (str(math.ceil(width)), str(math.ceil(height))) elif returntype == int: - return (int(width), int(height)) + return (math.ceil(width), math.ceil(height)) else: return (width, height) diff --git a/src/core.py b/src/core.py index c80d60e..89c1e86 100644 --- a/src/core.py +++ b/src/core.py @@ -29,6 +29,7 @@ class Core(): else: # unfrozen self.wd = os.path.dirname(os.path.realpath(__file__)) + self.componentsPath = os.path.join(self.wd, 'components') self.loadEncoderOptions() self.videoFormats = Core.appendUppercase([ @@ -66,14 +67,12 @@ class Core(): def findComponents(self): def findComponents(): - srcPath = os.path.join(self.wd, 'components') - if os.path.exists(srcPath): - for f in sorted(os.listdir(srcPath)): - name, ext = os.path.splitext(f) - if name.startswith("__"): - continue - elif ext == '.py': - yield name + for f in sorted(os.listdir(self.componentsPath)): + name, ext = os.path.splitext(f) + if name.startswith("__"): + continue + elif ext == '.py': + yield name self.modules = [ import_module('components.%s' % name) for name in findComponents() @@ -93,10 +92,12 @@ class Core(): return None component = self.modules[moduleIndex].Component( - moduleIndex, compPos, self) + moduleIndex, compPos, self + ) self.selectedComponents.insert( compPos, - component) + component + ) self.componentListChanged() # init component's widget for loading/saving presets diff --git a/src/presetmanager.py b/src/presetmanager.py index 44203e5..069bf62 100644 --- a/src/presetmanager.py +++ b/src/presetmanager.py @@ -123,7 +123,8 @@ class PresetManager(QtWidgets.QDialog): def clearPreset(self, compI=None): '''Functions on mainwindow level from the context menu''' compI = self.parent.window.listWidget_componentList.currentRow() - self.core.clearPreset(compI, self.parent) + self.core.clearPreset(compI) + self.parent.updateComponentTitle(compI, False) def openSavePresetDialog(self): '''Functions on mainwindow level from the context menu''' diff --git a/src/preview_thread.py b/src/preview_thread.py index 4a46d51..ac5751d 100644 --- a/src/preview_thread.py +++ b/src/preview_thread.py @@ -49,8 +49,19 @@ class Worker(QtCore.QObject): components = nextPreviewInformation["components"] for component in reversed(components): - frame = Image.alpha_composite( - frame, component.previewRender(self)) + try: + newFrame = component.previewRender(self) + frame = Image.alpha_composite( + frame, newFrame) + except ValueError: + self.parent.showMessage( + msg="Bad frame returned by %s's previewRender method. " + "This is a fatal error." % + str(component), + detail="bad frame: w%s, h%s" % ( + newFrame.width, newFrame.height) + ) + quit(1) self._image = ImageQt(frame) self.imageCreated.emit(QtGui.QImage(self._image))