- Passant del editor en Python

This commit is contained in:
2023-09-29 18:51:21 +02:00
parent d836f4509d
commit a141d11760
2 changed files with 0 additions and 121 deletions

View File

@@ -1,79 +0,0 @@
import math
import sys
from PIL import Image
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QImage, qRgb
from PyQt5.QtWidgets import (
QApplication,
QLabel,
QMainWindow
)
class Surface:
def __init__(self, image_file):
im = Image.open(image_file)
self.pixels = im.tobytes()
self.w = im.width
self.h = im.height
class Palette:
def __init__(self, image_file):
im = Image.open(image_file)
pal = im.getpalette()
pal_size = math.floor(len(pal)/3)
self.colors = []
for i in range(pal_size):
self.colors.append(qRgb(pal[i*3],pal[1+i*3],pal[2+i*3]))
class Canvas:
def __init__(self, palette):
# Crear el QImage
self.img = QImage(320, 240, QImage.Format.Format_Indexed8)
# Omplir la paleta
for i in range(len(palette.colors)):
self.img.setColor(i, palette.colors[i])
for y in range(240):
for x in range(320):
self.img.setPixel(x,y,20)
# Preparem els indices de sustitucio de la paleta
self.pal = bytearray(256)
for i in range(256):
self.pal[i] = i
def blt(self, dx, dy, surface, sx, sy, sw, sh, color):
self.pal[1] = color
for y in range(sh):
for x in range(sw):
col = self.pal[surface.pixels[(sx+x)+(sy+y)*surface.w]]
if col != 0:
self.img.setPixel(dx+x,dy+y,col)
self.pal[1] = 1
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("My App")
label = QLabel("Hello")
surface = Surface("data/gat.gif")
paleta = Palette("data/test.gif")
canvas = Canvas(paleta)
canvas.blt(10, 10, surface, 0, 0, 22, 35, 18)
label.setPixmap(QPixmap.fromImage(canvas.img).scaled(640,480, Qt.IgnoreAspectRatio, Qt.FastTransformation))
label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
label.setScaledContents(True)
self.setCentralWidget(label)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

View File

@@ -1,42 +0,0 @@
import sys
from PyQt5 import QtCore, QtWidgets
class Example(QtWidgets.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QtWidgets.QHBoxLayout(self)
left = QtWidgets.QFrame(self)
left.setFrameShape(QtWidgets.QFrame.StyledPanel)
mid = QtWidgets.QFrame(self)
mid.setFrameShape(QtWidgets.QFrame.StyledPanel)
right = QtWidgets.QFrame(self)
right.setFrameShape(QtWidgets.QFrame.StyledPanel)
splitter = QtWidgets.QSplitter(QtCore.Qt.Horizontal)
splitter.addWidget(left)
splitter.addWidget(mid)
splitter.addWidget(right)
splitter.setStretchFactor(1, 1)
splitter.setSizes([250, 640, 250])
hbox.addWidget(splitter)
self.setLayout(hbox)
QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create('Cleanlooks'))
self.resize(500+640, 700)
self.setWindowTitle('QtGui.QSplitter')
self.show()
def main():
app = QtWidgets.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()