Paste Code
Paste Blends
Paste Images
import bpy
import struct
import time

def write_tga(image_index, filepath):

with open(filepath, "wb") as f:

try:
img = bpy.data.images[image_index]
except IndexError:
print("Error: bpy.data.images[%i] does not exist!" % image_index)
return

w, h = img.size

"""
w_bytes = w.to_bytes(2, 'little')
h_bytes = h.to_bytes(2, 'little')

b = bytearray(16) # actually 18, but using slices increases size
b[2] = 2 # typ RGB(A) uncompressed
b[12:13] = w_bytes
b[14:15] = h_bytes
b[16] = 32 # 32bit per pixel
b[17] = 8 # image attribute bitfield

f.write(b)
"""

# A bit nicer than the above...
f.write(struct.pack("<HBIIBHHBB", 0, 2, 0, 0, 0, w, h, 32, 8))

"""
# Pretty fast
import array
p = [int(pix*255) for pix in img.pixels]

# Generator slightly slower than list
#p = (int(pix*255) for pix in img.pixels)

arr = array.array("B", p)
arr.tofile(f)
"""


# Slightly faster than above
#f.write(struct.pack(str(len(img.pixels))+"B", *[int(pix*255) for pix in img.pixels]))

# Seems to be the fastest method
f.write(bytes([int(pix*255) for pix in img.pixels]))

f.close()

filepath = "D:\\imgg.tga"

t = time.clock()
write_tga(0, filepath)
print("Time: %.2f" % (time.clock() - t))
  1. import bpy
  2. import struct
  3. import time
  4.  
  5. def write_tga(image_index, filepath):
  6.    
  7.     with open(filepath, "wb") as f:
  8.    
  9.         try:
  10.             img = bpy.data.images[image_index]
  11.         except IndexError:
  12.             print("Error: bpy.data.images[%i] does not exist!" % image_index)
  13.             return
  14.        
  15.         w, h = img.size
  16.        
  17.         """
  18.        w_bytes = w.to_bytes(2, 'little')
  19.        h_bytes = h.to_bytes(2, 'little')
  20.        
  21.        b = bytearray(16) # actually 18, but using slices increases size
  22.        b[2] = 2 # typ RGB(A) uncompressed
  23.        b[12:13] = w_bytes
  24.        b[14:15] = h_bytes
  25.        b[16] = 32 # 32bit per pixel
  26.        b[17] = 8 # image attribute bitfield
  27.        
  28.        f.write(b)
  29.        """
  30.        
  31.         # A bit nicer than the above...
  32.         f.write(struct.pack("<HBIIBHHBB", 0, 2, 0, 0, 0, w, h, 32, 8))
  33.        
  34.         """
  35.        # Pretty fast
  36.        import array
  37.        p = [int(pix*255) for pix in img.pixels]
  38.        
  39.        # Generator slightly slower than list
  40.        #p = (int(pix*255) for pix in img.pixels)
  41.        
  42.        arr = array.array("B", p)
  43.        arr.tofile(f)
  44.        """
  45.        
  46.  
  47.         # Slightly faster than above
  48.         #f.write(struct.pack(str(len(img.pixels))+"B", *[int(pix*255) for pix in img.pixels]))
  49.        
  50.         # Seems to be the fastest method
  51.         f.write(bytes([int(pix*255) for pix in img.pixels]))
  52.  
  53.         f.close()
  54.  
  55. filepath = "D:\\imgg.tga"
  56.  
  57. t = time.clock()
  58. write_tga(0, filepath)
  59. print("Time: %.2f" % (time.clock() - t))
go to heaven