-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
feature idea: how about a get_np_pixels() method? #329
Comments
Would it be overkill to have a default keyword argument like I suppose the advanced users would load and access the np_pixels array for that, so maybe this would be useless complexity. |
Here's where this is now: def get_np_pixels(self, x: int, y: int, w: int, h: int, *, bands: str = 'ARGB', dst: npt.NDArray[np.uint8] = None) -> npt.NDArray[np.uint8]:
"""$class_Sketch_get_np_pixels"""
x_slice = slice(x, x + w)
y_slice = slice(y, y + h)
self.load_np_pixels()
if bands == 'L':
pixels = self._np_pixels[x_slice, y_slice]
pixels = ((pixels[:, :, 0:1] / 255.0) * pixels[:, :, 1:] @ [0.299, 0.587, 0.114]).astype(np.uint8)
elif bands == 'ARGB':
pixels = self._np_pixels[x_slice, y_slice]
elif bands == 'RGB':
pixels = self._np_pixels[x_slice, y_slice, 1:]
elif bands == 'RGBA':
pixels = np.roll(self._np_pixels[x_slice, y_slice], 1, axis=2)
elif bands == 'BGR':
pixels = self._np_pixels[x_slice, y_slice, 3:0:-1]
elif bands == 'BGRA':
pixels = np.dstack([self._np_pixels[x_slice, y_slice, 3:0:-1], self._np_pixels[x_slice, y_slice, 0]])
else:
raise RuntimeError(f"Unknown `bands` value '{bands}'. Supported values are 'L', 'ARGB', 'RGB', 'RGBA', 'BGR', and 'BGRA'.")
if dst is not None:
if dst.shape != pixels.shape:
raise ValueError(f"Destination array has shape {dst.shape} but expected {pixels.shape}")
else:
dst[:] = pixels
return dst
else:
return pixels if pixels.base is None else pixels.copy() This seems to work but needs testing. The Should the
Processing keeps track of if the pixels array is "dirty" or not so its |
Yeah, if it is not too much trouble I think making And the reasoning about always providing a copy seems solid. |
I think it's necessary. I'm also going to add a |
I'm mostly done with the coding and testing of the new |
And now just the example code. Almost there! |
This is complete. @villares , you can see the reference docs for the 2 new methods on the dev website. I also added the new methods to the summary page. |
We have
get_pixels()
that can get us a single pixel or a PImage (should it be a Py5Image?) area of pixels. Could we get a np_array quickly without having to do theload_np_pixels()
thing before using.np_pixels[]
etc.? Would it make sense?I suppose there could be a performance penalty for calling load_np_pixels every time...
Originally posted by @villares in #328
The text was updated successfully, but these errors were encountered: