5
$\begingroup$

I know that it's possible to display metadata in the Image Editor, but I need to access this info from Python, eg. in the form of a dictionary; is that possible at the moment? If not, is there a specification for the way this metadata is stored?

$\endgroup$

1 Answer 1

5
$\begingroup$

See bpy.types.Image for the available information about images.

The current image of an editor can be accessed from SpaceImageEditor.image. So for a given context

context.space_data.image

Can you be more precise about what kind of metadata you are interested ?

It depends on the graphics file format (i. e. png format) and might be a question for Stack Overflow


Reading chunks from png

import os

# chunk generator
def chunk_iter(data):
    total_length = len(data)
    end = 4

    while(end + 8 < total_length):     
        length = int.from_bytes(data[end + 4: end + 8], 'big')
        begin_chunk_type = end + 8
        begin_chunk_data = begin_chunk_type + 4
        end = begin_chunk_data + length

        yield (data[begin_chunk_type: begin_chunk_data],
               data[begin_chunk_data: end])

# open arbitrary pngs
blender_path = os.path.join(os.environ['HOME'], "Documents", "Blender")
pngs = (os.path.join(blender_path, elem)
            for elem in os.listdir(blender_path) if elem.endswith(".png"))

for png in pngs:
    with open(png, 'rb') as fobj:
        data = fobj.read()

    # check signature
    assert data[:8] == b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A'
    print("-" * 80, "chunks of: %s" % png, "-"*80, sep='\n')

    for chunk_type, chunk_data in chunk_iter(data):
        print("chunk type: %s" % chunk_type.decode())
        if   chunk_type == b'iTXt':
            print("--chunk data:",  chunk_data.decode())
        elif chunk_type == b'tEXt':
            print("--chunk data:", *chunk_data.decode('iso-8859-1').split('\0'))

Output

--------------------------------------------------------------------------------
chunks of: /home/User/Documents/Blender/0001.png
--------------------------------------------------------------------------------
chunk type: IHDR
chunk type: pHYs
chunk type: tEXt
--chunk data: File <untitled>
chunk type: tEXt
--chunk data: Note Test
chunk type: tEXt
--chunk data: Date 2015/08/12 17:07:41
chunk type: tEXt
--chunk data: Time 00:00:00:01
chunk type: tEXt
--chunk data: Frame 1
chunk type: tEXt
--chunk data: Camera Camera
chunk type: tEXt
--chunk data: Scene Scene
chunk type: tEXt
--chunk data: RenderTime 00:00.06
chunk type: IDAT
chunk type: IDAT
chunk type: IEND

So metadata for png is stored in tEXt chunks, separated by 0 character. See png specification

$\endgroup$
2
  • $\begingroup$ Thank you, but by metadata, I meant specifically the stamp info saved by Blender in the file header, such as Frame, Time, etc. blender.org/manual/render/post_process/metadata.html $\endgroup$ Aug 12, 2015 at 9:07
  • $\begingroup$ Nice, thanks! Now to write an equivalent for every possible format ;) $\endgroup$ Aug 14, 2015 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .