1 Attachment(s)
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.
Attachment 8223
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::pixel else set some 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. :o
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?
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?
Re: How can I copy image from anohter image
Quote:
Sorry, what function can I create image the same size?
Code:
QImage newImage
= oldImage.
copy();
// or
QImage newImage
(oldImage.
size(), oldImage.
format());
newImage.fill(Qt::transparent); // or something like this
Quote:
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.
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 :)
Re: How can I copy image from anohter image
Try something like this for a slow approach
Code:
for (int x = 0; x < oldImage.width(); ++x) {
for (int y = 0; y < oldImage.width(); ++y) {
if (polygon.containsPoint(p, Qt::OddEvenFill)) {
QRgb color = oldImage.pixel(p);
newImage.setPixel(p, color);
}
}
}
(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.
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 :)
Re: How can I copy image from anohter image
Line 2 has an error and should be checking the height not width.
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?
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:
Code:
#include <QtGui>
int main(int argc, char *argv[])
{
// An arbitrary region to cut out
// Let's do it
path.addPolygon(region);
QImage cutout
(source.
size(),
QImage::Format_ARGB32_Premultiplied);
cutout.fill(Qt::transparent);
p.setClipPath(path);
p.drawImage(0, 0, source);
p.end();
l.
setPixmap(QPixmap::fromImage(cutout
));
l.show();
return a.exec();
}
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. ;)