Giter Site home page Giter Site logo

Comments (2)

TheBricktop avatar TheBricktop commented on May 27, 2024

My input:

  • More performant communication from opencv / numpy.arrays to godot types
  • functions that encapsulate creating images or streaming audio files generated in python.

from godot-python.

kb173 avatar kb173 commented on May 27, 2024

I know you commented on my related issue for this (#305) but for the sake of completeness, this is how one can currently go about efficiently converting a Matplotlib canvas (a Numpy Array) to a Godot PoolByteArray:

def canvas_to_godot_image(self, canvas):
	image = Image()
	
	width = canvas.get_width_height()[1]
	height = canvas.get_width_height()[0]
	
	size = width * height * 3
	
	pool_array = PoolByteArray()
	pool_array.resize(size)
	
	with pool_array.raw_access() as ptr:
		numpy_array = np.ctypeslib.as_array(ctypes.cast(ptr.get_address(), ctypes.POINTER(ctypes.c_uint8)), (size,))
		numpy_array[:] = np.frombuffer(canvas.tostring_rgb(), dtype=np.uint8).reshape(canvas.get_width_height()[::-1] + (3,)).flatten()
	
	image.create_from_data(height, width, False, Image.FORMAT_RGB8, pool_array)
	return image

As you wrote, it would be great if godot-python could provide this kind of functionality out of the box.

I think there are two ways in which this could be done:

  1. Define a PoolByteArray constructor specifically to construct from a Numpy Array, or
  2. Provide a Numpy-specific util class with a function such as NumpyUtil.numpy_array_to_pool_byte_array(array)

I think the first one would be more comfortable for users since they don't need to know that Numpy Arrays are a special case with a performant conversion method. However, the second one would be better for the godot-python codebase since it doesn't pollute the core codebase with Numpy-specific functionality.

Alternatively (or in addition), I think both options are realizable with compile options specifying whether Numpy-specific functionality should be compiled in or not - that would be another way of keeping things a bit separate.

@touilleMan what are your thoughts?

from godot-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.