I try to solve error for this code. I'm using mac os
file is in right directory and use gif file.
but I kept getting error. How can I solve this(please help me)
import turtle
import random
screen=turtle.Screen()
image1="/Users/jameslee/Downloads/front.gif"
image2="/Users/jameslee/Downloads/back.gif"
screen.addshape(image1)
screen.addshape(image2)
t1=turtle.Turtle()
coin=random.randint(0, 1)
if coin==0:
t1.shape(image1)
t1.stamp()
else :
t1.shape(image2)
t1.stamp()
Your code is fine. I've simplified it below for my testing purposes. The next issue is the *.gif files themselves. Either they are variants of GIF that tkinter doesn't recognize, or they aren't GIF files (e.g. something else renamed with a *.gif extension.) Since you say you're on Mac OSX, we can test this. Run /Applications/Utilities/Terminal.app, cd to the directory in question and run the Unix file command:
> cd /Users/jameslee/Downloads
> file front.gif
front.gif: GIF image data, version 89a, 50 x 50
>
Your output should be similar -- let us know what you get. Your code simplified:
from turtle import Screen, Turtle
from random import choice
image1 = "/Users/jameslee/Downloads/front.gif"
image2 = "/Users/jameslee/Downloads/back.gif"
screen = Screen()
screen.addshape(image1)
screen.addshape(image2)
turtle = Turtle()
turtle.shape(choice([image1, image2]))
turtle.stamp()
screen.exitonclick()
Related
I'm not able to add in images to my programme in Pycharm, I've imported Pygame and os and it wouldn't work. The image is in .png format 64 bit from flaticon.com
Is there something else I need to do to be able to add it in, I was following a PyGame tutorial online since I'm a beginner in programming
The python launcher just glitches when I add anything to do with an image. everything else works perfectly fine.
Here is the entire code of the project so far:
import pygame
import os
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Space Invaders - GAME ONE (PyGame)")
playerImg = pygame.image.load('space-invaders.png')
playerX = 370
playerY = 480
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
pygame.display.update()
this is the error message:
playerImg = pygame.image.load('space-invaders.png')
pygame.error: Couldn't open space-invaders.png
Process finished with exit code 1
it just glitches with any addition of an image. If I remove all image related code, it works fine
Also, import os just greys out in the editor
First check if the image is in the same folder as your .py. Next, make sure the image is really .png not just renamed for example, check this by trying to load another image. Last but not the least, check the spelling, maybe the name of the picture is misspelled. Since you are not using paths I can't remember anything else that could cause the issue.
Also, I do not know if it is the mistake during the copy process but last 2 lines of code need to be inside the for loop. And why you have imported os?
EDIT: Answer to the OP question in the comment
By importing os you can now use this module, nothing more. Here is how you can locate your .png file:
current_dir = os.path.dirname(__file__) #The location of your `.py`
img_dir = os.path.join(current_dir, "img_dir") #Path to the folder where your `png` is
player_img = pygame.image.load(os.path.join(img_dir, "space-invaders.png"))
Instead of "img_dir" you need to put the name of your folder, or the path, depending on the folder location.
I have two pictures "gif" and want to insert them as small pictures and add them to Python code. I want the picture to stay in an accurate location as well decrease it size. When i try to run this code the shell shows an error "screen isn't defined"
import turtle
import time
from tkinter import *
screen=turtle.Turtle()
screen=turtle.getscreen()
screen.register_shape("health.gif")
screen.penup()
screen.shape("health.gif")
screen.goto(x+50,y+150)
I don't know why you're getting that error... but you have a mixup with your variable names... you create a variable you call screen that is turtle.Turtle() but then you overwrite that variable by doing turtle.getscreen()
Doing:
import turtle
t=turtle.Turtle()
screen=t.getscreen()
screen.register_shape("health.gif")
t.penup()
t.shape("health.gif")
Draws a health.gif image (if that happens to be in your working directory)
Can someone help me load an image? It says "error: can't open tux.jpg:
import sys, pygame
pygame.init()
size = width, height = 600,400
screen = pygame.display.set_mode(size)
tux = pygame.image.load("tux.jpg")
screen.blit(tux,(200,200)) #Displays Tux On Screen
pygame.display.flip()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:sys.exit()
Please check that you have the image file within the directory that you are working in.
Using an absolute path may also be an option, for example:
tux = pygame.image.load("C:\\path\\to\\game\\tux.jpg")
For more information, please see this answer here.
The path you entered comes out as "./tux.png", i.e. the current working directory. Either place the file at the same location as your .py file (hence the default working directory for the script) or define the path to your image file.
For game file ordering, images are often in separate directories to the game scripts, The best way to do this is the os module. os.getcwd() gives the current working directory, and can be modified to the image directory using os.path.join. e.g.
game/
game.py
images/
tux.jgp
game.py uses pygame.load(os.path.join(os.getcwd(), "images")
(or define a datapath variable at the top in the same way if using lots of images!)
IMPORTANT i did not notice that there were two different places where i had to change the settings from Qt4 to SVG. I changed them both and the problem regarding the "no Turtle found" was solved. I need to thank Jonathan March, who turned me in the right direction with the link he suggested.
PROBLEM SOVED!!
I'm using Canopy 1.3.0.1715 (32bit) on a MacBook Pro 64bit OS 10.9.2.
When i try to use
from turtle import Turtle
Canopy says
name 'Turtle' is not defined
Here is my code, named draw.py (i want to draw a square):
from turtle import Turtle
t = Turtle()
def drawsquare(t, x, y, side):
t.up()
t.goto(x,y)
t.setheading(270)
t.down()
for count in range(4):
t.forward(side)
t.left(90)
I also created a file turtle.cfg like this
width = 300
height = 200
using_IDLE = True
colormode = 255
Please be as simple as you can, i just recently started using Python. Thanks everyone.
from turtle import Turtle
works for me on Canopy-32 bit on Mac64 (running in the Canopy python shell).
First thing to check: did you name some file turtle.py? If so, rename the file, delete the file turtle.pyc in the same directory if it exists, and try again. (If you name your file turtle.py, then python has no way to find the standard turtle module.)
Otherwise:
Where are you running this? In the Canopy python (ipython) shell?
Or did you start up Python some other way?
Wherever it is, what do you see when you type this?:
import sys, turtle
print sys.prefix
print turtle.__file__
Also, while this should not account for your import failure, be sure to read and follow the following:
https://support.enthought.com/entries/21793229-Using-Tkinter-Turtle-in-Canopy-s-IPython-panel
I would like to figure out how to save a bitmap or vector graphics image after creating a drawing with python's turtle module. After a bit of googling I can't find an easy answer. I did find a module called canvas2svg, but I'm very new to python and I don't know how to install the module. Is there some built in way to save images of the turtle canvas? If not where do I put custom modules for python on an Ubuntu machine?
from tkinter import * # Python 3
#from Tkinter import * # Python 2
import turtle
turtle.forward(100)
ts = turtle.getscreen()
ts.getcanvas().postscript(file="duck.eps")
This will help you; I had the same problem, I Googled it, but solved it by reading the source of the turtle module.
The canvas (tkinter) object has the postscript function; you can use it.
The turtle module has "getscreen" which gives you the "turtle screen" which gives you the Tiknter canvas in which the turtle is drawing.
This will save you in encapsulated PostScript format, so you can use it in GIMP for sure but there are other viewers too. Or, you can Google how to make a .gif from this. You can use the free and open source Inkscape application to view .eps files as well, and then save them to vector or bitmap image files.
I wrote the svg-turtle package that supports the standard Turtle interface from Python, and writes an SVG file using the svgwrite module. Install it with pip install svg-turtle, and then call it like this:
from svg_turtle import SvgTurtle
def draw_spiral(t):
t.fillcolor('blue')
t.begin_fill()
for i in range(20):
d = 50 + i*i*1.5
t.pencolor(0, 0.05*i, 0)
t.width(i)
t.forward(d)
t.right(144)
t.end_fill()
def write_file(draw_func, filename, width, height):
t = SvgTurtle(width, height)
draw_func(t)
t.save_as(filename)
def main():
write_file(draw_spiral, 'example.svg', 500, 500)
print('Done.')
if __name__ == '__main__':
main()
The canvasvg package is another option. After you run some turtle code, it will convert all the items on the tkinter canvas into an SVG file. This requires tkinter support and a display, where svg-turtle doesn't.