Results 1 to 13 of 13

Thread: How can I copy image from anohter image

  1. #1
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question How can I copy image from anohter image

    Hi, I have learn Qt for several months and now I have created a program that related to the drawing. I want to get part of an image that user need. They can circle or use the painter to draw the shape to cut the part of the image. Thank in advanced for help me.
    Capture1.jpg

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I copy image from anohter image

    Create a QImage of the same size. Create a QPolygon to represent you drawn area. Now loop through the length and breadth of the image. At each point, if QPoygon::contains(point), set original QImage:ixel else set some default.

  3. The following user says thank you to pkj for this useful post:

    chong_kimkeang (27th September 2012)

  4. #3
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How can I copy image from anohter image

    Thanks, but can you explain me in more detail because I am new to Qt. If possible, can you please write a short code example.

  5. #4
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How can I copy image from anohter image

    Sorry, what function can I create image the same size? and when I loop the point, still how can I cut the image in the shape I draw?

  6. #5
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How can I copy image from anohter image

    Sorry, what function can I create image the same size? and when I loop the point, still how can I cut the image in the shape I draw?

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

    Default Re: How can I copy image from anohter image

    Sorry, what function can I create image the same size?
    Qt Code:
    1. QImage newImage = oldImage.copy();
    2. // or
    3. QImage newImage(oldImage.size(), oldImage.format());
    4. newImage.fill(Qt::transparent); // or something like this
    To copy to clipboard, switch view to plain text mode 
    and when I loop the point, still how can I cut the image in the shape I draw?
    Using the approach described above: You need to obtain the outline of the selection area as a QPolygon. Then you can use QPolygon::containsPoint() to see if a given pixel (point) is inside or outside the polygon, i.e. selected or not selected. Then, for each pixel in the original that is inside the polygon set the corresponding pixel in the copy. It is fairly straightforward nested for() loops once you have the polygon.

    How you get the polygon depends on how your application handles the drawing... and we don't know that.

    There are probably other ways to do this using masks.
    Last edited by ChrisW67; 19th September 2012 at 22:27.

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

    chong_kimkeang (27th September 2012)

  9. #7
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How can I copy image from anohter image

    Thanks, now I have polygon but how can I crop the image inside the Polygon? what function should I use because I don't know other functions that can copy the image beside copy() that can copy only rectangle. I don't know how can I get the image inside it. Thanks for helping me
    Last edited by chong_kimkeang; 20th September 2012 at 04:36.

  10. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,594
    Thanks
    13
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How can I copy image from anohter image

    Try something like this for a slow approach
    Qt Code:
    1. for (int x = 0; x < oldImage.width(); ++x) {
    2. for (int y = 0; y < oldImage.width(); ++y) {
    3. QPoint p(x, y);
    4. if (polygon.containsPoint(p, Qt::OddEvenFill)) {
    5. QRgb color = oldImage.pixel(p);
    6. newImage.setPixel(p, color);
    7. }
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    (Untested code)

    You should investigate ways to use the image composition modes to draw a filled version of your polygon over a copy of the original leaving only the intersection. This would be much faster.

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

    chong_kimkeang (27th September 2012)

  12. #9
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How can I copy image from anohter image

    Thanks, however when I loop, it does nothing and I can't even close the app but I think it is because of other part of the code. Thanks all of you very much for giving me, I will ask you again whenever I have problem with Qt. Thanks

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

    Default Re: How can I copy image from anohter image

    Line 2 has an error and should be checking the height not width.

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

    chong_kimkeang (27th September 2012)

  15. #11
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How can I copy image from anohter image

    Hello everyone, for doing with polygon, I can't because it always can't be close. So could you tell me how to cut the image by using mask?

  16. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,594
    Thanks
    13
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How can I copy image from anohter image

    If you don't have a closed polygon then what defines the region you want to copy?

    Here is an example of a faster way to do it:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. QImage source("Lenna.png");
    8.  
    9. // An arbitrary region to cut out
    10. QPolygon region;
    11. region << QPoint(10, 10) << QPoint(100, 200) << QPoint(500, 50);
    12.  
    13. // Let's do it
    14. path.addPolygon(region);
    15.  
    16. QImage cutout(source.size(), QImage::Format_ARGB32_Premultiplied);
    17. cutout.fill(Qt::transparent);
    18. QPainter p(&cutout);
    19. p.setClipPath(path);
    20. p.drawImage(0, 0, source);
    21. p.end();
    22.  
    23. QLabel l;
    24. l.setPixmap(QPixmap::fromImage(cutout));
    25. l.show();
    26.  
    27. return a.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

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

    chong_kimkeang (1st October 2012)

  18. #13
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How can I copy image from anohter image

    Hello everyone, I am very happy because now I can cut that image. Greatly thank everyone particularly the Expert ChrisW67 who give me the sample code which help me create this success.

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. Replies: 3
    Last Post: 3rd August 2012, 10:35
  3. Replies: 1
    Last Post: 29th November 2009, 20:33
  4. How to copy sellected ellipse area from image
    By adamsakli in forum Newbie
    Replies: 2
    Last Post: 24th September 2009, 22:11
  5. How to copy selected ellipse area from image
    By adamsakli in forum Qt Programming
    Replies: 5
    Last Post: 24th September 2009, 19:54

Tags for this Thread

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.