Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from gimpfu import *
- def exportToRawFile( image, layer ):
- if( 1 != image.base_type ):
- pdb.gimp_message( "Image is not grayscale. Leaving" )
- return
- pdb.gimp_plugin_enable_precision();
- try:
- # Create a layer based on what is visible.
- tempLayer = pdb.gimp_layer_new_from_visible( image, image, "Visible" )
- image.add_layer( tempLayer, 0 )
- # Calculate the number of tiles.
- numTiles_Horiz = int(tempLayer.width / 64)
- if( tempLayer.width % 64 > 0 ):
- numTiles_Horiz += 1
- numTiles_Vert = int(tempLayer.height / 64)
- if( tempLayer.height % 64 > 0 ):
- numTiles_Vert += 1
- # Iterate over the tiles.
- pixelByteArray= [];
- debugStr = "";
- for i in range(numTiles_Horiz):
- for j in range(numTiles_Vert):
- # Update the progress bar.
- gimp.progress_update( float(i*numTiles_Vert + j) / float(numTiles_Horiz*numTiles_Vert) )
- # Get the source tile.
- srcTile = tempLayer.get_tile( False, j, i )
- # Iterate over the pixels of each tile.
- for y in range(srcTile.eheight):
- for x in range(srcTile.ewidth):
- # Get the pixel value.
- pixel = srcTile[x,y]
- # Build color value from bytes.
- colorVal = 0
- numBytes = int(len(pixel)*0.5) # divide by 2 to skip the alpha channel
- for idx in range(numBytes):
- pixelByteArray.append( ord(pixel[idx]) )
- byteHexVal = ord(pixel[idx]) << (idx*8)
- colorVal += byteHexVal
- debugStr += "pixel ({0},{1}): {2} (0x{2:x})\r\n".format( x, y, colorVal )
- pdb.gimp_message( debugStr )
- # Remove the temp layer.
- image.remove_layer( tempLayer )
- # Output pixel values to file.
- if "[Untitled]" != image.name:
- filePath = os.path.splitext( image.filename )[0] + ".raw"
- outputFile = open( filePath, 'wb' )
- outputFile.write( bytearray(pixelByteArray) )
- outputFile.close()
- pdb.gimp_message( "Written out to file {0}".format(filePath) )
- else:
- pdb.gimp_message( "You must save this project first." )
- except Exception as err:
- pdb.gimp_message( "Unexpected error: " + str(err) )
- # End progress.
- pdb.gimp_progress_end()
- return
- register(
- "python_fu_export_raw_file",
- "Export to Raw",
- "Export the image to a raw file.",
- "Me",
- "Me",
- "2015",
- "<Image>/MyScripts/Export to RAW",
- "*",
- [],
- [],
- exportToRawFile,
- )
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement