(self, data: bytes)
| 36 | self.image = Image.fromarray(np.clip(self.pixels, 0, 255).astype(np.uint8)) |
| 37 | |
| 38 | def _embed(self, data: bytes): |
| 39 | bits_per_ch = self.config.get_max_bits_used_per_channel() |
| 40 | bits = np.unpackbits(np.frombuffer(data, dtype=np.uint8)) |
| 41 | n = len(bits) |
| 42 | |
| 43 | if bits_per_ch == 1: |
| 44 | # 顺序 LSB:flat_idx == position,直接切片,避免 fancy indexing |
| 45 | flat = self.pixels.reshape(-1) |
| 46 | flat[:n] = ((flat[:n].astype(np.int32) & 0xFE) | bits).astype(np.uint8) |
| 47 | else: |
| 48 | for i, bit in enumerate(bits): |
| 49 | ch_pos = i // bits_per_ch |
| 50 | bit_slot = i % bits_per_ch |
| 51 | pixel_idx = ch_pos // self.channels |
| 52 | channel = ch_pos % self.channels |
| 53 | row = pixel_idx // self.width |
| 54 | col = pixel_idx % self.width |
| 55 | v = int(self.pixels[row, col, channel]) |
| 56 | mask = 0xFF ^ (1 << bit_slot) |
| 57 | self.pixels[row, col, channel] = (v & mask) | (int(bit) << bit_slot) |
| 58 | |
| 59 | def get_image(self) -> Image.Image: |
| 60 | return self.image |
no test coverage detected