Results 1 to 13 of 13

Thread: Copy A non-Rectangular Image

  1. #1
    Join Date
    May 2013
    Location
    Mumbai
    Posts
    11
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Post Copy A non-Rectangular Image



    hi

    I am able to crop image by using the copy() function by copying image from rectItem that i have drawn,but when i rotate image to 20 degree or any degree and now i want to crop that rotate rect Image, how can i copy a nonRectangular Image
    Help me guys .

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Copy A non-Rectangular Image

    The rotated image can be copied the same way as not rotated image, what is problem it doing so?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    May 2013
    Location
    Mumbai
    Posts
    11
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Copy A non-Rectangular Image

    i did try to copy the roated image from the Rect but u see as soon as i rotate the Rect the Rect is no longer a Rectangle it becomes a Ploygon so now how can i copy that because copy function only take Rect Paramter.....so how ll i copy that new Rect(thats now a polygon)...hope U got what i mean to say.


    or is there any alternate way to copy or crop from any shape beside only Rect

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Copy A non-Rectangular Image

    Rotated or not, a rectangle is still a rectangle.

    QImage::copy() can only copy a rectangle that is aligned with the sides of the image. If you want to copy a rectangular area that is rotated some number of degrees from horizontal then you need to rotate the QImage in the opposite direction and take a horizontal rectangular copy() from that image.

    Of course, if you have the content of the rectangle before you rotated it then, in an ideal world, that is what it contains after you rotate it also.

  5. #5
    Join Date
    May 2013
    Location
    Mumbai
    Posts
    11
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Copy A non-Rectangular Image

    Quote Originally Posted by ChrisW67 View Post

    If you want to copy a rectangular area that is rotated some number of degrees from horizontal then you need to rotate the QImage in the opposite direction and take a horizontal rectangular copy() from that image.

    .
    Sorry Chris but i m still not getting it .
    this is My code plzz check it .
    Qt Code:
    1. //Image loading is Done.
    2. //File Crop Code
    3.  
    4. void FileCropper::onFileCrop()
    5. {
    6. myLeft=ui->myLeftSpinBox->value();
    7. myRight=ui->myRightSpinBox->value();
    8. myTop=ui->myTopSpinBox->value();
    9. myBottom=ui->myBottomSpinBox->value();
    10. QPixmap pixmap = myBeforePixItem->pixmap();
    11.  
    12. QRectF newRect = myRectItem->rect();
    13. QPointF point = myRectItem->scenePos();
    14. float x = point.x();
    15. float y = point.y();
    16. QRectF moveRect( x + newRect.left() , y + newRect.top() , newRect.width(), newRect.height() );
    17. qDebug()<<"X:"<<x;
    18. qDebug()<<"Y:"<<y;
    19. QPixmap newImage = pixmap.copy( moveRect.toRect() );
    20. myAfterScene.setSceneRect( 0, 0, newRect.width(), newRect.height());
    21. ui->myAfterGraphicsVew->fitInView(myAfterScene.sceneRect(),Qt::KeepAspectRatio);
    22. ui->myAfterGraphicsVew->centerOn(newRect.width()/2,newRect.height()/2);
    23. myAfterPixItem->setPixmap( newImage );
    24. }
    25. //my Rect Code
    26.  
    27. void FileCropper::onChange()
    28. {
    29. myLeft=ui->myLeftSpinBox->value();
    30. myRight=ui->myRightSpinBox->value();
    31. myTop=ui->myTopSpinBox->value();
    32. myBottom=ui->myBottomSpinBox->value();
    33. QPixmap pixmap = myBeforePixItem->pixmap();
    34. QRectF newRect( myLeft, myTop, pixmap.width() - myLeft -myRight, pixmap.height() - myTop -myBottom );
    35. myRectItem->setRect( newRect );
    36. }
    37. //Rotate Code
    38. void FileCropper::onRotate(int val)
    39. {
    40. QRectF rotateRect(0,0,myBeforePixItem->pixmap().width(),myBeforePixItem->pixmap().height());
    41. QTransform t;
    42. t.translate(rotateRect.center().x(),rotateRect.center().y());
    43. t.rotate(val);
    44. t.translate(-rotateRect.center().x(),-rotateRect.center().y());
    45. myRectItem->setTransform(t,false);
    46. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Copy A non-Rectangular Image

    You started off wanting to copy a rectangular area, now you want to crop an image.

    The resulting image must be rectangular. What exactly do you want to occur to the areas outside the rotated rectangle? What size should the resulting image be; the same size as the original, or the minimum size to hold the rotated rectangle?

    Here is one approach to extract an arbitrary area:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv)
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. // http://en.wikipedia.org/wiki/File:Lenna.png
    8. QPixmap source("Lenna.png");
    9.  
    10. // My desired rectangle
    11. QRect area(150, 150, 300, 50);
    12. QTransform transform;
    13. transform.translate(area.center().x(), area.center().y());
    14. transform.rotate(45);
    15. transform.translate(-area.center().x(), -area.center().y());
    16.  
    17. QBitmap mask(source.size());
    18. mask.fill(Qt::color1);
    19. QPainter p(&mask);
    20. p.setPen(Qt::color0);
    21. p.setBrush(Qt::color0);
    22. p.setTransform(transform);
    23. p.drawRect(area);
    24. p.end();
    25.  
    26. QPixmap dest(source);
    27. dest.setAlphaChannel(mask);
    28.  
    29. QLabel before;
    30. before.setPixmap(source);
    31. QLabel after;
    32. after.setPixmap(dest);
    33.  
    34. before.show();
    35. after.show();
    36.  
    37. return app.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 25th June 2013 at 23:39.

  7. The following user says thank you to ChrisW67 for this useful post:

    Parvat (26th June 2013)

  8. #7
    Join Date
    May 2013
    Location
    Mumbai
    Posts
    11
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Smile Re: Copy A non-Rectangular Image

    i have send you my app image where in 1st image it does the cropping(copying ) of image perfectly but in 2 image i have rotate the rectangle and when i crop the result is in 2nd Image .

    i want to send my exe file how can i send u so that u can check what i meant to say?
    1.jpg2.jpg1.jpg2.jpg
    thanks for the approach .
    Last edited by Parvat; 26th June 2013 at 07:14.

  9. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Copy A non-Rectangular Image

    i want to send my exe file how can i send u so that u can check what i meant to say?
    Don't. I won't run it even if you gave me one that would run on my system, and I suspect no-one else will either. There are obvious reasons for banning executables.

    It is not the forum's job to discern your requirements or test your software. If you want to explain your requirement then do so in English, with the aid of pictures (screen shots or mock-ups). You can easily upload images to this forum. Perhaps then we will understand exactly what it is you want.

  10. #9
    Join Date
    May 2013
    Location
    Mumbai
    Posts
    11
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Copy A non-Rectangular Image

    k sorry for that i got it i have uploaded images .....

  11. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Copy A non-Rectangular Image

    I still don't know what you expected to see.
    For this input and selected rectangular area:
    No0.jpg
    Did you expect this:
    No1.jpg
    or this:
    No2.jpg
    (black areas transparent) or something else?
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  12. The following user says thank you to ChrisW67 for this useful post:

    Parvat (26th June 2013)

  13. #11
    Join Date
    May 2013
    Location
    Mumbai
    Posts
    11
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Smile Re: Copy A non-Rectangular Image

    Quote Originally Posted by ChrisW67 View Post
    I still don't know what you expected to see.
    For this input and selected rectangular area:
    No0.jpg
    Did you expect this:
    No1.jpg
    or this:
    No2.jpg
    (black areas transparent) or something else?
    Finally you got it what i mean to say
    For the image you gave
    i expected 1st output .
    No1.jpg
    back area Transparent

    Thanks Chris for understanding my query.
    Last edited by Parvat; 26th June 2013 at 08:54.

  14. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Copy A non-Rectangular Image

    Then my previous example code is exactly what you need.
    Construct a rectangle of the specified size.
    Construct a transform to rotate and shift it to the specified coordinates.
    Make a mask using a transformed painter
    Apply the mask as the alpha channel of a copy of the image.
    Profit.

  15. The following user says thank you to ChrisW67 for this useful post:

    Parvat (26th June 2013)

  16. #13
    Join Date
    May 2013
    Location
    Mumbai
    Posts
    11
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Copy A non-Rectangular Image

    Thanks .finally got it .

Similar Threads

  1. Qwebview Copy Text And Save Image
    By ShapeShiftme in forum Qt Programming
    Replies: 4
    Last Post: 11th August 2016, 06:30
  2. Copy to a memory image png or bmp
    By giorgik in forum Qt Programming
    Replies: 2
    Last Post: 22nd April 2013, 17:18
  3. How can I copy image from anohter image
    By chong_kimkeang in forum Newbie
    Replies: 12
    Last Post: 27th September 2012, 11:05
  4. Replies: 1
    Last Post: 29th November 2009, 20:33
  5. How to copy selected ellipse area from image
    By adamsakli in forum Qt Programming
    Replies: 5
    Last Post: 24th September 2009, 19:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.