#๐Ÿ”’ Stretching verticies

24 messages ยท Page 1 of 1 (latest)

thick violet
#

Hi everyone ! Can someone explain me how do I stretch verticies of an image in pygame, or other libraries except OpenGL? I want it to be like in the screenshot

trim nimbusBOT
#

@thick violet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

high spoke
#

does it have to be 2d? you could try a 3d extension of pygame or a 3d engine such as ursina

thick violet
#

yes it has to be 2D

high spoke
#

well the pygame transformations don't really support the asymmetric scaling of textures like you are trying to achieve

thick violet
high spoke
#

honestly it might be easier to use PIL or something to warp the image first and then put it into the game after

high spoke
#
import cv2
import numpy as np
from PIL import Image

def apply_perspective_transform(image_path):
    pil_image = Image.open(image_path)
    image = np.array(pil_image)
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    height, width = image.shape[:2]

    src_points = np.float32([
        [0, 0], [width, 0], [width, height], [0, height]
    ])

    dst_points = np.float32([
        [width * 0.2, height * 0.2], [width * 0.8, height * 0.1],
        [width * 0.9, height * 0.9], [width * 0.1, height * 0.8]
    ])

    matrix = cv2.getPerspectiveTransform(src_points, dst_points)
    transformed_image = cv2.warpPerspective(image, matrix, (width, height))
    transformed_image = cv2.cvtColor(transformed_image, cv2.COLOR_BGR2RGB)
    transformed_image = Image.fromarray(transformed_image)

    return transformed_image

transformed_image = apply_perspective_transform("img.png")
transformed_image.show()
#

this is so cursed but it works

#

if you apply the transformation before you put it into the game

#

no 2d library that I know of will do it during the runtime

#

just change around the destination points to whatever looks right

thick violet
#

thanks a lot man

high spoke
#

np

thick violet
# high spoke np

oh and the last thing, how do I draw the transformed image from the function on the window? screen.blit does not work

thick violet
#

ah yes screen.blit it's pygame

#

however do I show it on the screen

#

nevermind i fixed it

thick violet
#

!close

trim nimbusBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.