I am working on a new project that relies on Turtle's ability to import images. However, I have found that if I import and draw the image as a shape, nothing else can be drawn on top of it. How should this be adjusted so that Turtle can draw over the image(s)?
import turtle
def insert_img(image):
turtle.Screen().register_shape(str(image))
turtle.Turtle().shape(str(image))
turtle.speed(0)
insert_img('The Image.png')
turtle.pendown()
turtle.goto(150,-150)
Edit: I didn't find a direct solution to the issue, but I found a cheaty workaround that works around for me. Using multiple turtles and replacing the cursors with different images allows them to be utilized in a similar fashion to what I'm looking for. If anyone can find a better solution, great! Feel free to share it.
import turtle
turtle.Screen().addshape("The Image.png")
the_image = turtle.Turtle()
the_image.shape("The Image.png")
the_image.speed(0)
the_image.pendown()
the_image.goto(150,-150)
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?
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)
)
I'm making a lo-fi, low-resolution (1024x576) game and I was hoping I could get away with just doing supersampling (render the game at 2048x1152 then scale down) instead of proper anti-aliasing.
Trouble is, I don't see any way to render the OpenGL commands to a memory surface instead of the display surface. Is there a way?
Using https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit you can do image.blit(image2) and use any special flags you want with that
Do not forget: Display objects inherit from Surfaces. So you can blit the screen to another Surface and scale it down! Create a subprocess using the corresponding module, initialize Display with the dummy video driver (as seen in the headless_no_windows_needed.py Pygame example), send the Surface converted to a simple list using PixelArray, through IPC, recieve it in the main process, and blit it to a Display without OPENGL flag. You can also use FBOs.
I'm preparing exercises for school classes involving Python's turtle library.
The students are already drawing terrific pictures, but I want them to be able to detect existing pictures and colours in order to modify the behaviour of their program.
For example I would like to provide them with code which draws a maze using turtle, and then they can write the code to navigate the turtle around the maze (don't worry, I'll start simpler).
Is there a way to detect the colour of the pixels already drawn by the turtle?
Thanks!
Turtle uses Tkinter canvas, which you can get using turtle.getcanvas(), and according to this you cannot read the colour of a pixel without using a workaround of converting the canvas to a picture (bitmap) and read the bitmap.
You could try to keep an open array to work as the bitmap of your canvas and update it yourself as you draw new elements on the canvas, although that seems impractical unless the maze is simple and 'squary'.
I would use an array keep all x and y that is used for the maze in an array like stated above. Then have a size of a box around the turtle defined for detecting purposes.