This commit is contained in:
Benjamín 2021-09-02 13:35:27 +02:00
parent 125e0e2831
commit 9a1c6d95b8
3 changed files with 53 additions and 0 deletions

@ -0,0 +1,49 @@
from numpy import array as a
import numpy as np
import pygame
import copy
class Image:
def __init__(self, center, scalar, path, scene):
self.center = center
self.scalar = scalar
self.path = path
self.scene = scene
self.pd_ = self.scene.pd_
self.image = pygame.image.load(self.path).convert()
self.image = pygame.transform.scale(self.image,
self.to_ints((a(self.image.get_size()) * self.scalar).tolist()))
self.size = a(self.image.get_size(), dtype=np.float64)
self.sides_ratio = self.size[0] / self.size[1]
self.rescaled_size = copy.copy(self.size)
if self.scene.matrix[1, 1] - self.scene.matrix[0, 0] / self.sides_ratio >= 0:
self.rescaled_size *= self.scene.matrix[0, 0]
else:
self.rescaled_size *= self.scene.matrix[1, 1]
self.rescaled_image = pygame.transform.scale(self.image, self.to_ints((self.size * self.pd_).tolist()))
def blit(self):
if self.pd_ != self.scene.pd_:
self.pd_ = self.scene.pd_
self.rescaled_size = copy.copy(self.size)
if self.scene.matrix[1, 1] - self.scene.matrix[0, 0] / self.sides_ratio >= 0:
self.rescaled_size *= self.scene.matrix[0, 0]
else:
self.rescaled_size *= self.scene.matrix[1, 1]
self.rescaled_image = pygame.transform.scale(self.image, self.to_ints((self.size * self.pd_).tolist()))
self.scene.s.blit(self.rescaled_image, self.scene.matrix @ self.center)
@staticmethod
def to_ints(iterable):
for i in range(len(iterable)):
iterable[i] = int(iterable[i])
return iterable

@ -5,6 +5,7 @@ import numpy as np
from UI.Objects.nr_objects.Ellipse import Ellipse from UI.Objects.nr_objects.Ellipse import Ellipse
from UI.Objects.nr_objects.Circle import Circle from UI.Objects.nr_objects.Circle import Circle
from UI.Objects.nr_objects.Image import Image
from UI.Objects.nr_objects.Line import Line from UI.Objects.nr_objects.Line import Line
from UI.Objects.nr_objects.Text import Text from UI.Objects.nr_objects.Text import Text
from UI.Objects.r_objects.Rect import Rect from UI.Objects.r_objects.Rect import Rect
@ -31,3 +32,6 @@ class BasicScene(Scene):
t0 = Text(a([100, 50]), "hello world!", 26, (200, 200, 200), self) t0 = Text(a([100, 50]), "hello world!", 26, (200, 200, 200), self)
self.nr_objects.append(t0) self.nr_objects.append(t0)
i0 = Image(a([200, 0]), 0.8, "test.png", self)
self.nr_objects.append(i0)

BIN
UI/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB