I'm creating a small GUI to help me calculate the RGB of an image by clicking on a button I created the RGB calculator program in a separate code and create on Qt designer the push button and table widget to help me see the result but I don't know how to run the external RGB program in the QT prgram
this is the GUI program
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from qtpy.uic import loadUi
from PyQt5.QtCore import QProcess
# creat a class to import the UI file
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi("python.ui", self)
#for framless window
#flags = QtCore.Qt.WindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
#self.setWindowFlags(flags)
#main
if __name__ == "__main__":
App = QApplication(sys.argv)
welcome = MainWindow()
welcome.show()
sys.exit(App.exec())
and this is the RGB calculator code
from PIL import Image
i = Image.open("test.jpg", "r")
pixels = i.load()
width, height = i.size
nb_pixels=width*height
all_pixels = []
for x in range(width):
for y in range(height):
cpixel = pixels[x, y]
all_pixels.append(cpixel)
#calculate the RGB value of all pixels in the picture
red=sum(i[0] for i in all_pixels)//nb_pixels
green=sum(i[1] for i in all_pixels)//nb_pixels
blue=sum(i[2] for i in all_pixels)//nb_pixels
#Calculate luminance
luminance=0.2126*red 0.7152*green 0.0722*blue
print ("red=",red)
print ("green=",green)
print ("blue=",blue)
print ("luminance=",luminance)
All I want is to push the button an the information shows in the tabel something like this
and thank you in advance.
CodePudding user response:
You can create a python module (basically your UI and backend located together)
Then, in QT program you just import your RGB calculator program (you will need to wrap your code into a function (see below) and import this function, eg from calculator import calculate
)
after that, you just bind an onclick event in QT. When user presses the button the imported function will be called and executed)
from PIL import Image
def calculate():
i = Image.open("test.jpg", "r")
pixels = i.load()
width, height = i.size
nb_pixels=width*height
all_pixels = []
for x in range(width):
for y in range(height):
cpixel = pixels[x, y]
all_pixels.append(cpixel)
#calculate the RGB value of all pixels in the picture
red=sum(i[0] for i in all_pixels)//nb_pixels
green=sum(i[1] for i in all_pixels)//nb_pixels
blue=sum(i[2] for i in all_pixels)//nb_pixels
#Calculate luminance
luminance=0.2126*red 0.7152*green 0.0722*blue
print ("red=",red)
print ("green=",green)
print ("blue=",blue)
print ("luminance=",luminance)
Do not forget to mark the answer as accepted if that helped.
CodePudding user response:
you have to make database first to show data on table widget.you better use sqlite which is a built-in python database.then you should connect your button to calculate func and a function to insert your database to table.I recommend use threading for this so you gui window do not freeze.here is the sample:
import sqlite3
import threading
conn = sqlite3.connect("machine.db", check_same_thread=False)
mycursor = conn.cursor()
mycursor.execute(f"DROP TABLE IF EXISTS {table_name} ")
mycursor.execute(f"""CREATE TABLE {table_name} (red varchar(50),green varchar(50),blue varchar(50),luminance varchar(50))""")
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi("python.ui", self)
self.{your button name} = QtWidgets.QPushButton(clicked=lambda: threading.Thread(target=self.calculate()).start()
)
def calculate(self):
i = Image.open("test.jpg", "r")
pixels = i.load()
width, height = i.size
nb_pixels=width*height
all_pixels = []
for x in range(width):
for y in range(height):
cpixel = pixels[x, y]
all_pixels.append(cpixel)
#calculate the RGB value of all pixels in the picture
red=sum(i[0] for i in all_pixels)//nb_pixels
green=sum(i[1] for i in all_pixels)//nb_pixels
blue=sum(i[2] for i in all_pixels)//nb_pixels
#Calculate luminance
luminance=0.2126*red 0.7152*green 0.0722*blue
mycursor.execute(
f"insert or ignore into {table_name} values(?,?,?,?)",
(
red,green,blue,luminance
),
)
conn.commit()
mycursor.execute(f"SELECT * FROM {table_name}")
result = mycursor.fetchall()
for row_number, row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number, column_data in enumerate(row_data):
item = str(column_data)
if column_number == 0:
item = self.imagelabel(column_data)
self.tableWidget.setCellWidget(row_number, column_number, item)
else:
self.tableWidget.setItem(
row_number, column_number, QtWidgets.QTableWidgetItem(item)
)
self.tableWidget.verticalHeader().setDefaultSectionSize(50)