[Pygame] resizing an image

[Pygame] resizing an image

Postby metulburr » Sun Apr 21, 2013 6:04 pm

I checked on some examples, and i pretty much think i got the same idea from them, for the exception of the image not being displayed. The original image will display, but not the resiaed one. I also tried smoothscale versus scale, as i think i remember from before that it did it better, i guess.

Code: Select all
import pygame

class Player:
   def __init__(self, screen):
      self.screen = screen
      self.x = 0
      self.y = 0
      self.image_path = '/home/metulburr/Pictures/spoonbomb.png'
      self.image = pygame.image.load(self.image_path)
      self.image_resized = pygame.transform.smoothscale(self.screen, (50,100))
   
   def update(self):
      print(self.image_resized.get_size())
      self.screen.blit(self.image_resized, (0,0))

class Control:
   def __init__(self):
      pygame.init()
      self.screensize = (400,400)
      self.screen = pygame.display.set_mode(self.screensize)
      
      self.keys = None
      self.mouseY = 0
      self.mouseX = 0
      
      self.player = Player(self.screen)
      
      
   def main(self):
      run = True
      while run:
         for event in pygame.event.get():
            if event.type == pygame.QUIT:
               run = False
         self.update()
               
   def update(self):
      self.keys = pygame.key.get_pressed()
      self.mouseY, self.mouseX = pygame.mouse.get_pos()

      self.player.update()
      
      pygame.display.flip()
      
controller = Control()
controller.main()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1365
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: resizing an image

Postby metulburr » Mon Apr 22, 2013 12:09 am

nevermind. For some reason i though the first arg of smoothscale() was the screen's surface, but its the the image's surface, lol.

duh
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1365
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: resizing an image

Postby Mekire » Mon Apr 22, 2013 12:21 am

Here's some nonsense messing around with roto-zoom.

WASD to move, Arrow keys to zoom and rotate.
Code: Select all
import pygame as pg
import sys

DIRECT_DICT = {pg.K_a  : (-1, 0),
               pg.K_d  : ( 1, 0),
               pg.K_w  : ( 0,-1),
               pg.K_s  : ( 0, 1)}

class Player:
   def __init__(self, screen):
      self.screen = screen
      face_url  = "http://i1192.photobucket.com/albums/aa340/Mekire/smallface.png"
      self.orig_image = pg.image.load(image_from_url(face_url)).convert_alpha()
      self.image = self.orig_image.copy()
      self.rect = self.image.get_rect(center=screen.get_rect().center)
      self.size = 1
      self.angle = 0
      self.speed = 3

   def update(self):
      old_center = self.rect.center
      self.image = pg.transform.rotozoom(self.orig_image,self.angle,self.size)
      self.rect = self.image.get_rect(center=old_center)
      self.screen.blit(self.image, self.rect)

class Control:
   def __init__(self):
      pg.init()
      self.screensize = (400,400)
      self.screen = pg.display.set_mode(self.screensize)
      self.Clock = pg.time.Clock()
      self.keys = None
      self.mouseY = 0
      self.mouseX = 0

      self.player = Player(self.screen)


   def main(self):
      run = True
      while run:
         self.keys = pg.key.get_pressed()
         self.mouseY, self.mouseX = pg.mouse.get_pos()
         for event in pg.event.get():
            if event.type == pg.QUIT or self.keys[pg.K_ESCAPE]:
               run = False

         if self.keys[pg.K_UP]:
            self.player.size += 0.05
         if self.keys[pg.K_DOWN]:
            if self.player.size> 0.1:
                self.player.size -= 0.05
         if self.keys[pg.K_RIGHT]:
            self.player.angle -= 5
         if self.keys[pg.K_LEFT]:
            self.player.angle += 5

         for key in DIRECT_DICT:
            if self.keys[key]:
                self.player.rect.x += self.player.speed*DIRECT_DICT[key][0]
                self.player.rect.y += self.player.speed*DIRECT_DICT[key][1]
            self.player.rect.clamp_ip(self.screen.get_rect())

         self.update()

   def update(self):
      self.screen.fill((50,50,50))
      self.player.update()
      pg.display.flip()
      self.Clock.tick(60)

#################################
def image_from_url(url):
    try:
        from urllib2 import urlopen
        from cStringIO import StringIO as inout
    except ImportError:
        from urllib.request import urlopen
        from io import BytesIO as inout
    myurl = urlopen(url)
    return inout(myurl.read()) #Can be loaded by pygame.image.load

if __name__ == "__main__":
   controller = Control()
   controller.main()
   pg.quit();sys.exit()

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: resizing an image

Postby metulburr » Mon Apr 22, 2013 1:21 am

actually i have a question regarding that. I changed your image using smoothscale instead of rotozoom for zoom in and out effects. I dont see any difference in pixelation. Regardless at some point as you zoom in, your going to eventually see the pixels, what is the difference? Now what is the difference between smoothscale, scale, and rotozoom, as rotozoom allows for more features of rotating in addition to zoom? Wouldnt you just always use rotozoom then?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1365
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: resizing an image

Postby Mekire » Mon Apr 22, 2013 2:54 pm

For smoothscale/rotozoom, in terms of pixel art, preserving the pallet is often a priority. Smoothscale and rotozoom do not, I believe preserve the pallet. They add colors as needed to make the gradients smooth and such.

As for standard scale and rotate, they do not do this. I also make the assumption that they should be faster than smoothscale and rotozoom but haven't tested the theory extensively.

I should also mention that it is also very common (because scaling and rotating is so useless on pixel art) to make prerendered rotation frames. Drawing these is tedious as hell but it at least guarantees all the frames look nice.

Lastly, it might be obvious, but I should at least mention that you should always scale and rotate your original image. These processes are destructive and when applied cumulatively you will eventually be left with a mush.

-Mek

Edit: Oh and rotozoom only lets you scale the width and height by the same factor. The two separate scale functions let you scale width and height separately.
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests