forked from msikma/pokesprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_to_most_similar.php
executable file
·145 lines (127 loc) · 3.99 KB
/
rename_to_most_similar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env php
<?php
# rename_to_most_similar.php: used to rename the images from a new dump
# to the filenames (and paths) of an old dump.
#
# Use with care, overwrites files.
#
# This script requires libpuzzle to be compiled and present.
# See <https://github.com/jedisct1/libpuzzle> for more information.
set_time_limit(0);
$verbose = false;
$show_hits = true;
$dir_old = @$argv[1];
$dir_new = @$argv[2];
$treshold = 0.09;
$GLOBALS['file_exts'] = array('jpg', 'png', 'jpeg', 'gif');
if (!isset($dir_old) || !isset($dir_new)) {
print('usage: rename_to_most_similar.php old_dir new_dir'.PHP_EOL.'rename_to_most_similar.php: error: too few arguments'.PHP_EOL);
exit();
}
if (!is_dir($dir_old) || !is_dir($dir_new)) {
print('usage: rename_to_most_similar.php old_dir new_dir'.PHP_EOL.'rename_to_most_similarrename_to_most_similar.php: error: old_dir or new_dir aren\'t directories'.PHP_EOL);
exit();
}
$imgs_old = iterate_dir($dir_old);
$imgs_new = iterate_dir($dir_new);
$renamed_imgs = 0;
overwrite_clean_imgs($imgs_old);
die('z');
$report = '<table border="2"><tr><th>New</th><th>Old</th></tr>';
$unique_new_imgs = array();
$counter = 0;
$total = count($imgs_old) * count($imgs_new);
print('old: `'.$dir_old.'\' contains '.count($imgs_old).' image files.'.PHP_EOL);
print('new: `'.$dir_new.'\' contains '.count($imgs_new).' image files.'.PHP_EOL);
print(PHP_EOL.'We will make '.(count($imgs_old) * count($imgs_new)).' comparisons.'.PHP_EOL);
if ($verbose) {
print(PHP_EOL);
}
$counter_new = 0;
foreach ($imgs_new as $img_new_path => $img_new_info) {
$best_match = array('diff' => 1, 'dir' => '', 'path' => '');
foreach ($imgs_old as $img_old_path => $img_old_info) {
$diff = get_img_diff($img_new_path, $img_old_path);
if ($verbose || $show_hits) {
$perc = ($counter / $total);
if ($perc >= 1) {
$perc = ' 100%';
}
if ($perc < 1) {
$perc = sprintf('%04.1f%%', $perc * 100);
}
}
if ($verbose) {
print('['.$perc.'] comparing: `'.$img_new_path.'\' to `'.$img_old_path.'\': diff: '.$diff.PHP_EOL);
}
$counter += 1;
if ($diff <= $treshold && $diff < $best_match['diff']) {
// Found a better match.
$best_match['diff'] = $diff;
$best_match['path'] = $img_old_path;
$best_match['dir'] = $img_old_info['dir'];
}
}
if ($best_match['diff'] <= $treshold && $show_hits) {
$report .= '<tr><td class="new">'.$img_new_tag.'</td><td class="old">'.$img_old_tag.'</td></tr>';
print('['.$perc.'] renaming `'..'\' to `'..'\' (trehold: '.$treshold.')');
rename_img($img_new_path, $best_match);
$renamed_imgs += 1;
}
$counter_new += 1;
}
if ($verbose || $show_hits) {
print('[ 100%] done.'.PHP_EOL);
}
print(PHP_EOL.'Amount of renamed images: '.$renamed_imgs);
print(PHP_EOL);
function rename_img($new_path, $old_info)
{
var_dump($new_path);
print_r($old_info);
}
function overwrite_clean_imgs($imgs)
{
foreach ($imgs as $img => $info) {
print_r($info);
}
}
function iterate_dir($dir)
{
$stack = array();
try {
$dir_it = new \DirectoryIterator($dir);
} catch (Exception $e) {
print('error: can\'t open directory: '.$dir);
continue;
}
foreach ($dir_it as $file) {
// Some checks to ensure it's a valid image.
if ($file->isDot()) {
continue;
}
if ($file->isDir()) {
$dir_stack = iterate_dir($dir.'/'.$file->getFilename());
$stack = array_merge($dir_stack, $stack);
continue;
}
$fn = $file->getFilename();
$fn_bits = explode('.', $fn);
$fn_ext = strtolower(trim(end($fn_bits)));
$file_path = $dir.'/'.$fn;
if (!in_array($fn_ext, $GLOBALS['file_exts'])) {
continue;
}
$stack[$file_path] = array('dir' => $dir.'/');
}
return $stack;
}
function get_img_diff($new, $old)
{
$diff = trim(exec('puzzle-diff "'.$new.'" "'.$old.'" 2>&1', $output, $code));
if ($code !== 0) {
print(PHP_EOL.'rename_to_most_similar.php: error: couldn\'t run the `puzzle-diff\' script'.PHP_EOL);
die();
}
return floatval($diff);
}