MCPcopy
hub / github.com/TencentARC/Pixal3D / extract_image

Function extract_image

data_toolkit/blender_script/dump_pbr.py:179–206  ·  view source on GitHub ↗
(tex_node, channels)

Source from the content-addressed store, hash-verified

177# =============== NODE TREE PARSING ===============
178
179def extract_image(tex_node, channels):
180 image = tex_node.image
181 pixels = np.array(image.pixels[:])
182 data = pixels.reshape(image.size[1], image.size[0], -1)
183 data = data[..., channels]
184
185 if data.dtype != np.uint8:
186 data = np.clip(data, 0.0, 1.0)
187 data = (data * 255).astype(np.uint8)
188
189 if len(data.shape) == 2: # Single channel
190 pil_image = Image.fromarray(data, mode='L')
191 elif data.shape[2] == 3:
192 pil_image = Image.fromarray(data, mode='RGB')
193 elif data.shape[2] == 4:
194 pil_image = Image.fromarray(data, mode='RGBA')
195 else:
196 raise ValueError("Unsupported channel shape for image")
197
198 buffer = io.BytesIO()
199 pil_image.save(buffer, format='PNG')
200 png_bytes = buffer.getvalue()
201
202 return {
203 'image': png_bytes,
204 'interpolation': tex_node.interpolation,
205 'extension': tex_node.extension,
206 }
207
208
209def try_extract_image(link, expected_channel='RGB'):

Callers 1

try_extract_imageFunction · 0.85

Calls 2

reshapeMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected