question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question: how do i split a tilesheet into multiple grid and merge them together?

See original GitHub issue

I need to work with a tilesheet, composed of 10x10 tiles. for example, a 160x2560 image would contain 16x256=4096 tiles. after that, I need to merge those 4096 10x10 images back into 160x2560 image. I tried this:

#!/usr/bin/python3

from pathlib import Path
from pyvips import Image, Region

base = Path("ASCIITileset")
tiles = base / "ASCIITiles.png"
fallback = base / "fallback.png"
config = base / "tile_config.json"

image = Image.new_from_file(fallback, memory=True)  # type: ignore


def fetch(region: Region, x: int, y: int, size: int):
    return region.fetch(size * x, size * y, size, size)


patch_size = 10
width: int = image.width
height: int = image.height
rows, cols = height // patch_size, width // patch_size


region = Region.new(image)
print(f"{width=}, {height=}, {patch_size=}, {rows=}, {cols=}")
# res = image.grid(patch_size, rows, cols)
print(rows, cols)
Path("patches").mkdir(exist_ok=True)

for y in range(rows):
    for x in range(cols):
        print(f"({x}, {y})")
        patch = fetch(region, x, y, patch_size)
        img = Image.new_from_buffer(patch, "")
        img.write_to_file(f"patches/{y}-{x}.png")

but encountered with:

width=160, height=2560, patch_size=10, rows=256, cols=16
256 16
(0, 0)
Traceback (most recent call last):
  File "/home/scarf/repo/cata/tileset-tools/learnvips.py", line 43, in <module>
    img = Image.new_from_buffer(patch, "")
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/scarf/.asdf/installs/python/3.11.0rc2/lib/python3.11/site-packages/pyvips/vimage.py", line 384, in new_from_buffer
    pointer = vips_lib.vips_foreign_find_load_buffer(data, len(data))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: initializer for ctype 'void *' must be a cdata pointer, not _cffi_backend.buffer

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jcupittcommented, Oct 22, 2022

Sure, it’s the same. Eg. (untested):

image.dzsave("x", depth="one", tile_size=10, overlap=0)

And:

tiles = [pyvips.Image.new_from_file(f"{x}_{y}.jpeg") for y in range(down) for x in range(across)]
image = pyvips.Image.arrayjoin(tiles, across=90)

(I might have the x and y loops swapped, I always forget which way py list comprehensions do nesting)

0reactions
jcupittcommented, Oct 22, 2022

It supports deepzoom, zoomify and gmaps naming convention, so z/x_y, nnn/z_x_y and z/y/x, but that’s it. You’ll need to run a second pass to rename the files.

It can write an uncompressed zip, which will save a lot of time and disc space, especially on windows. That might be worth checking. You can write to a zip in memory and then use any standard python zip handler to pull tiles out efficiently.

Square tiles only, sorry.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Combining Tilesets | RPG Maker Forums
The proceed to cut and paste what you want into the that new tile set. When you're done, don't save, go to EXPORT...
Read more >
Divide a complex shapefile into a grid - GIS Stack Exchange
Try dividing it into tiles by creating a layer of tiles using Vector Grid with a specified spacing, and then intersecting the two...
Read more >
How to Split Photos Into a Grid for Instagram - Ampfluence
Mobile Apps to Split Photos into a Grid. 1. Tile Pic. Turn your pictures into big tiled banners to share on Instagram and...
Read more >
TILESET in Photoshop (Tutorial) - YouTube
Learn how to create tiles and tilesets in Photoshop!Here's a link to the Tileset Template: https://goo.gl/Xih8nw♥ Support Brackeys on ...
Read more >
How to Create a Tileset in Krita with File and Clone Layers ...
We teach the techniques the pros use to create games, with Free and Open Source Software. Subscribe for more!Get our game creation courses: ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found