May-25-2023, 01:41 AM
Not sure on why it happens as im still very new to python... Any suggestions or can you guys see any reason of why it might be happening??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import sys import os import cv2 from PyQt6.QtWidgets import QApplication, QLabel, QVBoxLayout, QPushButton, QScrollArea, QWidget, QMainWindow, QGridLayout, QFileDialog from PyQt6.QtGui import QPixmap, QImage, QPainter from PyQt6.QtCore import Qt, QThread, pyqtSignal class App(QMainWindow): def __init__( self , parent = None ): super (App, self ).__init__(parent) self .setWindowTitle( "Character Creator" ) self .setFixedSize( 600 , 500 ) self .setStyleSheet( "background-color: #3C3C3C;" ) self .categories = [ 'shirt' , 'pants' , 'shoes' , 'accessories' , 'eyes' , 'hair' , 'skin-colour' ] # Add more categories as needed self .category_buttons = [] for i, category in enumerate ( self .categories): button = QPushButton(category.upper(), self ) button.move( 10 , 40 + i * 30 ) button.setFixedSize( 100 , 25 ) button.setStyleSheet( "background-color: #1C1A1A; color: #EFEFEF;" ) button.clicked.connect( lambda _, c = category: self .show_category(c)) self .category_buttons.append(button) def show_category( self , category): category_root = CategoryWindow( self , category) category_root.show() class ImageLoaderThread(QThread): image_loaded = pyqtSignal(QPixmap, str ) def __init__( self , category_folder, parent = None ): super (ImageLoaderThread, self ).__init__(parent) self .category_folder = category_folder def run( self ): skin_parts = self .load_skin_parts( self .category_folder) for skin_part in skin_parts: pixmap = QPixmap(os.path.join( self .category_folder, skin_part)) self .image_loaded.emit(pixmap, skin_part) def load_skin_parts( self , folder): return os.listdir(folder) class CategoryWindow(QWidget): def __init__( self , parent = None , category = ''): super (CategoryWindow, self ).__init__(parent) self .setWindowTitle(category.upper()) self .setFixedSize( 200 , 500 ) self .setStyleSheet( "background-color: #3C3C3C;" ) self .layout = QVBoxLayout() self .setLayout( self .layout) self .scrollArea = QScrollArea() self .scrollArea.setWidgetResizable( True ) self .scrollAreaWidgetContents = QWidget( self .scrollArea) self .scrollAreaWidgetContents.setLayout( self .layout) self .scrollArea.setWidget( self .scrollAreaWidgetContents) self .layout.addWidget( self .scrollArea) category_folder = os.path.join(r 'Desktop\MinecraftSkinEditor\clothes\alex' , category) self .image_loader_thread = ImageLoaderThread(category_folder) self .image_loader_thread.image_loaded.connect( self .on_image_loaded) self .image_loader_thread.start() def on_image_loaded( self , pixmap, skin_part): label = ClickableQLabel() label.setPixmap(pixmap) label.clicked.connect( lambda sp = skin_part: self .on_skin_part_click(label, sp)) self .layout.addWidget(label) def on_skin_part_click( self , label, skin_part): print ( 'Clicked on skin part:' , skin_part) pixmap = label.pixmap().copy() # Open a dialog to select the skin part image file_dialog = QFileDialog() file_dialog.setWindowTitle( "Select Skin Part Image" ) file_dialog.setFileMode(QFileDialog.FileMode.ExistingFile) file_dialog.setNameFilter( "Images (*.png *.jpg *.jpeg *.gif)" ) if file_dialog. exec () = = QFileDialog.DialogCode.Accepted: image_path = file_dialog.selectedFiles()[ 0 ] skin_part_image = cv2.imread(image_path) # Resize the skin part image to fit the preview resized_skin_part_image = cv2.resize(skin_part_image, (pixmap.width(), pixmap.height()), interpolation = cv2.INTER_LINEAR) # Convert the resized skin part image to QPixmap skin_part_qimage = QImage(resized_skin_part_image.data, resized_skin_part_image.shape[ 1 ], resized_skin_part_image.shape[ 0 ], QImage.Format_BGR888) skin_part_pixmap = QPixmap.fromImage(skin_part_qimage) # Paste the skin part image onto the preview image pixmap = pixmap.copy() # Create a copy to modify painter = QPainter(pixmap) painter.setCompositionMode(QPainter.CompositionMode.DestinationOver) painter.drawPixmap( 0 , 0 , skin_part_pixmap) painter.end() # Update the label with the modified image label.setPixmap(pixmap) def create_complete_skin( self ): skin_parts = [] # Collect the skin parts from the labels for label in self .findChildren(ClickableQLabel): pixmap = label.pixmap() if pixmap is not None : skin_parts.append(pixmap.toImage()) # Create a blank image for the complete skin complete_skin_width = 64 complete_skin_height = 64 complete_skin = QImage(complete_skin_width, complete_skin_height, QImage.Format_ARGB32) complete_skin.fill(Qt.GlobalColor.transparent) # Paste the skin parts onto the complete skin painter = QPainter(complete_skin) for i, skin_part in enumerate (skin_parts): x = i % 8 * 8 y = i / / 8 * 8 painter.drawImage(x, y, skin_part) painter.end() # Save the complete skin save_path, _ = QFileDialog.getSaveFileName( self , "Save Complete Skin" , " ", " PNG Files ( * .png)") if save_path: complete_skin.save(save_path) class ClickableQLabel(QLabel): clicked = pyqtSignal() def __init__( self , parent = None ): QLabel.__init__( self , parent) def mousePressEvent( self , event): self .clicked.emit() if __name__ = = "__main__" : app = QApplication(sys.argv) window = App() window.show() sys.exit(app. exec ()) |