how to draw lines on a picture background in pygame

I would like to draw lines (of arbitrary position and length) onto a surface in pygame, which itself is an image loaded from a file on disk.

Can anyone point me to some example code that does this?


Asked by: Anna576 | Posted: 05-10-2021






Answer 1

This should do what you're asking for:

# load the image
image = pygame.image.load("some_image.png")

# draw a yellow line on the image
pygame.draw.line(image, (255, 255, 0), (0, 0), (100, 100))

Typically you don't draw to the original image, since you'll have to reload the image to get the original back (or create a copy of it before you start drawing onto it). Perhaps what you actually need is something more like this:

# initialize pygame and screen
import pygame
pygame.init()
screen = pygame.display.set_mode((720, 576))

# Draw the image to the screen
screen.blit(image, (0, 0))

# Draw a line on top of the image on the screen
pygame.draw.line(screen, (255, 255, 255), (0, 0), (50, 50))

Answered by: Lucas935 | Posted: 06-11-2021



Answer 2

Help on module pygame.draw in pygame:

NAME pygame.draw - pygame module for drawing shapes

FILE d:\program files\python25\lib\site-packages\pygame\draw.pyd

FUNCTIONS aaline(...) pygame.draw.aaline(Surface, color, startpos, endpos, blend=1): return Rect draw fine antialiased lines

aalines(...)
    pygame.draw.aalines(Surface, color, closed, pointlist, blend=1): return Rect

arc(...)
    pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1): return Rect
    draw a partial section of an ellipse

circle(...)
    pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect
    draw a circle around a point

ellipse(...)
    pygame.draw.ellipse(Surface, color, Rect, width=0): return Rect
    draw a round shape inside a rectangle

line(...)
    pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect
    draw a straight line segment

lines(...)
    pygame.draw.lines(Surface, color, closed, pointlist, width=1): return Rect
    draw multiple contiguous line segments

polygon(...)
    pygame.draw.polygon(Surface, color, pointlist, width=0): return Rect
    draw a shape with any number of sides

rect(...)
    pygame.draw.rect(Surface, color, Rect, width=0): return Rect
    draw a rectangle shape

Answered by: Rafael337 | Posted: 06-11-2021



Similar questions

Set picture as background in python in turtle module

I am a beginner at programming at can't seem to figure out how to use bgpic('photo.gif') in turtle. I have enclosed the 'photo.gif' in the same folder as the script. Every time I execute the program with bgpic, the turtle window crashes. Do I have to resize the picture? Change its format?


python - Background picture in pyqt4 qlabel

I modified this simple countdown timer form here: https://github.com/lunaryorn/snippets/blob/master/qt4/countdown.py as follows. Now I want to have a background image behind the countdown timer. Any ideas how to do this? #!/usr/bin/python import sys from PyQt4.QtCore import Qt, QTime, QTimer from PyQt4.QtGui im...


python - Insert a Picture as the Background of a Figure?

I wish to draw some pedestrian trajectories given the coordinates of every point on the trajectories. This can be easily done using matplotlib. But in my case, the trajectories mean nothing if they are simply drawn over a blank background. They will only make sense when I overlay them on the real floor map such as the one below.


python - how to change labels background as same picture

I started to learn Python one month ago. Sorry if my question is not good, this is my first question here. I have made a small game using tkinter, but I have a problem. I made a big label with a picture on it as background. Whenever I make more labels with text on them, the text will have gray background. However what I want is for every text to have the picture that I already placed as backgro...


How do I crop an image using a binary mask image of the same picture to remove the background in python?

I have tried to get the edge of the mask image with the following code: import numpy as np from matplotlib import pyplot as plt img = cv2.imread('ISIC_0000000_segmentation.png',0) edges = cv2.Canny(img,0,255) plt.subplot(121), plt.imshow(img, cmap='gray') plt.title('Original Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(edges, cmap='gray') plt.title('Edge Image'), plt.xticks([]), pl...


python - Paste RBG image without its background onto another picture (PIL)

So I need to post an RGB picture (with transparent background) onto background image, but when I am trying to post it with image.paste() I get transparent background through which I don't see the background I want. What should I do? Thanks! Code (its just that): skin = Image.open("./temp2.png") skin = skin.resize((148, 355)) stats.paste(skin,(42,232)) ...


python - How how to create a background from a picture on the screen in kivy?

I have a code, but when it executed it gives a black screen. Tell me what's wrong with it, please. I try to make a background from a picture on the screen in kivy. Here is my .py file: from kivy.app import App from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition from kivy.uix.floatlayout import FloatLayout from kivy.lang import Builder from kivy.graphics import Color, Rectangle from kivy....


Set picture as background in python in turtle module

I am a beginner at programming at can't seem to figure out how to use bgpic('photo.gif') in turtle. I have enclosed the 'photo.gif' in the same folder as the script. Every time I execute the program with bgpic, the turtle window crashes. Do I have to resize the picture? Change its format?


python - Background picture in pyqt4 qlabel

I modified this simple countdown timer form here: https://github.com/lunaryorn/snippets/blob/master/qt4/countdown.py as follows. Now I want to have a background image behind the countdown timer. Any ideas how to do this? #!/usr/bin/python import sys from PyQt4.QtCore import Qt, QTime, QTimer from PyQt4.QtGui im...


python - Insert a Picture as the Background of a Figure?

I wish to draw some pedestrian trajectories given the coordinates of every point on the trajectories. This can be easily done using matplotlib. But in my case, the trajectories mean nothing if they are simply drawn over a blank background. They will only make sense when I overlay them on the real floor map such as the one below.


python - how to change labels background as same picture

I started to learn Python one month ago. Sorry if my question is not good, this is my first question here. I have made a small game using tkinter, but I have a problem. I made a big label with a picture on it as background. Whenever I make more labels with text on them, the text will have gray background. However what I want is for every text to have the picture that I already placed as backgro...


How can I set a background picture on my window in Tkinter in Python?

This is the code setting the Background: ''' Background picture: ''' background_image=tk.PhotoImage("blood_PNG6140.png") background_label = tk.Label(parent, image=background_image) background_label.place(x=0, y=0, relwidth=1, relheight=1) I am getting this Error: Traceback (most recent call last): File "/Users/Lana/Desktop/Zaid/End_Projekt_MakE_YouR_ChoicE_zaid_zaim ON ...


How do I crop an image using a binary mask image of the same picture to remove the background in python?

I have tried to get the edge of the mask image with the following code: import numpy as np from matplotlib import pyplot as plt img = cv2.imread('ISIC_0000000_segmentation.png',0) edges = cv2.Canny(img,0,255) plt.subplot(121), plt.imshow(img, cmap='gray') plt.title('Original Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(edges, cmap='gray') plt.title('Edge Image'), plt.xticks([]), pl...


opencv - Remove the background from a picture using python

I am trying to remove the backgrounds of a set of tea leaf images. I tried these codes in StackOverflow itself but they don't work with my images. How to remove the background from a picture in OpenCV python


python - Paste RBG image without its background onto another picture (PIL)

So I need to post an RGB picture (with transparent background) onto background image, but when I am trying to post it with image.paste() I get transparent background through which I don't see the background I want. What should I do? Thanks! Code (its just that): skin = Image.open("./temp2.png") skin = skin.resize((148, 355)) stats.paste(skin,(42,232)) ...


python - How how to create a background from a picture on the screen in kivy?

I have a code, but when it executed it gives a black screen. Tell me what's wrong with it, please. I try to make a background from a picture on the screen in kivy. Here is my .py file: from kivy.app import App from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition from kivy.uix.floatlayout import FloatLayout from kivy.lang import Builder from kivy.graphics import Color, Rectangle from kivy....


python - Make my code handle in the background function calls that take a long time to finish

Certain functions in my code take a long time to return. I don't need the return value and I'd like to execute the next lines of code in the script before the slow function returns. More precisely, the functions send out commands via USB to another system (via a C++ library with SWIG) and once the other system has completed the task, it returns an "OK" value. I have reproduced the problem in the following example. How can ...


python - How can I launch a background process in Pylons?

I am trying to write an application that will allow a user to launch a fairly long-running process (5-30 seconds). It should then allow the user to check the output of the process as it is generated. The output will only be needed for the user's current session so nothing needs to be stored long-term. I have two questions regarding how to accomplish this while taking advantage of the Pylons framework: What...


python - In GTK on Windows, how do I change the background color of the whole app?

According to http://www.pygtk.org/docs/pygtk/gtk-constants.html, there are five state types: STATE_NORMAL, STATE_INSENSITIVE, etc. I want to set the background color of a Table, HBox, VBox, whatever, and I've tried setting every possible color of every kind of state: style = self.get_styl...


c# - Coming from a Visual Studio background, what do you recommend I use to start my VERY FIRST Python project?


python - How to launch and run external script in background?

This question already has answers here:


python - wxPython : Problem updating background color of controls

[EDIT - Reduced and re-posted code, restated question] I would like to change the background color of a frame (or panel; whichever makes it work). The problem is that the background color of controls on that frame (or panel) don't have their background color updated until I click on the control (the slider control, specifically). Any ideas on how to fix this? Here's some demo code: i...


python - Pygame: Blitting a moving background creates too much blur

What I am trying to do is create a viewport to view a small portion of a background. (And later put sprites in). However the problem I have noticed is there seems to be an issue of the background blurring when it starts moving. I was not sure if this is because blitting is slow or because of a problem in the code. I was looking for examples on how others blit or create scrolling backgrounds and found this article: ...


bash - Executing python script in background in init.d

to interact with my iPhone, i have created a python script that sends and recives data through a socket, the script must be started after emule in order to work, i have thought of something like this: PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/local/bin/amuled WEB=/usr/local/bin/amuleweb NAME=amuled DESC=amuled RUNAMULE=no USER=piros # ADDED FOR iPhone SOCKET= /home/piros/...


python - How do I launch background jobs w/ paramiko?

Here is my scenario: I am trying to automate some tasks using Paramiko. The tasks need to be started in this order (using the notation (host, task)): (A, 1), (B, 2), (C, 2), (A,3), (B,3) -- essentially starting servers and clients for some testing in the correct order. Further, because in the tests networking may get mucked up, and because I need some of the output from the tests, I would like to just redirect output to a ...


How to launch a background process in Python with a delayed invocation?

Ok, this is a bit of a thorny problem. I need to launch a backgrounded process that will (1) wait N secs, and then (2) execute some command. Additionally, I need to capture the pid of the background process itself, because when the parent process finishes it will kill the backgrounded process if necessary. It looks a bit like this in shell syntax: (sleep 15 && run_some_cmd) & # launch bg proce...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top