(tex_node, channels)
| 177 | # =============== NODE TREE PARSING =============== |
| 178 | |
| 179 | def 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 | |
| 209 | def try_extract_image(link, expected_channel='RGB'): |
no test coverage detected