这是我针对 Powershell 的丑陋解决方案(现在是一个多平台解决方案)——我一次性编写了它,但它应该可以工作。我试图评论它以弥补它的糟糕程度。
不过,我会在执行此操作之前备份您的图像。以防万一。
这里的问题是它只检测每个文件是否与前一个文件重复 - 如果您需要检查每个文件是否与其他文件重复,您需要在其中嵌套另一个 for() 循环,这应该很容易。
#get the list of files with imagemagick
#powershell handily populates $files as an array, split by line
#this will take a bit
$files = identify -format "%# %f\n" *.png
$arr = @()
foreach($line in $files) {
#add 2 keys to the new array per line (hash and then filename)
$arr += @($line.Split(" "))
}
#for every 2 keys (eg each hash)
for($i = 2; $i -lt $arr.Length; $i += 2) {
#compare it to the last hash
if($arr[$i] -eq $arr[$i-2]) {
#print a helpful message and then delete
echo "$($arr[$i].Substring(0,16)) = $($arr[$i-2].Substring(0,16)) (removing $($arr[$i+1]))"
remove-item ($arr[$i+1])
}
}
奖励:删除任何具有特定哈希的图像(在我的例子中是一个全黑的 640×480 png):
for($i = 2; $i -lt $arr.Length; $i += 2) {
if($arr[$i] -eq "f824c1a8a1128713f17dd8d1190d70e6012b509606d986e7a6c81e40b628df2b") {
echo "$($arr[$i+1])"
remove-item ($arr[$i+1])
}
}
双重奖励:C 代码检查写入的图像是否与 hash/ 文件夹中的给定哈希冲突,如果是,则将其删除 - 为 Windows/MinGW 编写,但在必要时不应该太难移植。可能是多余的,但我想我会把它扔掉,以防它对任何人有用。
char filename[256] = "output/UNINITIALIZED.ppm";
unsigned long int timeint = time(NULL);
sprintf(filename, "../output/image%lu.ppm", timeint);
if(
writeppm(
filename,
SCREEN_WIDTH,
SCREEN_HEIGHT,
screenSurface->pixels
) != 0
) {
printf("image write error!\n");
return;
}
char shacmd[256];
sprintf(shacmd, "sha256sum %s", filename);
FILE *file = popen(shacmd, "r");
if(file == NULL) {
printf("failed to get image hash!\n");
return;
}
//the hash is 64 characters but we need a 0 at the end too
char sha[96];
int i;
char c;
//get hash until the first space
for(i = 0; (i < 64) && (c != EOF) && (c != 0x32); i++) {
sha[i] = c = fgetc(file);
}
pclose(file);
char hashfilename[256];
sprintf(hashfilename, "../output/hash/%s", sha);
if(_access(hashfilename, 0) != -1) {
//file exists, delete img
if(unlink(filename) != 0) {
printf("image delete error!\n");
}
} else {
FILE *hashfile = fopen(hashfilename, "w");
if(hashfile == NULL)
printf("hash file write error!\nfilename: %s\n", hashfilename);
fclose(hashfile);
}