中的简单pygame用户

2024-09-29 06:29:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试创建一个脚本,允许用户在一个图像目录中循环,并为每个图像标记关键点(左键单击标记绿色圆圈,右键单击标记红色圆圈)。在

用于创建一个用于创建数据库的机器学习脚本。在每个映像的每个循环结束时,我希望将文件名和关键点位置附加到一个列表中,稍后将对其进行pickle和引用。在

我的问题是我可以为一个图像创建这个(见下文),但似乎不能使它为一个图像列表工作。在

提前感谢您的支持!在

### Import Modules 
import pygame
import os


### Functions

# Load image
_image_library = {}
def get_image(path):
    global _image_library
    image = _image_library.get(path)
    if image == None:
        canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep)
        image = pygame.image.load(canonicalized_path)
        _image_library[path] = image
    return image


##############
### Script ###

pygame.init()
screen = pygame.display.set_mode((1200, 700))
done = False
clock = pygame.time.Clock()

## Initializations

# Colors
color_blue = (143,184,222)
color_teal = (68,161,160)
color_red = (200,0,0)
color_grey = (26,47,42)

# Standard initailizations
point_confirmed = True
active_color = color_teal
margin = 5
kpi_radius = 5
x, y = 30, -250 #starting location 
fontsize = 36
font = 'helvetica' if 'helvetica' in pygame.font.get_fonts() else pygame.font.Font(None, fontsize) # Instantiate system's default font

# Image loop initialization
active_text = font.render("Image Loaded", True, color_grey)
image_flagged = False
kpi_primary = (0,0)
kpi_secondary = (0,0)

image = get_image('/Users/omeed/Dropbox/Spottr/Scripts/Data/4 AP JPG_1001_0.jpg') 

while not done:

    for event in pygame.event.get(): # Empties event queue (else OS will think program unresponsive)

        # Non key/mouse inputs
        if event.type == pygame.QUIT: 
            done = True

        # Key inputs
        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_UP]: y -= 100
        if pressed[pygame.K_DOWN]: y += 100
        if pressed[pygame.K_LEFT]: x -= 100
        if pressed[pygame.K_RIGHT]: x += 100
        if pressed[pygame.K_f]: image_flagged = not image_flagged
        if pressed[pygame.K_ESCAPE]: done = True 
        if pressed[pygame.K_EQUALS]:  kpi_radius = min(100, kpi_radius + 3)  
        if pressed[pygame.K_MINUS]: kpi_radius = max(1, kpi_radius - 3) 
        if pressed[pygame.K_SPACE]: point_confirmed = not point_confirmed
        active_color = color_blue if point_confirmed else color_teal    

        # Mouse inputs
        clicked = pygame.mouse.get_pressed()
        if clicked[0]==1: 
            kpi_primary = pygame.mouse.get_pos() # Relative screen location
            imgpixel_primary = ( kpi_primary[0]-x , kpi_primary[1]-y ) #Image pixel location (column, row)
        if clicked[2]==1: 
            kpi_secondary = pygame.mouse.get_pos() # Relative screen location
            imgpixel_secondary = ( kpi_secondary[0]-x , kpi_secondary[1]-y ) #Image pixel location (column, row)


        # Blitting and Filling
        screen.fill((0, 0, 0)) # Reset screen to black before updating with new keypress to move rectangle
        screen.blit(image, (x, y))
        pygame.draw.rect(screen, active_color, pygame.Rect(30, 30, 300, fontsize*2))
        screen.blit(active_text, (35,35) )
        if kpi_primary != (0,0): 
            pygame.draw.circle(screen, (0,255,0), (imgpixel_primary[0]+x,imgpixel_primary[1]+y), kpi_radius, 1) 
        if kpi_secondary != (0,0): 
            pygame.draw.circle(screen, (255,0,0), (imgpixel_secondary[0]+x,imgpixel_secondary[1]+y), kpi_radius, 1) 

        pygame.display.flip() # Required for updates to game screen to become visible
        clock.tick(60) # Slows program to 60fps by blocking GUI e-xecution until 1/60 of sec passed

Tags: pathimagegetiflocationscreenpygamecolor