Prześlij pliki do '02/do_sprawdzenia'
This commit is contained in:
parent
2cfe9fd539
commit
114169a365
829
02/do_sprawdzenia/filters.py
Normal file
829
02/do_sprawdzenia/filters.py
Normal file
@ -0,0 +1,829 @@
|
||||
from PyQt5 import QtCore, QtGui, QtWidgets
|
||||
import numpy as np
|
||||
import math
|
||||
|
||||
#from imageio import imread
|
||||
|
||||
|
||||
class Ui_MainWindow(object):
|
||||
def setupUi(self, MainWindow):
|
||||
|
||||
MainWindow.setObjectName("MainWindow")
|
||||
MainWindow.resize(1032, 847)
|
||||
self.centralwidget = QtWidgets.QWidget(MainWindow)
|
||||
self.centralwidget.setObjectName("centralwidget")
|
||||
self.image = QtWidgets.QLabel(self.centralwidget)
|
||||
self.image.setGeometry(QtCore.QRect(0, 0, MainWindow.width(), MainWindow.height()))
|
||||
self.image.setText("")
|
||||
self.image.setScaledContents(False)
|
||||
self.image.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
|
||||
self.image.setWordWrap(False)
|
||||
self.image.setObjectName("image")
|
||||
MainWindow.setCentralWidget(self.centralwidget)
|
||||
|
||||
self.menubar = QtWidgets.QMenuBar(MainWindow)
|
||||
self.menubar.setGeometry(QtCore.QRect(0, 0, 1032, 22))
|
||||
self.menubar.setObjectName("menubar")
|
||||
|
||||
self.menuFile = QtWidgets.QMenu(self.menubar)
|
||||
self.menuFile.setObjectName("menuFile")
|
||||
|
||||
self.menuFilters = QtWidgets.QMenu(self.menubar)
|
||||
self.menuFilters.setObjectName("menuFilters")
|
||||
|
||||
self.menuDesaturation = QtWidgets.QMenu(self.menubar)
|
||||
self.menuDesaturation.setObjectName("menuDesaturation")
|
||||
|
||||
self.menuContrast = QtWidgets.QMenu(self.menubar)
|
||||
self.menuContrast.setObjectName("menuContrast")
|
||||
|
||||
self.menuBrightness = QtWidgets.QMenu(self.menubar)
|
||||
self.menuBrightness.setObjectName("menuBrightness")
|
||||
|
||||
self.menuBrightnessUp = QtWidgets.QMenu(self.menubar)
|
||||
self.menuBrightnessUp.setObjectName("menuBrightnessUp")
|
||||
|
||||
self.menuBrightnessDw = QtWidgets.QMenu(self.menubar)
|
||||
self.menuBrightnessDw.setObjectName("menuBrightnessDw")
|
||||
|
||||
self.menuSaturation = QtWidgets.QMenu(self.menubar)
|
||||
self.menuSaturation.setObjectName("menuSaturation")
|
||||
|
||||
self.menuSaturationUp = QtWidgets.QMenu(self.menubar)
|
||||
self.menuSaturationUp.setObjectName("menuSaturationUp")
|
||||
|
||||
self.menuSaturationDw = QtWidgets.QMenu(self.menubar)
|
||||
self.menuSaturationDw.setObjectName("menuSaturationDw")
|
||||
|
||||
self.menuSDP = QtWidgets.QMenu(self.menubar)
|
||||
self.menuSDP.setObjectName("menuSDP")
|
||||
|
||||
self.menuMono = QtWidgets.QMenu(self.menubar)
|
||||
self.menuMono.setObjectName("menuMono")
|
||||
|
||||
MainWindow.setMenuBar(self.menubar)
|
||||
|
||||
self.statusbar = QtWidgets.QStatusBar(MainWindow)
|
||||
self.statusbar.setObjectName("statusbar")
|
||||
MainWindow.setStatusBar(self.statusbar)
|
||||
|
||||
self.actionOpen = QtWidgets.QAction(MainWindow)
|
||||
self.actionOpen.setObjectName("actionOpen")
|
||||
|
||||
self.actionNegative = QtWidgets.QAction(MainWindow)
|
||||
self.actionNegative.setObjectName("actionNegative")
|
||||
|
||||
self.actionDesatHSV = QtWidgets.QAction(MainWindow)
|
||||
self.actionDesatHSV.setObjectName("actionDesatHSV")
|
||||
|
||||
self.actionDesatSimplified = QtWidgets.QAction(MainWindow)
|
||||
self.actionDesatSimplified.setObjectName("actionDesatSimplified")
|
||||
|
||||
self.actionDesatCCIR = QtWidgets.QAction(MainWindow)
|
||||
self.actionDesatCCIR.setObjectName("actionDesatCCIR")
|
||||
|
||||
|
||||
self.actionBrightnessUp25 = QtWidgets.QAction(MainWindow)
|
||||
self.actionBrightnessUp25.setObjectName("actionBrightnessUp25")
|
||||
self.actionBrightnessUp50 = QtWidgets.QAction(MainWindow)
|
||||
self.actionBrightnessUp50.setObjectName("actionBrightnessUp50")
|
||||
self.actionBrightnessUp75 = QtWidgets.QAction(MainWindow)
|
||||
self.actionBrightnessUp75.setObjectName("actionBrightnessUp75")
|
||||
|
||||
self.actionBrightnessDw25 = QtWidgets.QAction(MainWindow)
|
||||
self.actionBrightnessDw25.setObjectName("actionBrightnessDw25")
|
||||
self.actionBrightnessDw50 = QtWidgets.QAction(MainWindow)
|
||||
self.actionBrightnessDw50.setObjectName("actionBrightnessDw50")
|
||||
self.actionBrightnessDw75 = QtWidgets.QAction(MainWindow)
|
||||
self.actionBrightnessDw75.setObjectName("actionBrightnessDw75")
|
||||
|
||||
self.actionConLinear = QtWidgets.QAction(MainWindow)
|
||||
self.actionConLinear.setObjectName("actionConLinear")
|
||||
self.actionConLog = QtWidgets.QAction(MainWindow)
|
||||
self.actionConLog.setObjectName("actionConLog")
|
||||
self.actionConPow = QtWidgets.QAction(MainWindow)
|
||||
self.actionConPow.setObjectName("actionConPowr")
|
||||
|
||||
self.actionSaturationUp25 = QtWidgets.QAction(MainWindow)
|
||||
self.actionSaturationUp25.setObjectName("actionSaturationUp25")
|
||||
self.actionSaturationUp50 = QtWidgets.QAction(MainWindow)
|
||||
self.actionSaturationUp50.setObjectName("actionSaturationUp50")
|
||||
self.actionSaturationUp75 = QtWidgets.QAction(MainWindow)
|
||||
self.actionSaturationUp75.setObjectName("actionSaturationUp75")
|
||||
|
||||
self.actionSaturationDw25 = QtWidgets.QAction(MainWindow)
|
||||
self.actionSaturationDw25.setObjectName("actionSaturationDw25")
|
||||
self.actionSaturationDw50 = QtWidgets.QAction(MainWindow)
|
||||
self.actionSaturationDw50.setObjectName("actionSaturationDw50")
|
||||
self.actionSaturationDw75 = QtWidgets.QAction(MainWindow)
|
||||
self.actionSaturationDw75.setObjectName("actionSaturationDw75")
|
||||
|
||||
self.actionSum = QtWidgets.QAction(MainWindow)
|
||||
self.actionSum.setObjectName("actionSum")
|
||||
self.actionDiff = QtWidgets.QAction(MainWindow)
|
||||
self.actionDiff.setObjectName("actionDiff")
|
||||
self.actionProduct = QtWidgets.QAction(MainWindow)
|
||||
self.actionProduct.setObjectName("actionProduct")
|
||||
|
||||
self.actionSchodki = QtWidgets.QAction(MainWindow)
|
||||
self.actionSchodki.setObjectName("actionSchodki")
|
||||
|
||||
|
||||
self.menuFile.addAction(self.actionOpen)
|
||||
|
||||
self.menuFilters.addAction(self.actionNegative)
|
||||
self.menuDesaturation.addAction(self.actionDesatHSV)
|
||||
self.menuDesaturation.addAction(self.actionDesatSimplified)
|
||||
self.menuDesaturation.addAction(self.actionDesatCCIR)
|
||||
|
||||
|
||||
self.menuBrightnessUp.addAction(self.actionBrightnessUp25)
|
||||
self.menuBrightnessUp.addAction(self.actionBrightnessUp50)
|
||||
self.menuBrightnessUp.addAction(self.actionBrightnessUp75)
|
||||
self.menuBrightnessDw.addAction(self.actionBrightnessDw25)
|
||||
self.menuBrightnessDw.addAction(self.actionBrightnessDw50)
|
||||
self.menuBrightnessDw.addAction(self.actionBrightnessDw75)
|
||||
|
||||
self.menuContrast.addAction(self.actionConLinear)
|
||||
self.menuContrast.addAction(self.actionConLog)
|
||||
self.menuContrast.addAction(self.actionConPow)
|
||||
|
||||
self.menuSaturationUp.addAction(self.actionSaturationUp25)
|
||||
self.menuSaturationUp.addAction(self.actionSaturationUp50)
|
||||
self.menuSaturationUp.addAction(self.actionSaturationUp75)
|
||||
self.menuSaturationDw.addAction(self.actionSaturationDw25)
|
||||
self.menuSaturationDw.addAction(self.actionSaturationDw50)
|
||||
self.menuSaturationDw.addAction(self.actionSaturationDw75)
|
||||
|
||||
|
||||
self.menubar.addAction(self.menuFile.menuAction())
|
||||
self.menubar.addAction(self.menuFilters.menuAction())
|
||||
|
||||
self.menuFilters.addAction(self.menuDesaturation.menuAction())
|
||||
self.menuFilters.addAction(self.menuBrightness.menuAction())
|
||||
self.menuBrightness.addAction(self.menuBrightnessUp.menuAction())
|
||||
self.menuBrightness.addAction(self.menuBrightnessDw.menuAction())
|
||||
|
||||
self.menuFilters.addAction(self.menuSaturation.menuAction())
|
||||
self.menuFilters.addAction(self.menuContrast.menuAction())
|
||||
self.menuSaturation.addAction(self.menuSaturationUp.menuAction())
|
||||
self.menuSaturation.addAction(self.menuSaturationDw.menuAction())
|
||||
|
||||
self.menubar.addAction(self.menuSDP.menuAction())
|
||||
self.menuSDP.addAction(self.actionSum)
|
||||
self.menuSDP.addAction(self.actionDiff)
|
||||
self.menuSDP.addAction(self.actionProduct)
|
||||
|
||||
self.menubar.addAction(self.menuMono.menuAction())
|
||||
self.menuMono.addAction(self.actionSchodki)
|
||||
|
||||
self.retranslateUi(MainWindow)
|
||||
QtCore.QMetaObject.connectSlotsByName(MainWindow)
|
||||
|
||||
self.actionOpen.triggered.connect(lambda: self.loadImageFromFile())
|
||||
self.actionNegative.triggered.connect(lambda: self.negatyw())
|
||||
self.actionDesatHSV.triggered.connect(lambda: self.desaturation('hsv'))
|
||||
self.actionDesatSimplified.triggered.connect(lambda: self.desaturation('simplified'))
|
||||
self.actionDesatCCIR.triggered.connect(lambda: self.desaturation('CCIR'))
|
||||
|
||||
self.actionConLinear.triggered.connect(lambda: self.contrast("Linear"))
|
||||
self.actionConLog.triggered.connect(lambda: self.contrast("Logarithmic"))
|
||||
self.actionConPow.triggered.connect(lambda: self.contrast("Pow"))
|
||||
|
||||
|
||||
self.actionBrightnessUp25.triggered.connect(lambda: self.brightness('up', 25))
|
||||
self.actionBrightnessUp50.triggered.connect(lambda: self.brightness('up', 50))
|
||||
self.actionBrightnessUp75.triggered.connect(lambda: self.brightness('up', 75))
|
||||
self.actionBrightnessDw25.triggered.connect(lambda: self.brightness('dw', 25))
|
||||
self.actionBrightnessDw50.triggered.connect(lambda: self.brightness('dw', 50))
|
||||
self.actionBrightnessDw75.triggered.connect(lambda: self.brightness('dw', 75))
|
||||
|
||||
self.actionSaturationUp25.triggered.connect(lambda: self.saturation(25))
|
||||
self.actionSaturationUp50.triggered.connect(lambda: self.saturation(50))
|
||||
self.actionSaturationUp75.triggered.connect(lambda: self.saturation(75))
|
||||
self.actionSaturationDw25.triggered.connect(lambda: self.saturation(-25))
|
||||
self.actionSaturationDw50.triggered.connect(lambda: self.saturation(-50))
|
||||
self.actionSaturationDw75.triggered.connect(lambda: self.saturation(-75))
|
||||
|
||||
self.actionSum.triggered.connect(lambda: self.sdp('sum'))
|
||||
self.actionDiff.triggered.connect(lambda: self.sdp('diff'))
|
||||
self.actionProduct.triggered.connect(lambda: self.sdp('product'))
|
||||
|
||||
self.actionSchodki.triggered.connect(lambda: self.mono('schodki'))
|
||||
|
||||
|
||||
def retranslateUi(self, MainWindow):
|
||||
_translate = QtCore.QCoreApplication.translate
|
||||
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
|
||||
self.menuFile.setTitle(_translate("MainWindow", "File"))
|
||||
self.menuFilters.setTitle(_translate("MainWindow", "Filters"))
|
||||
self.actionOpen.setText(_translate("MainWindow", "Open ..."))
|
||||
self.actionNegative.setText(_translate("MainWindow", "Negative"))
|
||||
|
||||
self.menuDesaturation.setTitle(_translate("MainWindow", "Desaturation"))
|
||||
self.actionDesatHSV.setText(_translate("MainWindow", "Based on HSV"))
|
||||
self.actionDesatSimplified.setText(_translate("MainWindow", "Simplified"))
|
||||
self.actionDesatCCIR.setText(_translate("MainWindow", "CCIR 601"))
|
||||
|
||||
self.menuContrast.setTitle(_translate("MainWindow", "Contrast"))
|
||||
self.actionConLinear.setText(_translate("MainWindow", "Linear"))
|
||||
self.actionConLog.setText(_translate("MainWindow", "Logarithmic"))
|
||||
self.actionConPow.setText(_translate("MainWindow", "Pow"))
|
||||
|
||||
self.menuBrightness.setTitle(_translate("MainWindow", "Brightness"))
|
||||
self.menuBrightnessUp.setTitle(_translate("MainWindow", "Up"))
|
||||
self.actionBrightnessUp25.setText(_translate("MainWindow", "25"))
|
||||
self.actionBrightnessUp50.setText(_translate("MainWindow", "50"))
|
||||
self.actionBrightnessUp75.setText(_translate("MainWindow", "75"))
|
||||
self.menuBrightnessDw.setTitle(_translate("MainWindow", "Down"))
|
||||
self.actionBrightnessDw25.setText(_translate("MainWindow", "25"))
|
||||
self.actionBrightnessDw50.setText(_translate("MainWindow", "50"))
|
||||
self.actionBrightnessDw75.setText(_translate("MainWindow", "75"))
|
||||
|
||||
self.menuSaturation.setTitle(_translate("MainWindow", "Saturation"))
|
||||
self.menuSaturationUp.setTitle(_translate("MainWindow", "Up"))
|
||||
self.actionSaturationUp25.setText(_translate("MainWindow", "25"))
|
||||
self.actionSaturationUp50.setText(_translate("MainWindow", "50"))
|
||||
self.actionSaturationUp75.setText(_translate("MainWindow", "75"))
|
||||
self.menuSaturationDw.setTitle(_translate("MainWindow", "Down"))
|
||||
self.actionSaturationDw25.setText(_translate("MainWindow", "25"))
|
||||
self.actionSaturationDw50.setText(_translate("MainWindow", "50"))
|
||||
self.actionSaturationDw75.setText(_translate("MainWindow", "75"))
|
||||
|
||||
self.menuSDP.setTitle(_translate("MainWindow", r"+/-/*"))
|
||||
self.actionSum.setText(_translate("MainWindow", "SUM"))
|
||||
self.actionDiff.setText(_translate("MainWindow", "DIFFERENCE"))
|
||||
self.actionProduct.setText(_translate("MainWindow", "PRODUCT"))
|
||||
|
||||
self.menuMono.setTitle(_translate("MainWindow", "MONO"))
|
||||
self.actionSchodki.setText(_translate("MainWindow", "schodki"))
|
||||
|
||||
|
||||
def loadImageFromFile(self):
|
||||
fname = QtWidgets.QFileDialog.getOpenFileName(self.centralwidget, 'Open file', './', "Image files (*.pgm *.pbm *.ppm)")
|
||||
reader = QtGui.QImageReader(fname[0])
|
||||
reader.setAutoTransform(True)
|
||||
new_image = reader.read()
|
||||
|
||||
self.width = new_image.width()
|
||||
self.height = new_image.height()
|
||||
self.nb_chanels = int(new_image.depth() / 8) if new_image.depth() > 1 else 1
|
||||
self.nb_real_channels = int(new_image.bitPlaneCount() / 8) if new_image.bitPlaneCount() > 1 else 1
|
||||
self.img_format = new_image.format()
|
||||
self.image_object = new_image
|
||||
self.data = []
|
||||
|
||||
for i in range(self.height):
|
||||
row = []
|
||||
|
||||
for j in range(self.width):
|
||||
impix = new_image.pixel(j, i)
|
||||
pixel = []
|
||||
|
||||
if self.nb_chanels == 1:
|
||||
pixel = [QtGui.qRed(impix)]
|
||||
|
||||
elif self.nb_chanels == 3:
|
||||
pixel = [QtGui.qRed(impix), QtGui.qGreen(impix), QtGui.qBlue(impix)]
|
||||
|
||||
elif self.nb_chanels == 4:
|
||||
pixel = [QtGui.qRed(impix), QtGui.qGreen(impix), QtGui.qBlue(impix), QtGui.qAlpha(impix)]
|
||||
|
||||
row.append(pixel)
|
||||
|
||||
self.data.append(row)
|
||||
|
||||
self.pixmap = QtGui.QPixmap.fromImage(new_image)
|
||||
self.loadImage()
|
||||
|
||||
def loadImage(self):
|
||||
self.image.setPixmap(self.pixmap)
|
||||
|
||||
def negatyw(self):
|
||||
data_result = []
|
||||
|
||||
for i in range(self.height):
|
||||
row = []
|
||||
for j in range(self.width):
|
||||
pixel = []
|
||||
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(255 - self.data[i][j][k])
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
row.append(pixel)
|
||||
|
||||
data_result.append(row)
|
||||
|
||||
image_result = QtGui.QImage(self.width, self.height, self.img_format)
|
||||
|
||||
if self.nb_chanels > 1:
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
image_result.setPixel(j, i, QtGui.qRgba(data_result[i][j][0],
|
||||
data_result[i][j][1],
|
||||
data_result[i][j][2],
|
||||
data_result[i][j][3]))
|
||||
elif self.image_object.bitPlaneCount() == 8:
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
image_result.setPixel(j, i, QtGui.qRgba(data_result[i][j][0],
|
||||
data_result[i][j][0],
|
||||
data_result[i][j][0],
|
||||
255))
|
||||
else:
|
||||
image_result.setColorCount(2)
|
||||
image_result.setColor(0, QtGui.qRgba(0, 0, 0, 255))
|
||||
image_result.setColor(1, QtGui.qRgba(255, 0, 0, 0))
|
||||
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
if data_result[i][j][0] == 0:
|
||||
image_result.setPixel(j, i, 0)
|
||||
else:
|
||||
image_result.setPixel(j, i, 1)
|
||||
|
||||
self.pixmap = QtGui.QPixmap.fromImage(image_result)
|
||||
self.image.setPixmap(self.pixmap)
|
||||
|
||||
self.data = data_result
|
||||
|
||||
def desaturation(self, standard):
|
||||
if self.nb_chanels > 1:
|
||||
data_result = []
|
||||
|
||||
for i in range(self.height):
|
||||
row = []
|
||||
for j in range(self.width):
|
||||
pixel = []
|
||||
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(self.data[i][j][k])
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
row.append(pixel)
|
||||
|
||||
data_result.append(row)
|
||||
|
||||
image_result = QtGui.QImage(self.width, self.height, self.img_format)
|
||||
|
||||
if standard == 'hsv':
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
color = QtGui.QColor(data_result[i][j][0],
|
||||
data_result[i][j][1],
|
||||
data_result[i][j][2],
|
||||
data_result[i][j][3])
|
||||
|
||||
|
||||
color.convertTo(2)
|
||||
color.setHsv(color.hue(), 0, color.value())
|
||||
|
||||
image_result.setPixelColor(j, i, color)
|
||||
elif standard == 'simplified':
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
R = data_result[i][j][0]
|
||||
G = data_result[i][j][1]
|
||||
B = data_result[i][j][2]
|
||||
|
||||
gray = (max(R,G,B) + min(R,G,B))/3
|
||||
|
||||
image_result.setPixel(j, i, QtGui.qRgba(gray,
|
||||
gray,
|
||||
gray,
|
||||
data_result[i][j][3]))
|
||||
|
||||
else: #CCIR
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
R = data_result[i][j][0]
|
||||
G = data_result[i][j][1]
|
||||
B = data_result[i][j][2]
|
||||
|
||||
gray = 0.299 * R + 0.587 * G + 0.114 * B
|
||||
|
||||
image_result.setPixel(j, i, QtGui.qRgba(gray,
|
||||
gray,
|
||||
gray,
|
||||
data_result[i][j][3]))
|
||||
|
||||
|
||||
self.pixmap = QtGui.QPixmap.fromImage(image_result)
|
||||
self.image.setPixmap(self.pixmap)
|
||||
self.data = data_result
|
||||
|
||||
def brightness(self, way, value):
|
||||
data_result = []
|
||||
|
||||
if way == 'up':
|
||||
for i in range(self.height):
|
||||
row = []
|
||||
for j in range(self.width):
|
||||
pixel = []
|
||||
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(min(self.data[i][j][k] + value, 255))
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
row.append(pixel)
|
||||
|
||||
data_result.append(row)
|
||||
|
||||
image_result = QtGui.QImage(self.width, self.height, self.img_format)
|
||||
|
||||
else:
|
||||
for i in range(self.height):
|
||||
row = []
|
||||
for j in range(self.width):
|
||||
pixel = []
|
||||
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(max(0, self.data[i][j][k] - value))
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
row.append(pixel)
|
||||
|
||||
data_result.append(row)
|
||||
|
||||
image_result = QtGui.QImage(self.width, self.height, self.img_format)
|
||||
|
||||
|
||||
if self.nb_chanels > 1:
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
image_result.setPixel(j, i, QtGui.qRgba(data_result[i][j][0],
|
||||
data_result[i][j][1],
|
||||
data_result[i][j][2],
|
||||
data_result[i][j][3]))
|
||||
elif self.image_object.bitPlaneCount() == 8:
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
image_result.setPixel(j, i, QtGui.qRgba(data_result[i][j][0],
|
||||
data_result[i][j][0],
|
||||
data_result[i][j][0],
|
||||
255))
|
||||
else:
|
||||
image_result.setColorCount(2)
|
||||
image_result.setColor(0, QtGui.qRgba(0, 0, 0, 255))
|
||||
image_result.setColor(1, QtGui.qRgba(255, 0, 0, 0))
|
||||
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
if data_result[i][j][0] == 0:
|
||||
image_result.setPixel(j, i, 0)
|
||||
else:
|
||||
image_result.setPixel(j, i, 1)
|
||||
|
||||
self.pixmap = QtGui.QPixmap.fromImage(image_result)
|
||||
self.image.setPixmap(self.pixmap)
|
||||
self.data = data_result
|
||||
|
||||
def sdp(self, op):
|
||||
|
||||
#wpierw musimy wczytać nowy obrazek
|
||||
fname = QtWidgets.QFileDialog.getOpenFileName(self.centralwidget, 'Open file', './', "Image files (*.pgm *.pbm *.ppm)")
|
||||
reader = QtGui.QImageReader(fname[0])
|
||||
reader.setAutoTransform(True)
|
||||
imageB = reader.read()
|
||||
|
||||
width = imageB.width()
|
||||
height = imageB.height()
|
||||
nb_chanels = int(imageB.depth() / 8) if imageB.depth() > 1 else 1
|
||||
nb_real_channels = int(imageB.bitPlaneCount() / 8) if imageB.bitPlaneCount() > 1 else 1
|
||||
img_format = imageB.format()
|
||||
image_object = imageB
|
||||
dataB = []
|
||||
|
||||
for i in range(height):
|
||||
row = []
|
||||
|
||||
for j in range(width):
|
||||
impix = imageB.pixel(j, i)
|
||||
pixel = []
|
||||
|
||||
if nb_chanels == 1:
|
||||
pixel = [QtGui.qRed(impix)]
|
||||
|
||||
elif nb_chanels == 3:
|
||||
pixel = [QtGui.qRed(impix), QtGui.qGreen(impix), QtGui.qBlue(impix)]
|
||||
|
||||
elif nb_chanels == 4:
|
||||
pixel = [QtGui.qRed(impix), QtGui.qGreen(impix), QtGui.qBlue(impix), QtGui.qAlpha(impix)]
|
||||
|
||||
row.append(pixel)
|
||||
|
||||
dataB.append(row)
|
||||
|
||||
# pixmap = QtGui.QPixmap.fromImage(imageB)
|
||||
|
||||
if op == 'sum':
|
||||
data_result= []
|
||||
|
||||
for i in range(max(height, self.height)):
|
||||
row = []
|
||||
for j in range(max(width, self.width)):
|
||||
pixel = []
|
||||
|
||||
|
||||
if i < min(height, self.height) and j < min(width, self.width):
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append((self.data[i][j][k] + dataB[i][j][k])/2)
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
elif i < self.height and j < self.width:
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(self.data[i][j][k])
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
elif i < height and j < width:
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(dataB[i][j][k])
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
else:
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(0)
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
|
||||
row.append(pixel)
|
||||
|
||||
data_result.append(row)
|
||||
|
||||
image_result = QtGui.QImage(self.width, self.height, self.img_format)
|
||||
|
||||
elif op == 'diff':
|
||||
data_result= []
|
||||
|
||||
for i in range(max(height, self.height)):
|
||||
row = []
|
||||
for j in range(max(width, self.width)):
|
||||
pixel = []
|
||||
|
||||
|
||||
if i < min(height, self.height) and j < min(width, self.width):
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append((self.data[i][j][k] - dataB[i][j][k] + 256) % 256)
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
elif i < self.height and j < self.width:
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(self.data[i][j][k])
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
elif i < height and j < width:
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(dataB[i][j][k])
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
else:
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(0)
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
|
||||
row.append(pixel)
|
||||
|
||||
data_result.append(row)
|
||||
|
||||
else: # op == 'product':
|
||||
data_result= []
|
||||
|
||||
for i in range(max(height, self.height)):
|
||||
row = []
|
||||
for j in range(max(width, self.width)):
|
||||
pixel = []
|
||||
|
||||
|
||||
if i < min(height, self.height) and j < min(width, self.width):
|
||||
for k in range(self.nb_real_channels):
|
||||
pxl = (self.data[i][j][k] * dataB[i][j][k]) % 256
|
||||
pixel.append(min(255, max(0, pxl)))
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
elif i < self.height and j < self.width:
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(self.data[i][j][k])
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
elif i < height and j < width:
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(dataB[i][j][k])
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
else:
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(0)
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
|
||||
row.append(pixel)
|
||||
|
||||
data_result.append(row)
|
||||
|
||||
image_result = QtGui.QImage(self.width, self.height, self.img_format)
|
||||
|
||||
if self.nb_chanels > 1:
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
image_result.setPixel(j, i, QtGui.qRgba(data_result[i][j][0],
|
||||
data_result[i][j][1],
|
||||
data_result[i][j][2],
|
||||
data_result[i][j][3]))
|
||||
elif self.image_object.bitPlaneCount() == 8:
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
image_result.setPixel(j, i, QtGui.qRgba(data_result[i][j][0],
|
||||
data_result[i][j][0],
|
||||
data_result[i][j][0],
|
||||
255))
|
||||
else:
|
||||
image_result.setColorCount(2)
|
||||
image_result.setColor(0, QtGui.qRgba(0, 0, 0, 255))
|
||||
image_result.setColor(1, QtGui.qRgba(255, 0, 0, 0))
|
||||
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
if data_result[i][j][0] == 0:
|
||||
image_result.setPixel(j, i, 0)
|
||||
else:
|
||||
image_result.setPixel(j, i, 1)
|
||||
|
||||
self.pixmap = QtGui.QPixmap.fromImage(image_result)
|
||||
self.image.setPixmap(self.pixmap)
|
||||
|
||||
self.data = data_result
|
||||
|
||||
|
||||
|
||||
def saturation(self, val):
|
||||
if self.nb_chanels > 1:
|
||||
|
||||
image_result = QtGui.QImage(self.width, self.height, self.img_format)
|
||||
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
color = QtGui.QColor(self.data[i][j][0],
|
||||
self.data[i][j][1],
|
||||
self.data[i][j][2],
|
||||
self.data[i][j][3])
|
||||
|
||||
color.convertTo(2)
|
||||
color.setHsv(color.hue(), min(max(0, color.saturation() + val), 100),color.value())
|
||||
|
||||
image_result.setPixelColor(j, i, color)
|
||||
|
||||
|
||||
self.pixmap = QtGui.QPixmap.fromImage(image_result)
|
||||
self.image.setPixmap(self.pixmap)
|
||||
|
||||
|
||||
def mono(self, funkcja):
|
||||
temp = [63, 127, 191, 255]
|
||||
|
||||
if self.image_object.bitPlaneCount() == 8:
|
||||
|
||||
data_result= []
|
||||
|
||||
for i in range(self.height):
|
||||
row = []
|
||||
for j in range(self.width):
|
||||
pixel = []
|
||||
|
||||
for k in range(self.nb_real_channels):
|
||||
pixel.append(temp[self.data[i][j][k]//64])
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
row.append(pixel)
|
||||
|
||||
data_result.append(row)
|
||||
|
||||
image_result = QtGui.QImage(self.width, self.height, self.img_format)
|
||||
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
image_result.setPixel(j, i, QtGui.qRgba(data_result[i][j][0],
|
||||
data_result[i][j][0],
|
||||
data_result[i][j][0],
|
||||
255))
|
||||
|
||||
|
||||
self.pixmap = QtGui.QPixmap.fromImage(image_result)
|
||||
self.image.setPixmap(self.pixmap)
|
||||
self.data = data_result
|
||||
|
||||
def contrast(self, funkcja):
|
||||
data_result = []
|
||||
for i in range(self.height):
|
||||
row = []
|
||||
for j in range(self.width):
|
||||
pixel = []
|
||||
if funkcja == "Linear":
|
||||
|
||||
for k in range(self.nb_real_channels):
|
||||
#zwiekszenie o 50
|
||||
pxl = int((127+(self.data[i][j][k] - 127)*255/155+0.5))
|
||||
pixel.append(min(255, max(0, pxl)))
|
||||
|
||||
|
||||
elif funkcja == "Logarithmic":
|
||||
for k in range(self.nb_real_channels):
|
||||
pxl = int(35 * math.log(self.data[i][j][k] + 1))
|
||||
pixel.append(min(255, max(0, pxl)))
|
||||
|
||||
else: #wykladniczy
|
||||
for k in range(self.nb_real_channels):
|
||||
|
||||
pxl = int(10 ** (self.data[i][j][k]/50) - 1)
|
||||
pixel.append(min(255, max(0, pxl)))
|
||||
|
||||
if self.nb_real_channels < self.nb_chanels:
|
||||
pixel.append(255)
|
||||
|
||||
row.append(pixel)
|
||||
|
||||
data_result.append(row)
|
||||
|
||||
image_result = QtGui.QImage(self.width, self.height, self.img_format)
|
||||
if self.nb_chanels > 1:
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
|
||||
image_result.setPixel(j, i, QtGui.qRgba(data_result[i][j][0],
|
||||
data_result[i][j][1],
|
||||
data_result[i][j][2],
|
||||
data_result[i][j][3]))
|
||||
|
||||
elif self.image_object.bitPlaneCount() == 8:
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
image_result.setPixel(j, i, QtGui.qRgba(data_result[i][j][0],
|
||||
data_result[i][j][0],
|
||||
data_result[i][j][0],
|
||||
255))
|
||||
else:
|
||||
image_result.setColorCount(2)
|
||||
image_result.setColor(0, QtGui.qRgba(0, 0, 0, 255))
|
||||
image_result.setColor(1, QtGui.qRgba(255, 0, 0, 0))
|
||||
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
if data_result[i][j][0] == 0:
|
||||
image_result.setPixel(j, i, 0)
|
||||
else:
|
||||
image_result.setPixel(j, i, 1)
|
||||
|
||||
self.pixmap = QtGui.QPixmap.fromImage(image_result)
|
||||
self.image.setPixmap(self.pixmap)
|
||||
self.data = data_result
|
||||
|
||||
|
||||
self.pixmap = QtGui.QPixmap.fromImage(image_result)
|
||||
self.image.setPixmap(self.pixmap)
|
||||
self.data = data_result
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
|
||||
app = QtWidgets.QApplication(sys.argv)
|
||||
MainWindow = QtWidgets.QMainWindow()
|
||||
ui = Ui_MainWindow()
|
||||
ui.setupUi(MainWindow)
|
||||
MainWindow.show()
|
||||
sys.exit(app.exec_())
|
||||
|
Loading…
Reference in New Issue
Block a user