Skip to main content

Sprite detection package

Project description

Sprite Detection

Features

  • Find the Most Common Color in an Image.
  • Find Sprites in an Image.
  • Draw Sprite Label Bounding Boxes.

Why this project is useful?

  • Used reasonable library for process Image.
  • Can be used on a big image.
  • Easy to understand.

Usage

  • Find the Most Common Color in an Image
>>> from PIL import Image
# JPEG image
>>> image = Image.open('first_image.jpg')
>>> image.mode
'RGB'
>>> find_most_common_color(image)
(0, 221, 204)
# PNG image
>>> image = Image.open('second_image.png')
>>> image.mode
'RGBA'
>>> find_most_common_color(image)
(0, 0, 0, 0)
# Grayscale image
>>> image = image.convert('L')
>>> image.mode
'L'
>>> find_most_common_color(image)
0
  • Find Sprites in an Image.
>>> from PIL import Image
>>> image = Image.open('metal_slug_single_sprite.png')
>>> sprites, label_map = find_sprites(image, background_color=(255, 255, 255))
>>> len(sprites)
1
>>> for label, sprite in sprites.items():
...     print(f"Sprite ({label}): [{sprite.top_left}, {sprite.bottom_right}] {sprite.width}x{sprite.height}")
Sprite (1): [(0, 0), (29, 37)] 30x38
>>> import pprint
>>> pprint.pprint(label_map, width=120)
[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
 [0,0,1,1,1,1,0,0,1,1,1,1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,1,1,1,1,1,1,1,1,1,0],
 [0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
 [0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1],
 [0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,0],
 [0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,0,0],
 [0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
 [0,0,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
 [0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
 [0,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0],
 [1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
 [1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0],
 [1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0]]

Other example with the following image:

>>> from PIL import Image
>>> image = Image.open('optimized_sprite_sheet.png')
>>> sprites, label_map = find_sprites(image)
>>> len(sprites)
22
>>> for label, sprite in sprites.items():
...     print(f"Sprite ({label}): [{sprite.top_left}, {sprite.bottom_right}] {sprite.width}x{sprite.height}")
Sprite (25): [(383, 1), (455, 102)] 73x102
Sprite (43): [(9, 2), (97, 122)] 89x121
Sprite (26): [(110, 4), (195, 123)] 86x120
Sprite (46): [(207, 4), (291, 123)] 85x120
Sprite (16): [(305, 8), (379, 123)] 75x116
Sprite (53): [(349, 125), (431, 229)] 83x105
Sprite (61): [(285, 126), (330, 181)] 46x56
Sprite (100): [(1, 129), (101, 237)] 101x109
Sprite (106): [(106, 129), (193, 249)] 88x121
Sprite (93): [(183, 137), (278, 241)] 96x105
Sprite (95): [(268, 173), (355, 261)] 88x89
Sprite (178): [(6, 244), (101, 348)] 96x105
Sprite (185): [(145, 247), (245, 355)] 101x109
Sprite (141): [(343, 257), (417, 372)] 75x116
Sprite (169): [(102, 262), (142, 303)] 41x42
Sprite (188): [(249, 267), (344, 373)] 96x107
Sprite (192): [(412, 337), (448, 372)] 37x36
Sprite (256): [(89, 353), (184, 459)] 96x107
Sprite (234): [(11, 356), (104, 461)] 94x106
Sprite (207): [(188, 358), (281, 463)] 94x106
Sprite (229): [(384, 374), (456, 475)] 73x102
Sprite (248): [(286, 378), (368, 482)] 83x105
  • Draw Sprite Label Bounding Boxes.
>>> from PIL import Image
>>> image = Image.open('optimized_sprite_sheet.png')
>>> sprites, label_map = find_sprites(image)
>>> # Draw sprite masks and bounding boxes with the default white background color.
>>> sprite_label_image = create_sprite_labels_image(sprites, label_map)
>>> sprite_label_image.save('optimized_sprite_sheet_bounding_box_white_background.png')
>>> # Draw sprite masks and bounding boxes with a transparent background color.
>>> sprite_label_image = create_sprite_labels_image(sprites, label_map, background_color=(0, 0, 0, 0))
>>> sprite_label_image.save('optimized_sprite_sheet_bounding_box_transparent_background.png')
Sprite Masks with White Background Sprite Masks with Transparent Background

Built with

Authors

  • Le Quang Nhat (masternhat) - Intek student - Developer

Pull requests welcome!

Spotted an error? Something doesn't make sense? Send me a pull request!

Support

Ask your question here: https://www.google.com/

Everyone can Maintains && Contributing

Just follow steps:

  1. Fork it (https://github.com/intek-training-jsc/sprite-detection-masternhat.git)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

License

This project is licensed under the MIT License - see the LICENSE.md 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

spriteutil_fada_module-1.0.2.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

spriteutil_fada_module-1.0.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file spriteutil_fada_module-1.0.2.tar.gz.

File metadata

  • Download URL: spriteutil_fada_module-1.0.2.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.8

File hashes

Hashes for spriteutil_fada_module-1.0.2.tar.gz
Algorithm Hash digest
SHA256 087333c49cbf38fd4aa0116f5e9e65e36f0e8a2fc34c463c8da93033cf5efddc
MD5 e04ce08b7731c61e4bd3df86cfc6628c
BLAKE2b-256 343862bb4c2222b1f19ab6ff7620a507862bdc4a81e4556f3a534829954822dc

See more details on using hashes here.

File details

Details for the file spriteutil_fada_module-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: spriteutil_fada_module-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.8

File hashes

Hashes for spriteutil_fada_module-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b76a1a820cffd252b5baff903f4d7b51f1f8acd202773ada195d1673dc7c9240
MD5 e28abef883a02bfd61df0dacb6573a47
BLAKE2b-256 97ca275fe6b4d11c741395f5420cd424d65065f3583a86d8f3dd318fc76ff345

See more details on using hashes here.

Supported by

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