How to read pixel colours using Python turtle.py - python

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.

Related

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 make an invisible sprite in pygame? (python)

to elaborate, I'm currently creating a maze layout using wall sprites. however, I want the maze to be invisible when I am actually playing the game, while still able to be able to have collision detection. Is this possible in any way? Thanks.
Are you using images to show your walls? Because if you are, you can use a program called paint.net to create a transparent sprite. Just create a file with the desired dimensions and use the eraser tool to make the whole thing transparent (click on the little squiggly arrow up at the top to make it easier). Save it as a png in your pygame folder and you have a transparent square.

Simple animations in Python 2.7 [duplicate]

This question already has answers here:
simple animation using tkinter
(2 answers)
Closed 6 years ago.
In Python 2.7, I want to display some simple graphics, such as a red square moving around on a blue window. On every frame, I want to update the position of the square, and render the new window. I am looking for something simple and lightweight.
I have seen people use matplotlib for drawing shapes, but I don't want to have to deal with axes and data points. I have also seen pygame suggested, but this seems to heavyweight for what I want, as I do not want to create a game, just a simple animation.
So what I really want is something where on every frame, I just indicate the colour of every pixel on an image, and then display that image. What are some good suggestions?
Tkinter is not good for setting individual pixels. If you want to move rectangles or ovals though (a small oval will look like a pixel, but it doesn't scale for updating a whole image).
def update(x,y):
canvas.delete('all')
canvas.create_rectangle(x-1,y-1,x+1,y+1)
You can, of course, be more judicious, saving the return value of the rectangle and then only clear the appropriate elements. Or you can move existing elements directly as Bryan points out. As he explains elsewhere Tkinter of course, supports drawing images, ovals, and a slew of other things. Here's a canonical source edit: that is old and not canoncial This one's slightly better For a general source on animating with a timer loop, here's Bryan agian
Bryan also noted that you can work with pixels directly You can do that with PhotoImage.
Array-Like Pixel Access Without Graphical Extensions
A robust module like pygame will be the most scalable option. However, I've had success (in educational settings only) writing graphics engines by modifying the elements of a numpy array and then displaying it as an image (you also need this link to display the images).
This lets you do pixel level modifications; since it's relatively trivial to write C-extensions that modify numpy arrays, you can prototype fast image processing doing custom manipulations. While I've written whole graphics engines using just tkinter this way, again I can only reccomend it for educational purposes.
Otherwise, just bite the bullet and pull in openGl or pygame. You'll save yourself a ton of time in the long run.
Summary
Very simple animations can be done right in tkinter
For educational purposes, you can do arbitrary graphics with numpy and tkinter
For rhobust animations, check out a full library (openGl, matplotlib, pygame) that suits your needs (graphical rendering, statistical graphing, game development, etc.)

GTK drawable area transparent background color

I have a GTK drawing area and I want to have an image display as the background for it, while other things can be drawn over it.
My first attempt at this involved me simply taking the image, putting it into a pixmap, and drawing it before I draw other objects. This resulted in the objects backgrounds completely covering my background image.
Now I am thinking I need to change the object's pixmap's gtk.gdk.GC so the background color it has is transparent. Here is where I am having problems. I do not know how to set a gtk.gdk.Color's alpha.
How do I set a gtk.gdk.Color's alpha? (or any other way of making the color transparent)
If this idea seems unlikely to work please let me know. I am pretty new to GTK.
I don't know if this can help you, but my best advice is to use Cairo: an open source 2D graphics library providing cross-platform support for advanced 2D drawing. It is also the easiest way to draw 2D graphics, I think
(I've tried GDK in the past and I thought it was much more tricky to use).
In python you can use the specific bindings Pycairo.
Here you can learn more about it, with easy tutorials.
For your specific problem, see more about images
and trasparency.
The main function for trasparency are:
set_source_rgba(red, green, blue[, alpha=1.0])
paint_with_alpha(alpha)
A great summary to learn how to manage images and transparency is in the example Reflected image in the same page linked before about trasparency.

Drawing a polygon in pygame [duplicate]

This question already has an answer here:
Drawing polygons in pygame using list
(1 answer)
Closed last month.
i am jaison i like to build an application for land survey process. for that i need to plot points in a canvas for a given gsi file. for example
the points be
a. .b
c. .d .e
these are the 5 points and i need to develop a tool to connect these points by line. while closing a boundry by like connecting points acdba. give it a parcel_id and other details like land owner, tax payment etc. save these details for feature query.
in real time the points will be more than 100000. and i need the canvas should have pan and zoom property.
friends i like to do this project in python using pygame. is it possible to do.
i am a newbie to python.
please need help.
Pygame is absolutely a good tool for this.
Look into using a pygame.surface object, and the pygame.draw module.
http://www.pygame.org/docs/ref/
If you need anything more complicated than dots, pygame.sprite is a relatively well developed module as well.
http://www.pygame.org/docs/ref/draw.html#pygame.draw.polygon
This is the function for drawing polygons in pygame. You can draw straight to screen or to a separate surface. I am not sure how fast it is with lots of points, you might want to split up what your drawing into smaller segments and just display what you need.
If you do need to show the whole polygon I would look into some way of compressing the amount of points to only get the general shape

Categories

Resources