Can't draw shapes over imported image - python

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)

Related

I Want to create a Turtle animation for this image. How can I change the co-ordinates?

I tried to create a turtle animation of the Banner in the picture but I can't get it right. I know how to create trapezium but after that, I don't know how to connect other parts and also don't know how to draw within the circle.
I tried creating trapezium but after that I don't know anything. I am beginner programmer.

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

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!

How to create circle and triangle with specific colour using turtle in python?

import turtle
import math
import random
import os`
player = turtle.Turtle()
player.color("yellow")
player.shape("triangle")
player.penup()
player.speed(0)`
maxGoals = 10
goals = []
for count in range(maxGoals):
goals.append(turtle.Turtle())
goals[count].color("red")
goals[count].shape("circle")
goals[count].penup()
goals[count].speed(0)
goals[count].setposition(random.randint(-290,290),random.randint(-290,290))`
When run this code i am getting only the circle's outline in Red color and the triangle's outline in Yellow color
I need the circle filled with red colorand the triangle filled with Blue color. I have included the image below:
Kindly help me to fix this issue
I ran the code provided by you and it is working just fine. Please try the same code on the different machine and provide details so that if this is a system specific problem, I'll be able to help you if you provide moreinformation.
I ran it using a mac, python 2.7
joginder
This issue has come up several times on SO, but unfortunately, I can only locate one this morning: Turtle will not draw angled lines
The answer always seems to be that it's a graphics driver issue. Once corrected, things start working as expected. Sorry I can't be more specific but it depends on your system's setup.

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.

pygame.gfxdraw.aacircle gows funny for big circles, any alternatives?

Working on my Major project for software design and development and have run into the hurdle that when using pygame.gfxdraw.aacircle to draw big circles, the output goes screwy as seen here
the window in the picture is showing a section of a circle with a radius of size 1561
if no-one can suggest a fix or alternate way of drawing aa circles i will probably just use the regular circle function as it doesn't look to bad at sch a large radius.
I would suggest not to use pygame.gfxdraw, since it is clearly marked as experimental.
From the documentation:
EXPERIMENTAL!: meaning this api may change, or dissapear in later
pygame releases. If you use this, your
code will break with the next pygame
release.
Just stick with the regular circle function.
EDIT:
Maybe you should open a bugreport on the pygame bugtracker or the pygame mailinglist.

Categories

Resources