Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
module PixenToGameQuery
require 'RMagick'
include Magick
FPR = 12 # Frames per row in a sprite sheet generated by Pixen
COLPAD = 2 # Pix between each column of images on the sprite sheet
ROWPAD = 2 # Pix between each row of images on the sprite sheet
LEFTPAD = 4 # Extra pix on left of sprite sheet
RIGHTPAD = 20 # Extra pix on right of sprite sheet
BOTTOMPAD = 2 # Extra pix on bottom of sprite sheet
def PixenToGameQuery.convert_sprite(image_path, width, height, frames, new_image_path)
original =
img = + COLPAD) * frames, height)
sprite_rows = (frames.to_f / FPR).ceil
0.upto(sprite_rows - 1) do |rownum|
rowframes = (rownum == sprite_rows - 1) ? frames % FPR : FPR
row_width = rowframes * (width + COLPAD)
pix = original.get_pixels(LEFTPAD,
ROWPAD + (height + ROWPAD) * rownum,
img.store_pixels(rownum * FPR * (width + COLPAD),
Copy link

robesris commented May 11, 2010

This is a simple script that uses RMagick to convert a sprite sheet created by Pixen and convert it to a single-row sprite suitable for use with the gameQuery library. Pixen automatically creates rows of 12 frames each and puts some extra padding in. This script accounts for that and trims the image down and puts all the frames in a single row.

image_path: path to the file generated by Pixen
width: the width of a single frame
height: the height of a single frame
frames: the total number of frames in the animation
new_image_path: the path to write the new file to

Copy link

robesris commented May 12, 2010

Minor changes to encapsulate the code in a Module and change filename to match module name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment