PDA

View Full Version : Maintaining Aspect Ratio of Image in Qt



rohitkk
30th April 2014, 13:21
Dear All,

Is there any way for maintaining the aspect ratio for the Image being taken from the db.
What exactly I am doing is I am populating db with the image (the datatype for the image is BLOB in DB) & later on I am displaying that image on the reports.

The code I have used for displaying the Image on the reports is below,



QPainter *reportPainter;
QPixmap p;
QByteArray myImage;
p.loadFromData(myImage); //myImage = Acquired from the db
p.scaled(120,60,Qt::KeepAspectRatio); //This is not required as x & y co-ordinates are fix
reportPainter->drawImage(QRect(60,0,120,60),p.toImage()); //Adding image in QPainter with rectangle size kept fixed.


In Line of Code no. 4 : Image is scaled to fix width & height (viz. not the requirement, as it should be calculated based on the acquired image height & width)
In Line of Code no. 5 : Similarly, the Image is drawn in the fix rectangle.

How to get the image's height & width present in the db? or Is there any other way to do so?
And yes I want to maintain the aspect ratio of the image in the report acquired from the database.

Thanks in Advance.
Regards,
Rohit K

anda_skoa
30th April 2014, 13:42
Line 4 loads them image, it does not scale it at all.
Line 5 is a very expensive NOOP, you are creating a scaled version of the image and then ignore it (return value not used at all).
In Line 6 is another unneeded operation, i.e. toImage(). If you want to work with QImage, use QImage instead of QPixmap. Right now you convert from image to pixmap (internally in line 4) and then back again in line 6.

Cheers,
_

rohitkk
30th April 2014, 14:05
Line 4 loads them image, it does not scale it at all.
Yeah I know this.

Line 5 is a very expensive NOOP, you are creating a scaled version of the image and then ignore it (return value not used at all).
What is the right way to do it?
Does storing the returned value really helps?

In Line 6 is another unneeded operation, i.e. toImage(). If you want to work with QImage, use QImage instead of QPixmap. Right now you convert from image to pixmap (internally in line 4) and then back again in line 6.
I have used QByteArray to retrieve image from the database (present as BLOB in db) hence, i am not converting the image i am loading the data (present in QByteArray datatype) into the QPixMap.
I have used QPixMap instead of the QImage because i want to maintain the aspect ratio , as it cannot be done using the QImage (I have tried it before & Image was coming distorted using QImage) & hence, again converting the QPixMap to QImage to display it on the QPainter .

anda_skoa
30th April 2014, 17:56
Does storing the returned value really helps?

It helps if you want to use the scaled image. If your goal is to use CPU time then you can of course ignore the result.
However, from your general description it sounds like you want the scaled image, not the original.



I have used QByteArray to retrieve image from the database (present as BLOB in db) hence, i am not converting the image i am loading the data (present in QByteArray datatype) into the QPixMap.

Loading is always done through QImage, so loading into QPixmap loads into a QImage and then converts it to a QPixmap.



I have used QPixMap instead of the QImage because i want to maintain the aspect ratio

What?
Qimage::scaled() takes the same arguments.

Cheers,
_

rohitkk
6th May 2014, 15:53
Ok .
I have modified my Code.


QImage img;
if (img.loadFromData(schoolLogo)) {
reportPainter->drawImage(QRect(60,0,120,60),img.scaled(img.size() ,Qt::KeepAspectRatio));
}


Please see the screenshot for ,
screen 1 : my actual Image (.jpg)10337
screen 2: In my pdf that I have generated containing that picture 10338

Hence, in screen 1 viz the actual image , have different height & width.
whereas in screen 2 viz the image on the pdf , where the aspect ratio is not at all maintained.

Please help me out to maintain the aspect ratio of the image & why isn't the Qt::KeepAspectRatio not working as desired?

Thanks in advanced.

Regards,
Rohit

anda_skoa
7th May 2014, 11:51
What do you think this part of the code does?


img.scaled(img.size(),Qt::KeepAspectRatio)

I.e. in words, what do you expect as the return value of img.scaled()?


Cheers,
_

kapoorsudhish
7th May 2014, 12:50
Try this as it worked for me:


//viewWidth,viewHeight is the size of the image in the aspect ratio needed.
image = im.scaled(viewWidth,viewHeight,Qt::IgnoreAspectRat io,Qt::SmoothTransformation);

rohitkk
9th May 2014, 13:01
What do you think this part of the code does?


img.scaled(img.size(),Qt::KeepAspectRatio)


What is the correct way to do it?
As you know I have fixed size rectangle,

QRect(60,0,120,60)
And I want to display the image in this rectangle with it's aspect ratio maintained.



I.e. in words, what do you expect as the return value of img.scaled()?

A scaled Image . Right?

anda_skoa
9th May 2014, 13:10
What is the correct way to do it?

Correct depends on the desired outcome.
Your code is correct if you want an expensive copy of the original image.


As you know I have fixed size rectangle,

QRect(60,0,120,60)

Which is why I was wondering why you are not using that size instead of the image's own size.



A scaled Image . Right?
Yes, scaled to exactly the same size.

I have troubles understanding why you are not passing the size you want to scale to.


Cheers,
_

rohitkk
9th May 2014, 13:27
Try this as it worked for me:


//viewWidth,viewHeight is the size of the image in the aspect ratio needed.
image = im.scaled(viewWidth,viewHeight,Qt::IgnoreAspectRat io,Qt::SmoothTransformation);


What will be view width & view height?
Is it the width & height of the fixed size rectangle in which image will be displayed? or width & height of the image?

Added after 10 minutes:

@anda_skoa
Up till now u must have understand my problem ...
plz provide me the solution/...

Regards,
Rohit

Is there any way to maintain aspect ratio ? or I have to calculate the aspect ratio?

anda_skoa
9th May 2014, 14:11
Up till now u must have understand my problem ...
plz provide me the solution/...


Come on, you are a software developer :)

QImage::scaled() needs two things from you
* a target size hint
* an aspect hint

You have the latter correct: Qt::KeepAspectRatio

You have a target rectangle: QRect(60,0,120,60)

Go see if you can find a method that results in the size of a QRect.

Cheers,
_

kapoorsudhish
10th May 2014, 06:17
The viewWidth/viewHeight are the width and height of the display area rectangle (fixed size rectangle) where the image needs to be displayed. By passing it to the image scaled function the input image gets scaled to the width and height of the display rectangle area.

Also to maintain aspect ratio my suggestion is to adjust the aspect ratio of the display rectangle and not of the image.