【问题标题】:How to create a simple Image (QImage) with text and colors in QT and save it as file如何在 QT 中创建带有文本和颜色的简单图像(QImage)并将其保存为文件
【发布时间】:2017-01-27 23:21:57
【问题描述】:

我必须为没有自己的图像/图片的文章创建一些图像作为占位符。我已经在 python 中实现了这一点,但我想再次在 qt 应用程序中编写代码。 python 代码正在运行,它看起来像这样:

def createImage(text="Leer", pfad="", bildname="TextBild.jpg", colorname='red'):
    offset      = 20                                                                                                                                # weisses Textfeld um diesen Offset grösser darstellen
    bild        = Image.new('RGB', (width, height), colorname)
    draw        = ImageDraw.Draw(bild)
    font        = ImageFont.truetype("cour.ttf", 20)
    w,h         = draw.textsize(text, font)
    draw.rectangle((((width-w-offset)/2,(height-h-offset)/2),((width+w+offset)/2,(height+h+offset)/2)), fill='white')
    draw.text(((width-w)/2,(height-h)/2),text,'black',font)
##  print "Erzeuge: " + pfad + os.sep + bildname, "\nPfad:\t"+pfad, "\nBildname:\t"+bildname
    bild.save(pfad + os.sep + bildname) if os.path.exists(pfad) else bild.save(bildname)

但是如何在 Qt 中做到这一点?我知道有 QImage、QPainter 等,但我没有找到有用的示例。也许我做了一个系统调用来创建一个带有 qt 应用程序文本的图像。

图像如下所示:

提前感谢每一个有用的提示。

【问题讨论】:

  • QPainter 用于在屏幕上绘图,而不是创建用于保存的图像。 PIL/pillow 更有用。
  • @furas - 这在很大程度上是不正确的。 QPainter 用于在绘图设备上绘图,这包括小部件或您将其“放在屏幕上”和 QImage 等。
  • 也许,我问错了问题。上面的 python 代码确实是它应该做的,它创建了一个包含文本的图像并将图像保存为文件。我想知道的是,如何使用 qt 和 c++ 来完成。我在 c++ 应用程序中有相同的要求,但我不想对 python 函数进行系统调用。但是我没有任何线索可以用 c++ 和 qt 类来做到这一点。那么,也许 c++ 开发人员的某个人可以帮助我提供 c++ 端的等效源代码?

标签: python qt qpainter qimage


【解决方案1】:

对于您提供的这个特定图像,这将完成这项工作:

QImage image(QSize(400,300),QImage::Format_RGB32);
QPainter painter(&image);
painter.setBrush(QBrush(Qt::green));
painter.fillRect(QRectF(0,0,400,300),Qt::green);
painter.fillRect(QRectF(100,100,200,100),Qt::white);
painter.setPen(QPen(Qt::black));
painter.drawText(QRect(100,100,200,100),"Text you want to draw...");
image.save("testImage.png");

【讨论】:

    【解决方案2】:

    完美!这正是我一直在寻找的。非常感谢你!

    bool createImage(QString text="Leer", QString path="./", QString imageName="TextImage.png", QColor aColor=Qt::red)
    {
        int width = 1024;
        int height = 768;
        int offset = 25;
        int w = 400;
        int h = 200;
    
        QImage image(QSize(width,height),QImage::Format_RGB32);
        QPainter painter(&image);
        painter.setBrush(QBrush(aColor));
        painter.fillRect(QRectF(0,0,width,height),Qt::darkGreen);
        qDebug() << (width-w-offset)/2 << "\t" << (height-h-offset)/2 << "\t" << w << "\t" << h;
        QRect aRect = QRect( (width-w)/2, (height-h)/2, w, h );
        QRect aRectOffset = QRect( (width-w+offset)/2, (height-h+offset)/2, w-(offset/2), h-(offset/2) );
        painter.fillRect(QRect(aRect),Qt::white);
        painter.setPen(QPen(Qt::black));
        painter.setFont(QFont( "Courier", 20) );
        painter.drawText(QRect(aRectOffset),text);
        QDir aDir = QDir(path);
        if ( aDir.mkpath(path) )
            return image.save(path + "/" + imageName);
        else
            return image.save(imageName);
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        createImage("Ein einfacher Text\nDer auch mal länger sein kann.", "", "TestBild.png", Qt::green);
    
        return a.exec();
    } 
    

    我只是对上面的代码进行了一些尝试,现在它对我来说非常有用。 :-)

    【讨论】:

      猜你喜欢
      • 2016-02-06
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      相关资源
      最近更新 更多