Skip to main content

Sprite detect

Project description

# Sprite Detection

## Sprite

A [sprite](<https://en.wikipedia.org/wiki/Sprite_(computer_graphics)>) is a small [raster graphic](https://en.wikipedia.org/wiki/Raster_graphics) (a bitmap) that represents an object such as a [character, a vehicle, a projectile, etc.](https://www.youtube.com/watch?v=a1yBP5t-fSA)

![Metal Slug ](metal_slug_sprite_large.png)

Sprites are a popular way to create large, complex scenes as you can manipulate each sprite separately from the rest of the scene. This allows for greater control over how the scene is rendered, as well as over how the players can interact with the scene.

Sprites are mainly used in 2D video games, such as [Shoot’em up](https://en.wikipedia.org/wiki/Shoot_%27em_up) in which the hero combats a large number of enemies by shooting at them while dodging their fire:

————————————————————————— | —————————————————————– | —————————————————— |
![Cannon Fodder](2d_video_game_cannon_fodder.jpg) | ![Commando](2d_video_game_commando.jpg) | ![Metal Slug](2d_video_game_metal_slug.png) |

## Sprite Animation

Several sprites, representing a same object, can be rendered in quick succession to give the illusion of movement of this object (animation).

For example, the animation of the hero Marco Rossi (Metal Slug), running with a gun, is composed of 9 sprites:

![Metal Slug Sprite Running with a Gun](metal_slug_sprites_running_with_gun.png)

## Sprite Sheet

It is not uncommon for games to have tens to hundreds of sprites. Loading each of these as an individual image would consume a lot of memory and processing power. To help manage sprites and avoid using so many images, many games use [sprite sheets](https://www.youtube.com/watch?v=crrFUYabm6E) (also known as image sprites).

A sprite sheet consists of multiple sprites in one image. In other words, sprite sheets pack multiple sprites into a single picture. Using sprite sheet, video game developers create sprite sheet animation representing one or several animation sequences while only loading a single file:

![Metal Slug Sprites](metal_slug_sprite_sheet_large.png)

## Sprite Bounding Box

A frame (bounding box) can be used to delimit the sprite in the sprite sheet. This bounding box is defined with two 2D points top_left and the bottom_right, which their respective coordinates x and y are relative to the top-left corner of the sprite sheet’s image.

For example:

![Shape Bounding Boxes](metal_slug_sprite_detection_bounding_boxes.png)

## Sprite Mask

The mask of a sprite defines the 2D shape of the sprite. For example, the sprite sheet [metal_slug_sprite_standing_stance.png](metal_slug_sprite_standing_stance_large.png) contains the 3 following sprites:

![Metal Slug Standing Stance](metal_slug_sprite_standing_stance_large.png)

The masks of these sprites are respectively:

![](metal_slug_sprite_detection_coloring.png)

## Optimized Sprite Sheet

Sprites could be evenly placed in the sprite sheet according to their bounding box. The disadvantage is that this method of placement wastes a lot of memory because of all the additional transparency of each sprite, especially when sprites have different width and height.

The developers are aware of the wasted memory and started to optimize the space in the sprite sheets. Sprites are placed close to each others, depending on their shape. The bounding box of a sprite may intersect the bounding box of another sprite, but the shape of this sprite is always separated from the shape of any other sprites with at least 1 transparent pixel.

![Optimized Sprite Sheet](optimized_sprite_sheet.png)

This space optimization is even more efficient when some big sprites have concave shape (i.e., shape that curves inward):

Scene with 2 Sprites | Sprite Sheet | Sprite Masks |
———————– | ——————————————– | ————————————————- |
![Islands](islands.png) | ![Islands Sprite Sheet](islands_sprites.png) | ![Islands Sprite Masks](islands_sprite_masks.png) |

# Aim of the project

  • Detect sprites in the input image and draw those sprites to new image with its bounding box and mark.

# Usage Information

### Requirement

  • Python 3.6

  • Numpy

  • Pillow

### Installation

`bash $ pipenv install spritesheet-lecuong91 `

Example:

`python image = SpriteSheet('metal_slug_sprites_running_with_gun.png') sprites, label = image.find_sprites() sprite_label_image = image.create_sprite_labels_image() sprite_label_image.save('test.png') `

# Contact Information If you have any problems using this library, please use the contact below.

Email: cuong.le@f4.intek.edu.vn

# License

This project is licensed under the MIT License - see the LICENSE.txt file for details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spritesheet_lecuong91-0.0.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

spritesheet_lecuong91-0.0.2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file spritesheet_lecuong91-0.0.2.tar.gz.

File metadata

  • Download URL: spritesheet_lecuong91-0.0.2.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.9

File hashes

Hashes for spritesheet_lecuong91-0.0.2.tar.gz
Algorithm Hash digest
SHA256 eb2071cd8ef146ffafd376baa462d19896416fb1a0e5f6a139cb990accf3f9e0
MD5 0e3942940433b1da73fef1cb3cc53c20
BLAKE2b-256 54b2c3e42f53ab23edbdf37d348c39fb5c11cd8a392d3e688fb14360874aa0ee

See more details on using hashes here.

File details

Details for the file spritesheet_lecuong91-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: spritesheet_lecuong91-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.9

File hashes

Hashes for spritesheet_lecuong91-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a6073694192f4239b03bd764e3a208e3348025f0eab7d1cf47868da6cb6b371c
MD5 94ede0d97f820e780b20ad5d71aa889a
BLAKE2b-256 da8db50903f9ed416f57d954847d62c381f5536096d5fad55d12194982c130fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page