Как я могу оптимизировать код маски инверсии в PygamePython

Программы на Python
Anonymous
Как я могу оптимизировать код маски инверсии в Pygame

Сообщение Anonymous »

Итак, я написал тестовую функцию для инвертирования изображения в определенной области и форме с возможностью изменения местоположения и размера. Единственная проблема заключается в том, что чем больше размер маски, тем медленнее она перемещается или увеличивается. Есть ли какие-нибудь решения для ускорения кода?

Код: Выделить всё

import pygame

fps = pygame.time.Clock()
pygame.init()
pygame.event.set_allowed([pygame.QUIT])

# declare all necessary vars
image = pygame.image.load('...\\image.png')
org_mask_inversion = pygame.image.load('...\\mask_inversion.png')
ix, iy = 0, 0
size_x, size_y = 100, 100
current_ix, current_iy = ix, iy
current_size_x, current_size_y = size_x, size_y
screen = pygame.display.set_mode([960, 720])
mask_inversion = pygame.transform.scale(org_mask_inversion, (size_x, size_y))
image2 = image

# function that inverts color values
def inversion_of_color(r, g, b):
return [255 - r, 255 - g, 255 - b]

# image inversion function
def inversion_of_surface(surface, sx, sy):
# it creates a new surface of the same size as the one specified in the function
inverted_surface = pygame.surface.Surface(mask_inversion.get_size(), pygame.SRCALPHA)

# passes over the entire size of the mask
for x in range(mask_inversion.get_width()):
for y in range(mask_inversion.get_height()):
# takes the color of the mask pixel
m_color = mask_inversion.get_at((x, y))
# checks if the pixel is green color
if m_color.g == 255:
# checks if the mask extends beyond the screen area
if 0 

Подробнее здесь: [url]https://stackoverflow.com/questions/78532738/how-can-i-optimize-the-code-of-inversion-mask-in-pygame[/url]

Вернуться в «Python»