I am working on making a drawing program in Pygame. All features are basically complete except for the ability to save your image. Is there any built in Pygame function that does this, or will I have to find some other way (E.G. taking a screenshot or something).
-
1crop the screen, then use `pygame.image.save()` – Jan Wilamowski Jul 12 '21 at 02:20
2 Answers
screen is a Surface object which has method subsurface(Rect) to get part of surface - and this part will be also a Surface object.
And there is function pygame.image.save(surface, filename)
Other method: at start create smaller Surface and draw on this surface (instead of drawing directly on screen) and blit() this surface on screen to display it and save() this surface to file.
You can even use many surfaces and keep them on list to create function Undo. When you use new tool then you duplicate surface and put it on list. When you use Undo then you get last surface from list.
- 134,197
- 12
- 106
- 148
-
1Thank you! This should be perfect! Also, funnily enough, I just finished implementing a similar `undo()` function, so great minds think alike I guess. – Some Dumb Python User Jul 12 '21 at 03:08
-
Also, I would've helpful voted this, but I guess I don't have enough reputation. – Some Dumb Python User Jul 12 '21 at 03:09
-
separated surface could be used also to draw image bigger then screen. You can create bigger image and display only some subsurface and use some buttons to scroll surface. – furas Jul 12 '21 at 03:10
-
as I know when you mark answer as accepted then you may have enough reputation but you have to wait few minutes to upvote it. – furas Jul 12 '21 at 03:11
-
If it's a drawing game, then you'll probably find it very easy to manually make the image.
I know it sounds scary, but it really isn't, especially in python.
You can use PyPng to convert a 2D array into an image, as I assume you already use a 2D array to store the drawing
- 73
- 1
- 5
-
I actually store the lines required to make the finished product (using the draw.line function). Is there any easy way to do that, if you know? – Some Dumb Python User Jul 12 '21 at 02:29