- Proves de editor en PyQt5

This commit is contained in:
2023-09-28 18:55:30 +02:00
parent b0e4db1e8e
commit fa5f1de3e6

79
editor/editor.py Normal file
View File

@@ -0,0 +1,79 @@
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()