Convert canvas contents into a canvas creatable image tkinter - python

On my canvas, I am creating a large number of objects and images, and once loaded, the program finds it hard to maintain this large quantity and frequently crashes. A solution I thought up would be to convert the canvas into an image (perhaps using PIL?), clear the canvas and use canvas.create_image to paste the image-form of the original canvas. I am aware, and have found in similar questions, there are ways to convert the image to a png file/save the file on the PC - I do not want this - I want to turn the image into a PIL image or tkinter PhotoImage.
How can I go about doing this?
Thanks

Related

Tkinter screwing with PIL image transparency?

I made a custom image viewing program in python since I couldn't find one that worked how I wanted it to, and I know it at least USED to work with transparent gifs but now it doesn't. The effects seem to vary from gif to gif to gif but it always is an issue with the transparency. Doing some testing with .save I found that the PIL image is completely fine and I can export it as a png with absolutely no issues and perfect transparency. With that said, despite making the program I never really learned Tkinter since I just didn't need to use it beyond simple canvas clearing and updating, so I have no clue how to test beyond PIL. I believe the issue should be in these lines :
image = ImageTk.PhotoImage(GifFrameSized) #GifFrameSized is the resized GIF
imagesprite = canvas.create_image(show.w/2,show.h/2,image=image) #w and h are the width and height of the monitor
root.update_idletasks()
root.update()
canvas.pack()
I genuinely have no idea how the issue could be coming from any of these but I was able to use PIL to save a png of the frame in the line immediately above "image = ImageTk.PhotoImage(GifFrameSized)" and it looked fine so I have to imagine its somewhere in those lines.

Saving a large turtle graphics image from the entire canvas, not just the window

After looking for answers all around, I find lots of help and guidance on how to save a file either using turtle screen or tkinter canvas mixed with the turtle module. I'm drawing a large image and the windows size is fine and scrollable, letting me see the entire image. But when I try to save the image to postscript, I only get the window view, not the entire canvas. Cannot find out how to proceed, any tips?
There might be an answer in How can I convert canvas content to an image? but I cannot see how to connect tkinter with turtle. Can someone elaborate on that?

Fastest way to draw a pixel on tkinter

Note: You really only need to read the checklist and understand that I want to do this in Tkinter, the rest of the information is for clarification
The complete code is here: https://gist.github.com/SnugBug/1cc5ea67d11487d69aae8549107372ef
I need to be able to manipulate pixels. The goal is to be able to:
Control which pixels are drawn first
Change the color and position of each pixel
Update everything as a whole, so that if a pixel changes the change shows up
Clear everything as a whole
The question is, what's the fastest way to check off this list in tkinter? I tried creating an image with PIL, then loading it into tkinter, but I cannot update the image or clear it. The other thing I tried is using tkinter's PhotoImage class, as shown below:
#The function definitions are in the GIST.
#This snippet should be enough information to understand the problem, however.
for i in range(0,3600):
rot = [0,i,0]
Tx,Ty,Tz,Zm = [0,0,200,200]
x,y,z = [10,10,10]
for m,n in itertools.product(range(-50,50,2),range(-50,50,2)):
x,y,z = rotate([m,n,0],rot)
img.put("#ffffff", (int(WIDTH/2 + ((x+Tx)*Zm/(z+Tz))), int(HEIGHT/2 - ((y+Ty)*Zm/(z+Tz)))))
canvas.update()
img.blank()
#the confusing math in the `img.put` call is just 3D math
This way is extremely slow. The next way I tried is even slower. It's drawing a line like this:
canvas.create_line(x,y,x+1,y+1, ...)
Which creates a line of length 1, showing a single pixel. This is excruciatingly slow.
If the separate image method is the fastest, could you include a working snippet in your answer? I cannot figure out the separate image method. I have PIL installed, that's what I was using to attempt it. I lost the python file so I cannot include the code I used to attempt the separate image method.
What I mean by the separate image method: create an image using PIL, drawing on it using PIL, then making that show up on a tkinter screen. This doesn't meet everything on the checklist, however (from what I understand).
If the separate image method is not the fastest, please tell me a way I can check off everything in the checklist some other way. I have a few questions I looked at for help and some sites. They're below
Why is Photoimage put slow?
Any of these answers don't work for me because it only creates squares. I need to be able to make any shape.
python tkinter: how to work with pixels?
This answer doesn't work because it's too slow.
How to load .bmp file into BitmapImage class Tkinter python This could be helpful
http://zetcode.com/gui/tkinter/drawing/ None of these methods work because I cannot manipulate the order each pixel is drawn, and the color of each individual pixel. If you are familiar with 3D terminology, I need this for a Z-Buffer
If there are any confusions or you need something clarified, please tell me below in the comment section. I am open minded, so if you have a deep understanding of my question and have another idea on how to solve it, I would love to hear it.
If you are using Windows, then the fastest way to put an image on a frame is by ImageWin. The tkinter process of first transforming from PIL image to a tkphotoimage is very slow.
from PIL import Image, ImageWin
from win32gui import GetDC
from tkinter import Tk
root = Tk()
im = Image.open(<file path>)
ImageWin.Dib(im).draw(
GetDC(ImageWin.HWND(root.winfo_id())),
(0,0,100,100)
)

the pillow make the image being more size than before

I use python pillow to do a easy gif image reverse,but I found that the image has become more size(10m) than before(1m). Anyone know how to make it?
And here is my code:
from PIL import Image, ImageSequence
from PIL import ImagePalette
with Image.open('sd.gif') as im:
if im.is_animated:
frames = [f.copy() for f in ImageSequence.Iterator(im)]
frames.reverse()
frames[0].save('out.gif', save_all=True, append_images = frames[1:])
I can't tell for sure without examining the actual images, but I can guess what likely happened:
Some gifs are optimized with a method that finds pixels in each frame where nothing is changing (or changing only very slightly) from frame to frame, and make them transparent instead of storing the color for each pixel, to reduce the amount of data. For some gifs with large static areas in many consecutive frames this can be very efficient way to reduce file size.
When you are reversing the GIF, the frames must be unoptimized first, otherwise there would be transparent areas without any data. This can increase file size quite a bit. The difference may vary from one image to another.
You probably can solve this by running some gif optimization algorithm after the new image is created.

How do I make a tiled background on a tkinter canvas?

I have a small image file that I would like to make as the tiled background image on a tkinter canvas in Python 3.4. I want the image to repeat indefinitely even if the canvas window is resized. How can I go about implementing this?
Looking around on the internet leads me to believe that there isn't any native tkinter feature for doing this simply. Is there any "black-box" method in an image library that can interface with tkinter for doing this tiling? Or should I manually render copies of the image at regularly spaced coordinates?

Categories

Resources