Is there a easy way to load sf::Image using for example QDialog or other library? I mean I want to load image for using it in sfml by "Choose image..." window.
You can use QFileDialog to get the path of the given image:
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
QDir::currentDirPath() ,
tr("Images (*.png *.xpm *.jpg)"));
if (!fileName.isEmpty()) {
// Now you can init your sf::image
}
According to the official documentation of SFML, you can use the loadFromFile function
sf::Image image;
if (image.loadFromFile(filename.toStdString())) {
std::cout << "image loaded with success";
}
Related
I'm using this code to load image and show in screen.
void MainWindow::on_actionOpen_triggered(void){
QString fileName;
fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "C:\\Users", tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));
qim.load( fileName );
pixmap = QPixmap::fromImage( qim );
scene.clear();
scene.addPixmap( pixmap );
ui->graphicsView->setScene(&scene);
}
But when I upload a small picture, it doesn't fill the screen and it appears in its original size. As you can see:
The desired screen should look like this:
(I am trying to make an application similar to this site. Grayscale doesn't matter.)
image used:
Try fitInView with Qt::AspectRatioMode on your ui->graphicsView object.
This solved my problem:
qim.load( fileName );
pixmap = QPixmap::fromImage( qim );
QPixmap scaled_img = pixmap.scaled(this->width(), this->height(), Qt::KeepAspectRatio);
scene.clear();
scene.addPixmap( scaled_img );
ui->graphicsView->setScene(&scene);
I'm struggling to print an image to the PNG format using Qt4.
The code below has default settings of either PDF or PS, but no way to choose PNG:
void DetectorView::printToFile()
{
// A basic printing function
QPrinter printer;
QPrintDialog dialog(&printer, this);
if (dialog.exec()==QDialog::Accepted) {
QPainter painter(&printer);
this->render(&painter);
std::cout << "INFO [DetectorView::printToFile] Wrote file " << std::endl;
}
else {
std::cout << "INFO [DetectorView::printToFile] Cancelling printer " << std::endl;
}
}
Any help would be appreciated!
Using this link: Rendering QWidget to QImage loses alpha-channel, you can render your widget to a QImage.
Then, using QImageWriter, you can save it to a png:
// render QWidget to QImage:
QImage bitmap(this->size(), QImage::Format_ARGB32);
bitmap.fill(Qt::transparent);
QPainter painter(&bitmap);
this->render(&painter, QPoint(), QRegion(), QWidget::DrawChildren);
// save QImage to png file:
QImageWriter writer("file.png", "png");
writer.write(bitmap);
Note: links provided are for Qt5, but this should work with Qt4.
Using jpo38's answer, I expanded to get the behaviour I wanted:
void DetectorView::printToFile()
{
QString default_name = "myImage.png";
QImage bitmap(this->size(), QImage::Format_ARGB32);
QPainter painter(&bitmap);
this->render(&painter,QPoint(),QRegion(),QWidget::DrawChildren);
QString filename = QFileDialog::getSaveFileName(this, tr("Save File"),QDir::homePath()+"/"+default_name,tr("Image Files (*.png *.jpg *.bmp)"));
QImageWriter writer(filename, "png");
writer.write(bitmap);
std::cout << "INFO [DetectorView::printToFile] Wrote image to file" << std::endl;
}
Note the QFileDialog which is needed to create the interactive window.
source image:
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"),".",
tr("Image Files(*.png *.jpg *.jpeg *.bmp)"));
image = cv::imread(fileName.toUtf8().data());
if(!image.data){
qDebug()<< "Could not open or find the image";
return ;
}
QString status = QString::number(image.rows) + "x" +
QString::number(image.cols);
ui->label_2->setText(status);
//show
cv::namedWindow("Original Image");
cv::imshow("Original Image",image);
image result:
I can't understand this situration,Where is the problem?
I'm writing a simple Qt application to test multi-threading (something I am also completely new to).I made a QApplication to manage the GUI,then I write a class VisionApp that contains the class MainWindow which is a subclass of QMainWindow.
In the MainWindow class,I write a function void MainWindow::getfromfilevd() which is connected to the button using this:
QObject::connect(ui->FileVdButton,SIGNAL(clicked()),this,SLOT(getfromfilevd()));
Then I want to read a image from file by using QFileDialog::getOpenFileName,my code is here:
void MainWindow::getfromfilevd()
{
//mutex.lock();
from_imgorvd = true;
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),"", tr("Image Files (*.png *.jpg *.bmp *.xpm)"));
if(fileName.isEmpty()) {
cv::Mat image;
image = cv::imread(fileName.toUtf8().constData(), CV_LOAD_IMAGE_COLOR);
mutex.lock();
Mat_Img = image.clone();
mutex.unlock();
}
}
however,every time I click the button ,the window of QFileDialog opened but it is blank,then my program finished unexpected.
when I use this code:
void MainWindow::getfromfilevd()
{
from_imgorvd = true;
cv::Mat image;
image = cv::imread("/home/somnus/Picture/mouse.jpg", CV_LOAD_IMAGE_COLOR);
if(! image.data) {
std::cout << "Could not open or find the image" << std::endl ;
}
else {
mutex.lock();
Mat_Img = image.clone();
mutex.unlock();
}
}
It works well.
I am really wonder which mistake I take...
Hope for your help
It should be like this, !fileName.isEmpty() in stead of fileName.isEmpty() because you need to load image when the file name is not empty not the opposite.
I'm on Windows7 and using Qt SDK 4.8.
Trying to read a file in with QImage but it just doesn't seem to load. That is, QImage(filename) or QImage(filename, "PNG") or QImage.load(filename) always return NULL.
Here's my code:
void MainWindow::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"),
QDir::homePath(),
tr("Image Files (*.png *.tga *.bmp)"));
if (!fileName.isEmpty())
{
targetImage = new QImage(fileName, "PNG");
if(targetImage->isNull())
{
QMessageBox::information(this,
tr("PhotoChop"),
tr("Cannot load %1.").arg(fileName));
return;
}
onScreenImage.setBackgroundRole(QPalette::Base);
onScreenImage.setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
onScreenImage.setScaledContents(true);
onScreenImage.setPixmap(QPixmap::fromImage(*targetImage));
}
}
What am I doing wrong?
it has to do with how QImage loads "plugins" for reading files in. One option is to enable these plugins (for different file formats) and then linking against them.
Though instead I just used Gdiplus::Bitmap (from windows) to load in the file and then, from that, created an HBITMAP which QPixmap can use.