Function Repository Resource:

PixelSort

Source Notebook

Sort pixels of an image to generate a pixel sorting glitch effect

Contributed by: Christian Pasquel

ResourceFunction["PixelSort"][img]

applies a pixel sorting effect to img.

ResourceFunction["PixelSort"][img,assoc]

applies a pixel sorting effect using the parameters specified in the association assoc.

Details

This function is based on the ASDF Pixel Sort Processing script developed by Kim Asendorf (2010).
The algorithm first computes an image based on the specified provided property (e.g. "Luminance") and sorts its pixels. The sorted pixels are mapped to the original image to produce its pixel sorted version. The intermediate image computed using the specified property, with its sorted pixels, can be returned instead of the original image by setting the "OriginalImage" component of the association assoc to False.
The pixel sorting algorithm parameters can be specified by giving the following components in the association assoc:
"Direction""Row"direction in which the sorting is applied ("Row" or "Column")
"Property""Luminance"computed pixel property to sort (mode)
"Level"0.2filter level (real 0 through 1)
"LevelTest""Min"whether the specified filter level is a maximum or a minimum
"Reversed"FalseTrue to sort the pixels into reverse canonical order
"OriginalImage"TrueFalse to return the image computed using the specified property (e.g. "Luminance") with its pixels sorted
Possible values for "Property" include:
"Luminance"computes the pixels' RGB luminance by using the formula {r*0.2126,g*0.7152,b*0.0722}
"Brightness"computes the pixels' RGB brightness by taking the mean of the values of the pixels
Possible values for "LevelTest" include:
"Min"use the specified level as a minimum when filtering pixels
"Max"use the specified level as a maximum when filtering pixels

Examples

Basic Examples (3) 

Get a sample image:

In[1]:=
img = ImageResize[ExampleData[{"TestImage", "Flower"}], 800]
Out[1]=

Sort the image pixels using the default values:

In[2]:=
ResourceFunction["PixelSort"][img]
Out[2]=

Sort the image pixels using custom values:

In[3]:=
ResourceFunction[
 "PixelSort"][img, <|"Direction" -> "Column", "Property" -> "Luminance", "Level" -> 0.7, "LevelTest" -> "Max", "OriginalImage" -> False, "Reversed" -> True|>]
Out[3]=

Scope (11) 

Get some sample images:

In[4]:=
img1 = ExampleData[{"TestImage", "Mandrill"}]
Out[4]=
In[5]:=
img2 = ExampleData[{"TestImage", "Tiffany"}]
Out[5]=
In[6]:=
img3 = ExampleData[{"TestImage", "Girl3"}]
Out[6]=

Sort rows of pixels of an image:

In[7]:=
ResourceFunction["PixelSort"][img1, <|"Direction" -> "Row"|>]
Out[7]=

Sort columns of pixels of an image:

In[8]:=
ResourceFunction["PixelSort"][img1, <|"Direction" -> "Column"|>]
Out[8]=

Sort the pixels of an image based on their luminance:

In[9]:=
ResourceFunction["PixelSort"][img3, <|"Property" -> "Luminance"|>]
Out[9]=

Sort the pixels based on their brightness:

In[10]:=
ResourceFunction["PixelSort"][img3, <|"Property" -> "Brightness"|>]
Out[10]=

Use a custom filter level:

In[11]:=
ResourceFunction["PixelSort"][img2, <|"Level" -> 0.5|>]
Out[11]=

Sort all pixels with a minimum luminance value of 0.7:

In[12]:=
ResourceFunction[
 "PixelSort"][img2, <|"Level" -> 0.7, "LevelTest" -> "Min"|>]
Out[12]=

Sort all pixels with a maximum brightness value of 0.7:

In[13]:=
ResourceFunction[
 "PixelSort"][img3, <|"Property" -> "Brightness", "Level" -> 0.7, "LevelTest" -> "Max"|>]
Out[13]=

Sort the pixels into reverse order:

In[14]:=
ResourceFunction[
 "PixelSort"][img3, <|"Level" -> 0.7, "LevelTest" -> "Max", "Reversed" -> True|>]
Out[14]=

Apply the filter on the image generated by computing the default luminance property:

In[15]:=
ResourceFunction[
 "PixelSort"][img3, <|"Level" -> 0.7, "LevelTest" -> "Max", "Reversed" -> True, "OriginalImage" -> False|>]
Out[15]=

Apply the filter on the image generated by computing the brightness of its pixels:

In[16]:=
ResourceFunction[
 "PixelSort"][img3, <|"Property" -> "Brightness", "Level" -> 0.7, "LevelTest" -> "Max", "Reversed" -> True, "OriginalImage" -> False|>]
Out[16]=

Properties and Relations (1) 

There are subtle differences between using the "Luminance" and "Brightness" properties:

In[17]:=
img1 = ExampleData[{"TestImage", "Mandrill"}];
In[18]:=
ImageDifference[
 ResourceFunction["PixelSort"][img1, <|"Property" -> "Luminance"|>], ResourceFunction["PixelSort"][img1, <|"Property" -> "Brightness"|>]]
Out[18]=

Possible Issues (2) 

An invalid property will return an error:

In[19]:=
img1 = ExampleData[{"TestImage", "Mandrill"}];
In[20]:=
ResourceFunction["PixelSort"][img1, <|"Property" -> "Chrominance"|>]
Out[20]=

An invalid level test will return an error:

In[21]:=
ResourceFunction["PixelSort"][img1, <|"LevelTest" -> "Median"|>]
Out[21]=

Neat Examples (2) 

Get a test image:

In[22]:=
img = ImageResize[ExampleData[{"TestImage", "Flower"}], 800]
Out[22]=

Create a pixel sorting animation:

In[23]:=
ListAnimate[
 Reverse[Table[
   ResourceFunction["PixelSort"][
    img, <|"Direction" -> "Column", "Property" -> "Luminance", "Level" -> level, "LevelTest" -> "Min", "OriginalImage" -> False,
      "Reversed" -> True|>], {level, 0.1, 0.6, 0.05}]], SaveDefinitions -> True]
Out[23]=

Version History

  • 1.0.0 – 23 March 2021

Related Resources

Author Notes

While this function is based on ASDF Pixel Sort by Kim Asendorf, it does not replicate the whole source code's algorithm. The original sorting modes (black (0), brightness (1), white(2)) have not been implemented as such. Instead, a luminance and a brightness modes are available (specified by using the "Property" component). Future updates of the function will include more modes and also the ability to provide a custom function as mode.

License Information