It is currently Fri Apr 26, 2024 1:08 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 31 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Assembling tiles based on co-ordinates in the file name.
PostPosted: Sun Jul 20, 2014 9:50 am  (#1) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
I have many parts of an image, each named as the co-ordinates of bottom left and top right corners of the image.

The file names currently take this format: "0,0,80,80.png" The numbers are in an arbitrary unit. Each tile is square and 80 units wide, measuring 256 x 256 pixels. I believe multiplying all values in the file name by 3.2 would give a 1:1 pixel mapping (256/80=3.2), so that's something I can do.

I found a GIMP script that can move a layer to a specified position: http://registry.gimp.org/node/27806

Does anyone know how to automate the task of putting each piece in the right place?

Thanks :)


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Sun Jul 20, 2014 3:49 pm  (#2) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
Yes, it takes a script... now is the question about you writing the script or about having it written by someone else?

_________________
Image


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Mon Jul 21, 2014 10:23 am  (#3) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
Another question is whether your coordinate system is top down (as used in most graphical UIs or in Gimp images (so 0,0 is the top left corner) or is down up (mathematical usage, where 0,0 would be the bottom left corner).

Otherwise your script would look like:

#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

import sys,os,glob
from gimpfu import *


def loadTiles(image,directory):
   for file in glob.glob(directory+'/'+'*,*,*,*.png'):
      count,ids = pdb.gimp_file_load_layers(image, file)
      layer=gimp.Item.from_id(ids[0])
      image.add_layer(layer,0)
                // (int(x)*32)/10 with your 3.2 factor
      x0,y0,x1,y1=tuple([int(x) for x in file.split('/')[-1].split('.')[0].split(',')])
      layer.set_offsets(x0,y1) // assuming top-down Y coordinate

whoiam='\n'+os.path.abspath(sys.argv[0])

register(
        "load-tiles",
        "Load tiles from files"+whoiam,
        "Load tiles from file",
        "Author","Author",
        "2014",
        "Load tiles from file",
        "*",
        [
      (PF_IMAGE, "image", "Input image", None),
      (PF_DIRNAME, "directory", "Directory", "/home/me/Sandbox/Tiles"),
        ],
        [],
   loadTiles,
   menu="<Image>/Image",
)

main()

_________________
Image


Last edited by ofnuts on Mon Jul 21, 2014 3:50 pm, edited 1 time in total.

Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Mon Jul 21, 2014 1:09 pm  (#4) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
Wow, that looks great. Thanks a lot.

The co-ordinate system is that maths kind, with the 0,0 at the bottom left. I guess I'll change (x0,y1) to (x0,y0)? :-)

At the moment I get an error that says:
Error: (C:\Programs\GIMP Portable\Data\.gimp\scripts\loadtiles.scm : 2) undefined sharp expression

Any ideas? :)

Thanks again


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Mon Jul 21, 2014 3:57 pm  (#5) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
Since it"s python it should go in "plug-ins", not in "scripts" and, under OSX and Linux, you should set its "executable" flag.

But in any case the code above wasn't meant to be run as is, it won't run with your images.

It would help a lot if you could zip and attach here a small example of your stuff so that I can finish it otherwise it's going to be a very protracted debugging session.

_________________
Image


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Tue Jul 22, 2014 3:44 pm  (#6) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
ofnuts wrote:
It would hep a lot if you could zip and attach here a small example of your stuff...


In your inbox :) Thanks!


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Thu Jul 24, 2014 12:37 pm  (#7) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
I've worked on some logic which I think will work for this script. This should work for tiles of any size and arrangement, as long as they are all the same resolution. The script is kept simple by building most of the image off-canvas and expanding the canvas to fit layers at the end.

0. The file name format is X1,Y1,X2,Y2,Xp,Yp.png where each part of the file name is a variable. All of these variables are declared as integers, as well as the variables Xf, Yf, Xo, Yo. The variables "ImportFolder" and "SaveFile" are declared as a strings.

1. The script pops up a dialogue box that prompts the user for "folder to import" and "save location". It saves these strings in their corresponding variables for calling later.
2. The script opens the first file in the folder that was selected. There could be many thousand files, so I don't know if it's best to enumerate them first or not.
3. The script uses X1 from the first image to create the variable Xo.
4. The script uses Y1 from the first image to create the variable Yo.
5. The script uses the formula Xp/(X2-X1) to create variable Xf. This is our X scale factor.
6. The script uses the formula Yp/(Y2-Y1) to create variable Yf. This is our Y scale factor.
7. The script duplicates the layer containing the first image. It sets the duplicated layer to be not visible, and re-names it with the name of the imported file.

8. The script imports the next file in the folder as a new layer above the previous layer.
9. The script offsets the new layer in X by (X1-Xo)*Xf.
10. The script offsets the new layer in Y by (Y1-Yo)*Yf*-1 (compensating for GIMP's inverted Y axis).
11. The script merges visible layers with "merge down". This will merge all layers except the duplicate from earlier, because it is not visible.
12. The script repeats from step 8 until all files are imported.

13. The script expands the canvas to fit all layers.
14. The script saves the file as .xcf in the location that the user selected earlier.


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Sun Jul 27, 2014 8:31 am  (#8) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
See attached zip. Menu is File>Create>From tiles, the only thing it requires is the directory containing the tiles.

Possibly a memory hog since it creates images that are over 100Mpx with your (very incomplete) test data. A version where layers are merged down one by one (instead of flattening them all together at the end) could use less RAM but is much slower.


Attachments:
load-tiles.zip [1.81 KiB]
Downloaded 138 times

_________________
Image
Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Sun Jul 27, 2014 5:07 pm  (#9) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
Thanks Ofnuts, this is really good! I appreciate the help.

It does seem there are big gaps in the data. I'll have to find out why.


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Sat Aug 23, 2014 4:46 pm  (#10) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
I couldn't quite decide if this should be a new post, because the topic is different, but I think this question belongs here for continuity :)

I'm using this script quite a lot - because it's amazing :D. I'm using it so much, in such a systematic way, that it'd be great if my input could be automated. All I do is point it at folders, but I've got more than 200 folders left to do and each takes a couple of hours to process.

I've got all my folders of tiles in a parent folder. Is it possible to point GIMP to this folder and have the script run on each child folder separately? The swap file would need to be purged between jobs. At the moment I have to restart GIMP between jobs to clear it out.

Ideally the transparent areas would be converted to a preset colour such as white, and the file would be converted to Indexed Colour with a preset scheme before it is saved. I found a function for this but I can't get it to be recognised.

I replaced the display instruction in the original script with a save function as below:
gimp.pdb.file_tiff_save(image, layer, directory + " (merged).tif", "raw_filename", 1)

Would anyone like to help me? :) Thanks


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Sat Aug 23, 2014 6:40 pm  (#11) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
Can you post a directory listing show all of the filenames in a tile set?

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Sat Aug 23, 2014 7:01 pm  (#12) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
Partial listing, since there are typically over 1600 files.

BBOX=252000,840800,252080,840880&WIDTH=256&HEIGHT=256.png.png
BBOX=252000,841040,252080,841120&WIDTH=256&HEIGHT=256.png.png
BBOX=252000,841120,252080,841200&WIDTH=256&HEIGHT=256.png.png
BBOX=252000,841280,252080,841360&WIDTH=256&HEIGHT=256.png.png
BBOX=252000,841360,252080,841440&WIDTH=256&HEIGHT=256.png.png
BBOX=252000,841600,252080,841680&WIDTH=256&HEIGHT=256.png.png
BBOX=252000,842000,252080,842080&WIDTH=256&HEIGHT=256.png.png
BBOX=252000,842560,252080,842640&WIDTH=256&HEIGHT=256.png.png
BBOX=252080,840640,252160,840720&WIDTH=256&HEIGHT=256.png.png
BBOX=252080,840960,252160,841040&WIDTH=256&HEIGHT=256.png.png
BBOX=252080,841040,252160,841120&WIDTH=256&HEIGHT=256.png.png
BBOX=252080,841120,252160,841200&WIDTH=256&HEIGHT=256.png.png
BBOX=252080,841360,252160,841440&WIDTH=256&HEIGHT=256.png.png
BBOX=252080,841520,252160,841600&WIDTH=256&HEIGHT=256.png.png
BBOX=252080,841760,252160,841840&WIDTH=256&HEIGHT=256.png.png
BBOX=252080,841840,252160,841920&WIDTH=256&HEIGHT=256.png.png
BBOX=252080,842000,252160,842080&WIDTH=256&HEIGHT=256.png.png
BBOX=252080,842800,252160,842880&WIDTH=256&HEIGHT=256.png.png
BBOX=252160,841360,252240,841440&WIDTH=256&HEIGHT=256.png.png
BBOX=252160,841520,252240,841600&WIDTH=256&HEIGHT=256.png.png
BBOX=252160,841680,252240,841760&WIDTH=256&HEIGHT=256.png.png
BBOX=252160,842080,252240,842160&WIDTH=256&HEIGHT=256.png.png


See my script above for the gory details (your eyes will bleed reading this Python :)

But IMHO, if things get industrial like this, the OP should switch to ImageMagick.

_________________
Image


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Mon Aug 25, 2014 10:49 am  (#13) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
Thanks for the info (though I'd prefer to have one of lumo's full, actual tile directories).

I have produced the following tarball of some example files for a 40x40 grid of 80x80 images (1600 files total). Does this look right? And would it be correct that the first file is not necessarily set at x=y=0 ?

ftp://silo.kerosenecow.net/bbox-files.tar.gz

ofnuts wrote:
But IMHO, if things get industrial like this, the OP should switch to ImageMagick.

My experience is that if one can avoid the actual loading of GIMP (when processing multiple files), it is typically about 2-4 times faster than ImageMagick for the the same operations. Lumo said GIMP spent hours processing a directory -- it should not take this long. I have played a little bit with the above set it only takes around 10 minutes (on my puny 2GHz machine with just 2GB of RAM). Either I am missing something or there is room for significant optimization of lumo's processing.

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Mon Aug 25, 2014 12:03 pm  (#14) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
The tiles are 256x256 but the coordinates are by steps of 80 units, so you have to scale them. The set I got from lumo was very incomplete (529 tiles for a 42x41 array, given their coordinates). The set you provided loads in 1'15" with my script (32" for Lumo's incomplete set), on my 2.6GHz Core i5. But your set made of mostly transparent tiles and Lumo's incomplete test set won't use as much RAM as a full one, and Lumo could be swapping (Gimp or system).

And yes, the first tile isn't at 0,0...

_________________
Image


Last edited by ofnuts on Tue Aug 26, 2014 5:06 pm, edited 2 times in total.

Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Tue Aug 26, 2014 4:47 pm  (#15) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
Ofnuts has already answered most of the questions for me :)

Your sample set looks correct. Good job :)

The sets can have different tile sizes. Most recently I joined a set that consisted of tiles at 5000x5000, each covering 500 units in X and Y. Ofnuts' script has a nice calculation at the start "256/80" which I change to match the source tiles. So this one was "5000/500".

I've found GIMP to be very capable at stitching these tiles. It's a shame that it can't use multiple swap files on different drives because then I could do larger sets. I'm limited to the free space on my SSD currently, plus a few gig of RAM (because 64 bit GIMP always crashes).

The tiles come from OS OpenData but I batch re-name them to a different scheme (based on co-ordinates) so that I can easily build a map of any given area from the available images. The scheme is something that was used by a tool I was using previously and I've stuck with it because I've built other tools around it now. I'm happy to omit the text portions and simplify the file name to 6 numbers, separated by commas. I always re-name the finished file to the six numbers scheme: Xmin,Ymin,Xmax,Ymax,Width,Height. This is so that it can be imported into another software package.

I've put a complete set on MailBigFile, here: http://mbf.me/ibyKS7
This is under the OS OpenData Licence so I'm obligated to write: Contains Ordnance Survey data © Crown copyright and database right 2014.


Last edited by Lumo on Tue Aug 26, 2014 5:02 pm, edited 1 time in total.

Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Tue Aug 26, 2014 5:02 pm  (#16) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
Lumo wrote:
The sets can have different tile sizes. Most recently I joined a set that consisted of tiles at 5000x5000, each covering 500 units in X and Y. Ofnuts' script has a nice calculation at the start "256/80" which I change to match the source tiles. So this one was "5000/500".


Easy enough to either:
  • obtain from additional parameters to the script
  • compute from the available data, since the tile size is in the name, and the other dimension can be guessed from the stepping

_________________
Image


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Tue Aug 26, 2014 5:03 pm  (#17) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
ofnuts wrote:
Easy enough to either:
  • obtain from additional parameters to the script
  • compute from the available data, since the tile size is in the name, and the other dimension can be guessed from the stepping

Yeah but I don't know how to do that, so I change the text ;-)


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Tue Aug 26, 2014 5:34 pm  (#18) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
Lumo wrote:
ofnuts wrote:
Easy enough to either:
  • obtain from additional parameters to the script
  • compute from the available data, since the tile size is in the name, and the other dimension can be guessed from the stepping

Yeah but I don't know how to do that, so I change the text ;-)


Another way is to give the complete specifications to the developer from the start...

About your new set, this leads to a 2.5 gigapixel image. Except burning CPUs and testing your disks I don't see much purpose in generating that... I don't know of any utility that will display such an image as a whole.

_________________
Image


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Tue Aug 26, 2014 6:29 pm  (#19) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
ofnuts wrote:
Another way is to give the complete specifications to the developer from the start...

Seriously?


Top
 Post subject: Re: Assembling tiles based on co-ordinates in the file name.
PostPosted: Tue Aug 26, 2014 6:39 pm  (#20) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
Lumo wrote:
ofnuts wrote:
Another way is to give the complete specifications to the developer from the start...

Seriously?


#7 != #1

_________________
Image


Top
Post new topic Reply to topic  [ 31 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Assembling to look like image while using up tiles from another image.

1

No new posts Attachment(s) Small Tiles - Big Preview for Gimp-2.10.20

0

No new posts Attachment(s) Patterns - Glassy Islamic Tiles

6

No new posts DeOldify A Deep Learning based project for colorizing old images

2

No new posts I'll make a filter based on other people's GEGL chain operations

1



* Login  



Powered by phpBB3 © phpBB Group