How to use QPixmap in Python

WBOY
Release: 2023-04-19 16:58:13
forward
1630 people have browsed it

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的矩形中显示图片
}
Copy after login

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);
}
Copy after login

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); 
}
Copy after login

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); 
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!