Hey again,
yes, it works. I played around with the phyton script and I was able to extract .tga files from the .3dxml files. I successfully extracted images from the LDD and from Lego Star Wars II.
It looks like the images are upside down, but I think that is just a small issue. There is one .lxf file available on
eurobricks that contains all decorations. The script could extract them all at once.
Code:
import zipfile, base64, struct, os
import xml.etree.ElementTree as etree
def load_images(filename):
if zipfile.is_zipfile(filename):
zf = zipfile.ZipFile(filename, 'r')
member = zf.namelist()[1]
filename = zf.open(member)
print 'Unzipping', member
tree = etree.parse(filename)
images = tree.find('.//{http://www.3ds.com/xsd/3DXML}ImageSet')
numimages = len(images)
print 'Number of images:', numimages
#imgs = []
for i in tree.findall('.//{http://www.3ds.com/xsd/3DXML}Pixel'):
write_tga(i, numimages)
#imgs.append(img)
def write_tga(image, numimages):
h = int(image.attrib['height'])
w = int(image.attrib['width'])
fmt = image.attrib['format']
pixels = image[0].text
size = len(`numimages`)
imgidx = image.attrib['id']
imgidx = str('Map-%0'+`size`+'d') % int(imgidx)
filename = imgidx + '.tga'
bits = 32 if 'A' in fmt else 24
alpha = 8 if 'A' in fmt else 0
dirname = 'textures'
if not os.path.exists(dirname): os.mkdir(dirname)
pathname = os.path.join(dirname, filename)
if not os.path.exists(pathname):
print 'writing %s (%dx%d) %s' % (pathname, w, h, fmt.lower())
with open(pathname, 'wb') as file:
file.write(struct.pack('12B', 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0))
file.write(struct.pack('HH', w, h))
file.write(struct.pack('BB', bits, alpha))
pixels = base64.b64decode(pixels) #$base64.b64decode(bytes(pixels, 'utf-8'))
pixels = rgb_to_bgr(pixels, fmt)
file.write(pixels)
def rgb_to_bgr(pixels, fmt):
pixels = list(pixels)
npixels = len(pixels)
step = 4 if 'A' in fmt else 3
for i in range(0, npixels, step):
pixels[i],pixels[i+2] = pixels[i+2],pixels[i]
#$pixels = map(chr, pixels)
pixels = ''.join(pixels)
#$pixels = bytes(pixels, 'latin-1')
return pixels
I am not well versed with python! What I did:
- Put the code in a file (e.g. extractimages.py)
- Saved the script in the folder where the .3dxml files are
- Opened a phyton console
- Imported os (import os)
- Changed directory in the python console to the path where the .3dxml files are (os.chdir('<path>'))
- Imported the new script (import extractimages)
- Extracted images (extractimages.load_images('<file>.3dxml'))
You will get a folder named 'textures'. I hope it works.
Rolf