Advertisement
wolfeel

export grayscale to raw

Feb 19th, 2015
41
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None
  1. #!/usr/bin/env python
  2. from gimpfu import *
  3.  
  4. def exportToRawFile( image, layer ):
  5.     if( 1 != image.base_type ):
  6.         pdb.gimp_message( "Image is not grayscale.  Leaving" )
  7.         return
  8.        
  9.     pdb.gimp_plugin_enable_precision();
  10.  
  11.     try:
  12.         # Create a layer based on what is visible.
  13.         tempLayer = pdb.gimp_layer_new_from_visible( image, image, "Visible" )
  14.         image.add_layer( tempLayer, 0 )
  15.    
  16.         # Calculate the number of tiles.
  17.         numTiles_Horiz = int(tempLayer.width / 64)
  18.         if( tempLayer.width % 64 > 0 ):
  19.             numTiles_Horiz += 1
  20.         numTiles_Vert = int(tempLayer.height / 64)
  21.         if( tempLayer.height % 64 > 0 ):
  22.             numTiles_Vert += 1
  23.        
  24.         # Iterate over the tiles.
  25.         pixelByteArray= [];
  26.         debugStr = "";
  27.         for i in range(numTiles_Horiz):
  28.             for j in range(numTiles_Vert):
  29.                 # Update the progress bar.
  30.                 gimp.progress_update( float(i*numTiles_Vert + j) / float(numTiles_Horiz*numTiles_Vert) )
  31.        
  32.                 # Get the source tile.
  33.                 srcTile = tempLayer.get_tile( False, j, i )
  34.                
  35.                 # Iterate over the pixels of each tile.
  36.                 for y in range(srcTile.eheight):
  37.                     for x in range(srcTile.ewidth):
  38.                         # Get the pixel value.
  39.                         pixel = srcTile[x,y]
  40.                        
  41.                         # Build color value from bytes.
  42.                         colorVal = 0                       
  43.                         numBytes = int(len(pixel)*0.5)  # divide by 2 to skip the alpha channel
  44.                         for idx in range(numBytes):
  45.                             pixelByteArray.append( ord(pixel[idx]) )
  46.                             byteHexVal = ord(pixel[idx]) << (idx*8)
  47.                             colorVal += byteHexVal     
  48.                        
  49.                         debugStr += "pixel ({0},{1}): {2} (0x{2:x})\r\n".format( x, y, colorVal )
  50.        
  51.         pdb.gimp_message( debugStr )
  52.        
  53.         # Remove the temp layer.
  54.         image.remove_layer( tempLayer )
  55.        
  56.         # Output pixel values to file.
  57.         if "[Untitled]" != image.name:
  58.             filePath = os.path.splitext( image.filename )[0] + ".raw"
  59.             outputFile = open( filePath, 'wb' )
  60.             outputFile.write( bytearray(pixelByteArray) )      
  61.             outputFile.close()
  62.             pdb.gimp_message( "Written out to file {0}".format(filePath) )
  63.         else:
  64.             pdb.gimp_message( "You must save this project first." )
  65.                
  66.     except Exception as err:
  67.         pdb.gimp_message( "Unexpected error: " + str(err) )
  68.    
  69.     # End progress.
  70.     pdb.gimp_progress_end()
  71.    
  72.     return
  73.    
  74. register(
  75.     "python_fu_export_raw_file",
  76.     "Export to Raw",
  77.     "Export the image to a raw file.",
  78.     "Me",
  79.     "Me",
  80.     "2015",
  81.     "<Image>/MyScripts/Export to RAW",
  82.     "*",
  83.     [],
  84.     [],
  85.     exportToRawFile,
  86. )
  87.  
  88. main()
Advertisement
RAW Paste Data Copied
Advertisement