Home >Backend Development >Python Tutorial >How to use QPixmap in Python

How to use QPixmap in Python

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBforward
2023-04-19 16:58:131737browse

QPixmap is mainly used for drawing and is optimized for image display; QImage is mainly designed for image I/O, image access and pixel modification. However, if you use QPixmap to load large pictures, it will take up a lot of memory and is suitable for loading small pictures; a picture of tens of K will be enlarged many times after being loaded.

If the picture is too large, you can use QImage to load it, and then convert it to QPixmap for user drawing. QPixmap drawing effect is the best.

1. Use QPixmap to display images

void Dialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(“D:/1.jpg”);
    painter.drawPixmap(0,0,50,50,pix); //在(0,0)点起始的宽高均为50的句型中显示图片
 
    painter.translate(50,50); //将起始点改为(50,50)
    painter.drawPixmap(0,0,50,50,pix); //在(50,50)起始的宽高为50的矩形中显示图片
}

2. Use QPixmap to achieve image scaling

You can use the scaled() function in the QPixmap class to enlarge or reduce the image. This function returns the size of a rectangle scaled to have the given width and height, based on the specified mode.

void Dialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(“D:/1.jpg”);
    painter.drawPixmap(0,0,50,50,pix); //在(0,0)点起始的宽高均为50的句型中显示图片
    
    qreal wid = pix.width(); //获取图像的宽高
    qreal hei = pix.height();
    pix = pix.scaled(wid*2,hei*2,Qt::KeepAspectRatio);//将图片宽高扩大两倍,且在矩形内保持宽高比值
    painter.drawPixmap(50,50,pix);
}

3. Use QPixmap to achieve image rotation

You can use the rotate() function of the QPainter class to achieve image rotation. The default is to rotate with the origin as the center.

void Dialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(“D:/1.jpg”);
    painter.translate(50,50); //将旋转中心改为(50,50)
    painter.rotate(90); //顺时针旋转90度
    painter.translate(-50,-50); 
    painter.drawPixmap(0,0,50,50,pix); 
}

You must first change the rotation center, then rotate again, and then restore the origin to achieve the desired effect.

4. Use QPixmap to realize image distortion

Use the shear(qreal sh, qreal sv) function of the QPainter class to realize image distortion. Parameter 1 realizes horizontal deformation, and parameter 2 realizes longitudinal deformation. When their value is 0, it means no distortion.

changes in image shape are achieved by changes in the coordinate system.

void Dialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(“D:/1.jpg”);
    painter.drawPixmap(0,0,50,50,pix); 
    painter.shear(0.5,0) ;//横向扭曲
    painter.drawPixmap(50,0,50,50,pix); 
    painter.shear(0,0.5);//纵向扭曲
    painter.drawPixmap(0,50,50,50,pix); 
    painter.shear(0.5,0.5);//同时横纵扭曲
    painter.drawPixmap(50,50,50,50,pix); 
}

The above is the detailed content of How to use QPixmap in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete