Skip to content

Instantly share code, notes, and snippets.

@joshuaskelly
Last active July 25, 2021 18:07
Show Gist options
  • Save joshuaskelly/fa057cf68dcb7a3177d7c0a011090034 to your computer and use it in GitHub Desktop.
Save joshuaskelly/fa057cf68dcb7a3177d7c0a011090034 to your computer and use it in GitHub Desktop.
Example of Unstructuring and Structuring Pixel Data Using Python
# Flattening a sequence of structured data
image = ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4))
[rgb for pixel in image for rgb in pixel]
# >>> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
# Structuring a sequence of flat data
image = (1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)
it = iter(image)
tuple(zip(it, it, it))
# >>> ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment