Search and replace tiles in a big image ?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
User avatar
Musaran
Posts: 3
Joined: 2016-09-09T04:59:34-07:00
Authentication code: 1151

Search and replace tiles in a big image ?

Post by Musaran »

Greetings.


I have a tiled image, size counting in 100 millions pixels.
Similar to this (684 KB extract), except cleaner.

I want to search-and-replace some tiles.
Like this one.


IM can very probably do that, but how ?


Things to know :
  • All tiles are aligned on a 16×16 grid with no offset, and size 16×16 or some small multiple.
  • Pixel content matches exactly.
  • Searched tile may have binary alpha.
  • Replacement tile can be just a solid color, as long as it's unique per tile searched.
  • Maybe I can scale the source images down, but not much.
  • Resulting image may be scaled down to 1/8.
  • I am using the latest IM on Windows 7 x64.

Reading the doc I see...

IM can extract a coordinate list :
compare -subimage-search -metric AE \
haystack.png needle.png miff:- 2>/dev/null |
convert - -delete 0 txt:- | sed -n '/#FFFFFF/s/:.*//p'
But this may be very slow. visgrep might be a fast alternative, but it's for Linux.

IM can easily replace a tile :
composite -geometry +32+64 replace.png big.png \
result.png
But can it read and process above coordinate list ?


Can these operations be chained ?
Can all search-and-replaces be done in one pass ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Search and replace tiles in a big image ?

Post by snibgo »

You say "Pixel content matches exactly." But your tile "like this one" (Smbombbmlock.png) isn't in your "684 KB extract" or the larger image (16896x14336), as far as I can see, so I'm not sure what you want to do.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Search and replace tiles in a big image ?

Post by snibgo »

I might add: IM doesn't have a "search and replace" operation. It can search, and it can replace, but the operations need to be scripted together.

IM's built-in search method is perfectly correct but slow. Many quicker and "probably correct" methods are possible, such as the one on my "Searching an image" page which is massively faster. I am currently extending that to a new method that is even faster, eg 100 times as fast.
snibgo's IM pages: im.snibgo.com
User avatar
Musaran
Posts: 3
Joined: 2016-09-09T04:59:34-07:00
Authentication code: 1151

Re: Search and replace tiles in a big image ?

Post by Musaran »

One tile I would actually search in this extract : Image


I am solving this particular problem at (literally) the source by branching the utility that generates those big images.

More work up-front, much easier afterwards.


But the question still stands as that need is bound to happen again.

Maybe the wise way would be to add search-and replace to GIMP or other open-source editors.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Search and replace tiles in a big image ?

Post by snibgo »

For that image, Google says "403. That’s an error."
snibgo's IM pages: im.snibgo.com
User avatar
Musaran
Posts: 3
Joined: 2016-09-09T04:59:34-07:00
Authentication code: 1151

Needle

Post by Musaran »

Okay, Google does not like direct linking...

Link
Post Reply