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?
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.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With