1. Brendan Howell
  2. PyCessing

Source

PyCessing / images.py

"""
    images.py
    Copyright 2012 Brendan Howell (brendan@howell-ersatz.com)

    This file is part of PyCessing.

    PyCessing is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PyCessing is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PyCessing.  If not, see <http://www.gnu.org/licenses/>.
"""

import pygame
import os

SCREEN = None
PROJECT_DIR = None

class Image:
    """All-purpose class for pixel images in pycessing"""
    def __init__(self, file_name):
        self.file_name = file_name
        self.loaded = False
        self.transformed = False

    def _loadSurface_(self):
        """checks if image file is read and loads if needed"""
        path = os.path.join(PROJECT_DIR, self.file_name)
        self.surface = pygame.image.load(path).convert_alpha()
        self.original_surface = self.surface.copy()
        self.loaded = True
    
    def draw(self, x, y, sx=None, sy=None, width=None, height=None):
        if(not(self.loaded)):
            self._loadSurface_()
        if(sx and sy):
            if(width and height):
                area = (sx, sy, width, height)
            else:
                area = (sx, sy, self.surface.get_width(), 
						self.surface.get_height())
        else:
            area = None
        SCREEN.unlock()
        SCREEN.blit(self.surface, (x, y), area)
        self.transformed = False

    def scale(self,width,height):
        if not(self.loaded):
            self._loadSurface_()
        if not(self.transformed):
            img = self.original_surface
            self.transformed = True
        else:
            img = self.surface
        self.surface = pygame.transform.smoothscale(img, (width, height))

    def flip(self,flipx=True,flipy=True):
        if not(self.loaded):
           self._loadSurface_()
        self.surface = pygame.transform.flip(self.surface, flipx, flipy)

    def rotate(self, angle):
        if not(self.loaded):
            self._loadSurface_()
        if not(self.transformed):
            img = self.original_surface
            self.transformed = True
        else:
            img = self.surface
        self.surface = pygame.transform.rotate(img, angle)

    def getWidth(self):
        if(not(self.loaded)):
            self._loadSurface_()
        return self.surface.get_width()

    def getHeight(self):
        if(not(self.loaded)):
            self._loadSurface_()
        return self.surface.get_height()

    def getPixel(self, x, y):
        if(not(self.loaded)):
            self._loadSurface_()
        return self.surface.get_at((x,y))

    def setPixel(self, x, y, r, g, b, a=None):
        if(not(self.loaded)):
            self._loadSurface_()
        if (a):
            self.surface.set_at((x, y), (r, g, b, a))
        else:
            self.surface.set_at((x, y), (r, g, b))
        
        
    def setCompositing(self, operation):
        pass