[Python] Last letter of name of GIMP brush missing, when created with Python

Discussão em 'Python' iniciado por Stack, Janeiro 31, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to create a (python) script, that takes an 2D-Array and converts it to a GIMP-brush in the .gbr format. I used the informations on the GIMP Developer Page to create a Bytestring that should be in the correct format, except the last caracter of the brush is always cut of. Other from that I can use the brush in Gimp without problems.

    I used the following script:

    def to_gbr(Image_array, name, spacing):
    # -- Create header
    # header size 0-3
    header_size_int = len(name)+28
    header_size = int.to_bytes(header_size_int,4)

    # version Type 4-7
    version_type = b"\x00\x00\x00\x02"

    # Width 8-11
    width_int = Image_array.shape[1]
    width = int.to_bytes(width_int,4)

    # height 12-15
    height_int = Image_array.shape[0]
    height = int.to_bytes(height_int,4)

    # Color depth 16-19
    # 1 = greyscale, 4 = RGBA
    if Image_array.shape[2] == 1:
    color_depth = int.to_bytes(1,4)
    elif Image_array.shape[2] == 4:
    print("RGB is not implemented yet")
    return
    else:
    print("unknown Color depth")
    return

    # Magic Number 20 - 23
    magic_number = b"GIMP"

    # spacing 24-27
    spacing = int.to_bytes(int(spacing),4)

    # name 28 - (header-size-1)
    name = bytes(name, encoding="utf-8")

    header = header_size + version_type + width + height + color_depth + magic_number + spacing + name

    # -- Read Data
    data = b""
    for y in range(height_int):
    for x in range(width_int):
    data = data + int.to_bytes(int(Image_array[y][x][0]))
    return header + data


    I tried to change the header size (the saved value that tells GIMP the size, but increasing it resulting Gimp in not being able to read it, reducing it was shifting the brush.

    Can anyone help me? I have no Idea what to do. Thanks in advance.

    Continue reading...

Compartilhe esta Página