Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible numpy code optimazation #13

Open
rien333 opened this issue Dec 5, 2017 · 0 comments
Open

Possible numpy code optimazation #13

rien333 opened this issue Dec 5, 2017 · 0 comments

Comments

@rien333
Copy link

rien333 commented Dec 5, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant