Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I crop an image in Qt?

Tags:

image

qt

crop

I load a PNG image in a QPixmap/QImage and I want to crop it. Is there a function that does that in Qt, or how should I do it otherwise?

like image 865
sashoalm Avatar asked Aug 10 '11 12:08

sashoalm


People also ask

How do I crop an image in QT?

Since you use QPixmap, you can use its copy method and supply it with a QRect to perform the actual crop. Show activity on this post. Just use of the QPixmap's copy() functions.

How do I crop an image in a div?

Using width, height and overflow to crop images in CSSAdd a div can give it class container. Set width and height to 150px then set the overflow to hidden. Use margin to position the image based on your needs. In this case, set it to -100px 0 0 -150px.


2 Answers

You can use QPixmap::copy:

QRect rect(10, 20, 30, 40); QPixmap original('image.png'); QPixmap cropped = original.copy(rect); 

There is also QImage::copy:

QRect rect(10, 20, 30, 40); QImage original('image.png'); QImage cropped = original.copy(rect); 
like image 120
hmuelner Avatar answered Sep 28 '22 05:09

hmuelner


Use QImage instead of QPixmap:

    QImage image("initial_image.jpg");     QImage copy ;     copy = image.copy( 0, 0, 128, 128);      copy.save("cropped_image.jpg"); 

This code will save a file cropped to upper left corner 128x128px.

like image 20
Zienek Avatar answered Sep 28 '22 05:09

Zienek