[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Modifying SVG image



Hi Jim,

Yes, I'm using a memory mapped file to avoid saving any intermediate file. Here is the pseudo-code which reads the SVG file and replaces all occurrences of one color (#C0C0C0) by another (#000000). String encoding is required as a memory mapped file is the byte array. There is one limitation in this approach - the size of the object which you replace should be of the same size as the object by which you replace. It was not the problem in this particular case though.

import mmap
import contextlib

bitmap_image = None
with open("test.svg", "r") as f:
    with contextlib.closing(mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_COPY)) as m:
        old_color = "#C0C0C0".encode()
        pos = m.find(old_color)
        new_color = "#000000"
        while pos != -1:
            m[pos : pos + len(new_color)] = new_color.encode()
            pos = m.find(old_color)
        bitmap_image = pygame.image.load(m).convert_alpha()

Best regards

On Thu, Apr 21, 2022 at 11:45 AM Jim <jim@xxxxxxxxxxxxxxxxxx> wrote:
I don't know the answer to your question but if you are concerned with
the delay in reading/modifying/writing/reading you can speed that up by
using a RAM disk.

Jim

On 4/11/22 13:02, Go Peppy wrote:
> Hi,
>
> The following command works fine in Pygame 2:
> pygame.image.load("test.svg")
> It loads an SVG file and creates a Surface object.
>
> I need to change colors in that SVG image/file. As this is a text file
> this is a pretty simple task.
> The only way I see is to load an SVG file, change colors, save file
> and then load using pygame.image.load. Is it possible to accomplish
> that task without creating/saving a new SVG file?
>
> Thanks in advance!