adamancer / stitch2d

A Python script used to stitch a two-dimensional grid of tiles into a mosaic
MIT License
26 stars 6 forks source link

Structured Mosaic - 101st Column #4

Closed MercurySe7en closed 1 year ago

MercurySe7en commented 1 year ago

When creating a structured mosaic, the output omits the 101st column of tiles, replacing it with the final column (in my case the 123rd column). Columns 102 through 122 are placed correctly. My file numbering appears to be correct for both alpha and numeric sorting. The fact that is occurs on the 101st & final column leads me to believe it is an arbitrary limit applied in a while loop which is only being ignored for one cycle ("while count <= 100") then picks up again. Capture

adamancer commented 1 year ago

How strange. Can you please share your tiles so I can take a closer look?

MercurySe7en commented 1 year ago

It's quite a large file, so peeled off 4 rows near the top (see zip file). Issue still manifests the same.

Note that the problem row applies to the files starting with "0100_ .jpg" which is actually the 101st column since the first column is "0000" not "0001".

Thanks for looking at it. Smaller version of expected final result is picture below.

selection.zip

expected_output

adamancer commented 1 year ago

I found the problem. When drawing a mosaic based purely on row/column (i.e., where there is no overlap between tiles and the user doesn't run the align method), the script expects all the tiles to have the same dimensions and places them by multiplying the column index by the tile width and row index by tile height. Since your last column is smaller than the rest, this results in a bad placement.

I'll fix this in the package, but in the meantime you can manually assign tile coordinates as follows:

from stitch2d import StructuredMosaic

mosaic = StructuredMosaic(
    "tiles",
    dim=4,                  
    origin="upper left",    
    direction="vertical",  
    pattern="raster"       
)

y = 0
for row in mosaic.grid:
    x = 0
    for tile in row:
        tile.x = x + tile.width
        tile.y = y + tile.height
        x += tile.width
    y += tile.height

mosaic.save("mosaic.jpg")

Note that this approach still expects all tiles in each column to have the same width and all tiles in each row to have the same height (so width/height can vary as long as each column/row is internally consistent).

Please let me know if that solves it for you.

MercurySe7en commented 1 year ago

Works PERFECTLY now with the extra few lines of code!

I found your program specifically for this project, but I'm excited to use it for other projects in the future.

Thanks.

adamancer commented 1 year ago

Fixed in 09586c6d7805a8dfc7c126ec9fd6d9e7fa562519