How to change size of turtle with custom shape? [duplicate] - python

I'm using turtle to make a little game and realized I could use image files with turtle.registershape(filename). I know you can resize the default shapes with turtle.shapesize or turtle.resizemode("auto") and changing pensize, but is there any way to resize a gif file using these methods?
import turtle
turtle.addshape("example.gif")
t = turtle.Turtle()
t.shape("example.gif")
t.resizemode("auto")
t.pensize(24)
t.stamp()
turtle.exitonclick()
I want something like this to work, but the turtle is displayed normally, not resized.

I reviewed the applicable turtle code and I believe the answer is, "No, not within turtle itself."
Bringing in tkinter, which underlies turtle, gives us some limited (integral) turtle image expansion and reduction capability:
from tkinter import PhotoImage
from turtle import Turtle, Screen, Shape
screen = Screen()
# substitute 'subsample' for 'zoom' if you want to go smaller:
larger = PhotoImage(file="example.gif").zoom(2, 2)
screen.addshape("larger", Shape("image", larger))
tortoise = Turtle("larger")
tortoise.stamp()
tortoise.hideturtle()
screen.exitonclick()
If you want more flexibility, the standard approach seems to be to either resize the graphic outside of turtle/tkinter or use the PIL module to resize the graphic dynamically and hand it to turtle/tkinter.

The answer is no. I went through the source code of cpython library and tracked to the draw image method call. See _drawturtle method of cpython library, where drawImage method is called with no information of variables like pensize or shapesize. I agree with #cdlane that you will have to use other resize using other libraries, however one tip, if you know beforehand the set of image sizes you will need, you can generate them and load during the game start instead of resizing at the runtime.

No, there is no way to resize gif's shape via turtle explicitly. However, you can resize the shape manually using this website: GIF RESIZER
Further, use the optimized gif (length & width wise) in your code. I also use this approach and is very helpful.
Thanks!

Related

Can't draw shapes over imported image

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)

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)
)

Using Pygame + PyOpenGL, draw to a Surface instead of straight to the display?

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.

How to read pixel colours using Python turtle.py

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.

Categories

Resources