В примере изображение растягивается вместе с окном, так как оно слишком маленькое.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QSizePolicy, QGridLayout
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
from PIL import Image
from PIL import ImageQt
class DrawOnPython(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Пример рисования в Python")
self.setGeometry(300, 300, 400, 300)
self.label_image = QLabel()
self.label_image.setScaledContents(True)
self.label_image.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.label_image.setAlignment(Qt.AlignCenter)
self.layout = QGridLayout(self)
self.layout.addWidget(self.label_image, 0, 0)
self.create_image()
self.show_image()
def create_image(self):
image = Image.new(mode="RGB", size=(20, 20))
self.image = image.convert("RGBA")
self.image.putpixel((3, 4), (255, 0, 0))
def show_image(self):
image_qt = ImageQt.ImageQt(self.image)
pixmap = QPixmap.fromImage(image_qt)
self.label_image.setPixmap(pixmap)
def main():
qapp = QApplication([])
draw_on_python = DrawOnPython()
draw_on_python.show()
sys.exit(qapp.exec())
if __name__ == '__main__':
main()