Mosaic an image's individual pixels?

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
wondering
Posts: 7
Joined: 2019-03-01T08:09:17-07:00
Authentication code: 1152

Mosaic an image's individual pixels?

Post by wondering »

Is it possible to break an image into discreet pixels and then reassemble those pixels to form a mosaic? The intended destination mosaic has identical dimensions, if that helps.

Another way to put it is that I want to apply a color pallette to an image, but I want to use each color from the pallette only once in the destination image.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Mosaic an image's individual pixels?

Post by snibgo »

Yes, although I don't really know what you mean.

What version of IM, on what platform? Sample images would help us understand what you want.

If the image is fairly small, you can split it into one image per pixel, rearrange those images, and re-assemble them into a single image.

IM doesn't have great tools for rearranging images, but you can do anything you want in C, for example: sort pixels into ascending order of lightness.
snibgo's IM pages: im.snibgo.com
wondering
Posts: 7
Joined: 2019-03-01T08:09:17-07:00
Authentication code: 1152

Re: Mosaic an image's individual pixels?

Post by wondering »

I'm using the latest version of IM on Windows, but I know MacOS too. What you're describing is exactly what I mean: Shatter an image into pixels and mosaic those pixels into a different image. I'm not sure where to start with this and I assume that writing hundreds of 1px files on disk would be painfully slow, so I hope there's a way around that.

Assemble pixels from the left to form the image on the right:
Image

Which should result in something like this (simulated result):
Image
Last edited by wondering on 2019-03-01T14:02:24-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Mosaic an image's individual pixels?

Post by fmw42 »

Perhaps my bash unix script, dice, at my link below is what you want?
wondering
Posts: 7
Joined: 2019-03-01T08:09:17-07:00
Authentication code: 1152

Re: Mosaic an image's individual pixels?

Post by wondering »

No, Dice's stated purpose is "To randomly rotate each successive square-sized patch in the image." I just want to assemble pixels as if they were parts of a mosaic to form an image. I've done this for sure using IM, but it was with maybe a dozen icon-sized images. Now I'd like to do it efficiently with thousands of 1px images (aka a palette).
Last edited by wondering on 2019-03-01T21:21:26-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Mosaic an image's individual pixels?

Post by snibgo »

You show the red "rose:" image, and a required result with a purple rose, and a gray/purple/green image. I don't understand where that gray/purple/green image should come from, or how to use it to transform the red rose image to a purple rose image.
snibgo's IM pages: im.snibgo.com
wondering
Posts: 7
Joined: 2019-03-01T08:09:17-07:00
Authentication code: 1152

Re: Mosaic an image's individual pixels?

Post by wondering »

The gray/purple/green image is user-provided. In this case it's just "Rose:" with its hex colours sorted alphabetically. But it could be anything else, really. The result should still be the closest possible approximation of Rose:
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Mosaic an image's individual pixels?

Post by snibgo »

Okay, the user provides the gray/purple/green image. What processing make the final image? Does this come from both the rose and the gray/purple/green image, or just one of these?
snibgo's IM pages: im.snibgo.com
wondering
Posts: 7
Joined: 2019-03-01T08:09:17-07:00
Authentication code: 1152

Re: Mosaic an image's individual pixels?

Post by wondering »

The processing would go like this: both the gray/purple/green image and "Rose:" are inputs.
"Rose:" is the reference image which the gray/purple/green image tries to replicate by rearranging its own pixels.
The output would be the gray/purple/green image with rearranged pixels to resemble "Rose:" as closely as possible.
The last image I provided is a simulation of the expected output.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Mosaic an image's individual pixels?

Post by fmw42 »

Is there an algorithm or reference to an algorithm that achieves your goal?
wondering
Posts: 7
Joined: 2019-03-01T08:09:17-07:00
Authentication code: 1152

Re: Mosaic an image's individual pixels?

Post by wondering »

Not that I'm aware of. The closest thing I've seen is IM's photo mosaic feature whereby multiple images are arranged and combined to form a larger image.
Here are some examples of what I'm trying to achieve, but I want to do it with pixels instead of images: http://www.mazaika.com/mazaika.html
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Mosaic an image's individual pixels?

Post by snibgo »

Okay. "rose:" and the the gray/purple/green image (call this "GPG") both contain 70x46 = 3220 pixels. You want the rearrangement of GPG that most closely resembles "rose:".

There are 3220! (3220 factorial) possible arrangements. This is an enormous number, far too many to try every one.

Another possibility, as suggested in your OP:

Code: Select all

Create an empty output image 70x46 pixels.
For every pixel P1 in GPG
{
  Find the closest pixel P2 in rose: for which the corresponding pixel in output is blank.
  Set that pixel in output.
}
The search could be in the opposite direction, like this:

Code: Select all

Create an empty output image 70x46 pixels.
For every pixel P2 in rose: for which the corresponding pixel in output is blank
{
  Find the closest pixel P1 in PGP.
  Set that pixel in output.
}
I don't think IM has anything built-in that would do this.
snibgo's IM pages: im.snibgo.com
wondering
Posts: 7
Joined: 2019-03-01T08:09:17-07:00
Authentication code: 1152

Re: Mosaic an image's individual pixels?

Post by wondering »

Seems like the second approach could work. I suppose the first step in solving this puzzle is to figure out how "closeness" between two colours is measured. Can this be expressed as a single number, or would it have to be tridimensional (RGB)?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Mosaic an image's individual pixels?

Post by fmw42 »

the root mean squared error RMSE color difference does what I think you want.

RMSE = sqrt ( ( (r1-r2)^ + (g1-g2)^ + (b1-b2)^2 )/3 )

See https://en.wikipedia.org/wiki/Root-mean ... _deviation

For speed, you can leave off the sqrt(), so that it becomes MSE

Zero is a perfect match.
Post Reply