remove_alpha_if_needed static method
Implementation
static Future<void> remove_alpha_if_needed(String imagePath) async {
File file = File(imagePath);
if (!file.existsSync()) {
print('文件不存在: $imagePath');
return;
}
List<int> bytes = await file.readAsBytes();
Image? image = decodeImage(Uint8List.fromList(bytes));
if (image == null) {
print('无法解析图片: $imagePath');
return;
}
if (!image.hasAlpha) {
print('图片没有透明通道,无需处理: $imagePath');
return;
}
Image newImage = image.convert(numChannels: 3);
await file.writeAsBytes(encodePng(newImage));
print('已去除 alpha 通道并覆盖: $imagePath');
}