adding an image to the Turtle Screen - python

How can I add image to my Turtle Screen using turtle graphics?
whenever I use the function addshape I keep getting errors.
does turtle graphics got any other way loading/importing images?
for example:
import turtle
screen = turtle.Screen()
image = r"C:\Users\myUser\Desktop\Python\rocketship.png"
screen.addshape(image)
turtle.shape(image)

The turtle module does have support for images, but only GIF images, not PNG or any other format. As the docs for addshape say:
name is the name of a gif-file and shape is None: Install the corresponding image shape.
And if you look at the source, they're serious about "gif-file": the way it decides whether you're trying to add an image or a polygon is by calling data.lower().endswith(".gif"), which obviously won't work for .png files.
And, even if you fix that, it will still only be able to handle the file formats that Tkinter supports out of the box, which includes some extra things like PPM/PGM/PBM, but still not PNG. If you want to support PNG files, you'll want to install Pillow as well.
At this point, you're getting beyond what people usually do with turtle. That might be worth pursuing (you'll learn a lot by doing so), but it may be simpler to use an image-converting program to convert the .png file to a .gif file so it will work with your existing code.

You can only use gif files with Python Turtle. Take any picture and convert/resize for free at ezgif.com.
Background:
win = turtle.Screen()
win.bgpic('background.gif')
Using shapes:
win.register_shape('pic1.gif')
sprite = turtle.Turtle()
sprite.shape('pic1.gif')
Example game:
https://www.youtube.com/watch?v=q49Xyo0LYDs

Related

HOW i can generate cubes random in pygame?

i need code
How can a randomly generated 3dcube using just pygame be created from the top of the screen and then fall from the top to the bottom of the screen
please help me
using python pygame
As mentioned by #The_spider , pygame is a 2D Game engine. It cannot load 3D files or do any 3D stuff. A few recommended modules would be Panda3d and Ursina(which is a simplified version of Panda3d) install them via pip.
If you really need to use pygame, you'll have to save an image of the cube at every angle and Blit it on the screen using an extremely complicated algorithm. Moreover, it will be extremely slow. Thus, use a python 3D game engine.

Is there any way to resize a gif shape with turtle in 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!

Using Pillow/PIL with Pygame

I'm currently trying to use a .png of a Tic Tac Toe board in Pygame so that the white part is transparent. However, I'm getting "libpng warning: sBIT: invalid" and the white is showing when I blit the board onto the background. The code is as follows:
background = pygame.image.load("blah.bmp")
board_surface = pygame.image.load("board.png")
board_surface.convert_alpha()
I read on the Pygame docs that only .bmp is guaranteed to work, and since image.get_extended() is returning a 1, I'm assuming that I simply don't have enough image processing support.
I've installed Pillows and imported the module, but I have no idea how to use this with Pygame. The tutorial shows how to load images as Pillow's Image class and so forth, but I need to represent images as Pygame's Surfaces.
I would appreciate any help with integrating these two things, or any other solutions on how to get my .png to work! For reference, I'm using Python3.3 and Pygame1.9.2
I have encountered this problem too. I solved it by switching to .tif (only with one f !). If you have Gimp installed use that to turn the white spaces into alpha=0 spaces (if you have problems doing that I can help with a more detailed explanation). After that import the image just as normal and use the .convert_alpha() method.

draw rotated font with pygame

What's the best way to draw rotated text rendered with fonts with pygame? I can just draw the font then rotozoom it, but it seems better result would be gotten if it were possible to draw the glyphs directly rotated, especially taking AA into account?
Pygame does not have the functionality you are looking for.
You will have to use the method you mention, i.e rotozoom on an the already rendered font. The quality of this method isn't all that bad actually. And concerning speed, you don't want to be rendering the fonts all the time anyway.
If you are desperate for speed, and the text isn't dynamic, you can always make a cache of i.e 32 directions for each text you want displayed. Pyglet will allow you to rotate sprites for "free", i.e hardware accelerated.
Or you can go look at the raw SDL libraries pygame uses and see if you can find something that draws rotated fonts directly and hack pygame to use it.
You can use a pyOpenGL surface in pygame, if that simplifies it for you.
Pyglet also uses OpenGL.
(for pygame surfaces) You can cache the rendered text, using that to rotate. Or pre-cache all 32 directions. [ see other answer ]

Recommendations for a simple 2D graphics python library that can output to screen and pdf?

I'm looking for an easy-to-use graphics lib for python that can output to screen as well as pdf. So, I would use code to draw some stuff (simple prims like ovals, rectangles, lines and points) to screen and then when things look good, have it output to pdf.
If you use Tkinter, you can draw on a Canvas widget, then use its .postscript method to save the contents as a PostScript file, which you can convert to PDF using ps2pdf.
postscript(self, cnf={}, **kw)
Print the contents of the canvas to a postscript
file. Valid options: colormap, colormode, file, fontmap,
height, pageanchor, pageheight, pagewidth, pagex, pagey,
rotate, witdh, x, y.
Matplotlib should be able to do it. See event handling here: http://matplotlib.sourceforge.net/examples/event_handling/index.html
You can use the Python Imaging Library for drawing images which can easily be displayed in various UIs, e.g. by displaying a jpg. Then, use ReportLab. Here's an example which shows how to use ReportLab with an image.
I'm not sure what you mean by drawing to "screen", i.e. if you're working with a specific UI toolkit. But if it's acceptable to draw and display PDFs without using an intermediate image (jpg, etc), then you might consider the PyX library, which makes it quite simple to do graphics with PDFs.
You could look into matplotlib, which is mainly for plotting but you could probably do some basic drawing.
Then there is pygame. But I'm not so sure if it can generate a pdf, however you can do 2D graphics with it.
There is something called ReportLab that can generate pdf's. Here is a bunch of tutorials using it.
This is a tricky question, because there are so many libraries available - there is a trade-off between beauty/easiness.
What I've done and works great is to produce the Postscript directly, it is not difficult at all, and you can preview it using Ghostview; converting tyo PDF is trivial (ps2pdf). Learning how to tell Postscript to create lines and circles is extremely simple.
If you want more extensibility, then go to Matplotlib, but beware of the many times when it will "decide for you what looks best" even if you don't like it.
Good luck.
Creating PDFs is always a pain, it doesn't make sense if you do not aim to lose sanity.
With that said, you are aiming to do two completely different things: when you draw to screen you draw into a raster bitmap, while PDFs are mostly dynamic, like HTML. (unlike HTML they are more prone to be the same over different platforms, but that's beside the point)
If you really want to do that, the solution might be finding something that outputs PDFs, and then showing the generated PDF on screen at every step.
I guess that's the only way to have WYSIWYG results.

Categories