Skip to content

Possible numpy code optimazation #13

Description

@rien333

The code that maps rgb pixel inputs to ascii characters in the generate_grayscale_for_image() function can be written more efficiently by using matrix calculations from numpy. Currently, it looks like this:

for h in range(height):
      for w in range(width):
          rgba = pixels[w, h]
          if rgba[3] != 255 and bgcolor is not None:
              rgba = alpha_blend(rgba, bgcolor)
			# ...
        rgb = rgba[:3]
        string += color[int(sum(rgb) / 3.0 / 256.0 * 16)]
    string += "\n

The calculation int(sum(rgb) / 3.0 / 256.0 * 16) in the inner-body of the loop can be done on the pixels input variable when it's represented as a matrix. The code will likely become less readable, but a comment could be added to elaborate on the new calculations. With this optimization in place, I was able to build a program that converts webcam input images to ascii text with a really decent framerate.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions