Answers for "pygame scale image"

12

how to change the scale of a picture in pygame

import pygame
picture = pygame.image.load(filename)
picture = pygame.transform.scale(picture, (1280, 720))
Posted by: Guest on November-16-2020
4

pygame scale image

"""Load image"""
# I'm loading an image, but that does not matter.
surface = pygame.image.load("path/to/file.png").convert()
# Documentation for Surface.convert() and Surface.convert_alpha():
# https://www.pygame.org/docs/ref/surface.html#pygame.Surface.convert

"""Scale / resize surface"""
surface = pygame.transform.scale(surface=surface, size=(50, 50))
# In depth documentation:
# https://www.pygame.org/docs/ref/transform.html#pygame.transform.rotate
Posted by: Guest on February-09-2021
2

pygame rotate image

import pygame
import pygame.font

pygame.init()
size = (400,400)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

def blitRotate(surf, image, pos, originPos, angle):

    # calcaulate the axis aligned bounding box of the rotated image
    w, h       = image.get_size()
    box        = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]]
    box_rotate = [p.rotate(angle) for p in box]
    min_box    = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
    max_box    = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])

    # calculate the translation of the pivot 
    pivot        = pygame.math.Vector2(originPos[0], -originPos[1])
    pivot_rotate = pivot.rotate(angle)
    pivot_move   = pivot_rotate - pivot

    # calculate the upper left origin of the rotated image
    origin = (pos[0] - originPos[0] + min_box[0] - pivot_move[0], pos[1] - originPos[1] - max_box[1] + pivot_move[1])

    # get a rotated image
    rotated_image = pygame.transform.rotate(image, angle)

    # rotate and blit the image
    surf.blit(rotated_image, origin)

    # draw rectangle around the image
    pygame.draw.rect (surf, (255, 0, 0), (*origin, *rotated_image.get_size()),2)

font = pygame.font.SysFont('Times New Roman', 50)
text = font.render('image', False, (255, 255, 0))
image = pygame.Surface((text.get_width()+1, text.get_height()+1))
pygame.draw.rect(image, (0, 0, 255), (1, 1, *text.get_size()))
image.blit(text, (1, 1))
w, h = image.get_size()

angle = 0
done = False
while not done:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key==pygame.K_ESCAPE:
                done = True

    pos = (screen.get_width()//2, screen.get_height()//2)
    pos = (200, 200)

    screen.fill(0)
    blitRotate(screen, image, pos, (w//2, h//2), angle)
    angle += 1

    pygame.draw.line(screen, (0, 255, 0), (pos[0]-20, pos[1]), (pos[0]+20, pos[1]), 3)
    pygame.draw.line(screen, (0, 255, 0), (pos[0], pos[1]-20), (pos[0], pos[1]+20), 3)
    pygame.draw.circle(screen, (0, 255, 0), pos, 7, 0)

    pygame.display.flip()

pygame.quit()
Posted by: Guest on September-25-2020
0

pygame size of image

image = pygame.image.load("path/to/file.png")  # load image

width, height = image.get_width(), image.get_height()  # get size
print(width, height)  # print size

"""
OUTPUT:
(50, 50)
"""
Posted by: Guest on January-03-2021
0

pygame scale

#Start Libraries
import pygame, sys
from pygame.locals import *
#End Libraries

pygame.init() #We're initializing the script

pygame.display.set_caption('Platform') #This is the name of the window
window_size = (600,400) #The size of the window
screen = pygame.display.set_mode(window_size) #Initializing the screen

image_not_scaled = pygame.image.load('file_path.png').convert() #Importing the image
image = pygame.transform.scale(image_not_scaled, (32, 32)) #You can insert whatever you want for example (64, 64)
														   #It MUST be a tuple

#Now we'll create the while loop
run = True #With this we can control the script (stop it)
while run: #For NOW itz a simple while true
    screen.fill((255, 255, 255)) #Filling the screen with white
    screen.blit(image, (0, 0)) #Blitting the image on the screen

    for event in pygame.event.get(): #Checking the events
        if event.type == QUIT: #Checking if the event is QUIT
            pygame.quit() #In that case we'll exit from pygame
            sys.exit() #In that case we'll exit from the script
    
    pygame.display.update() #Updating the screen
Posted by: Guest on August-18-2021
-2

rezise object pygame

import pygame

# Load picture
picture = pygame.image.load("your/file/directery.png") # choose a picture and spesefy directery

# Resize
picture = pygame.transform.scale(picture # spesefy the objekt, (100, 100))# here is the scale by pixels
Posted by: Guest on January-06-2021

Python Answers by Framework

Browse Popular Code Answers by Language