-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_spliced_images.m
More file actions
319 lines (256 loc) · 10.3 KB
/
write_spliced_images.m
File metadata and controls
319 lines (256 loc) · 10.3 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
function write_spliced_images(filenames, splice_count, varargin)
% Control program for cross_image_splice that takes an input that is a list
% of filenames and randomly chooses splice_count pairs to splice into each
% other. can optionally compress images at different compression levels,
% trim images to a certain size, or splice a block of exactly a certain
% size.
% Written by Efron Licht. Feel free to use, modify, etc, but please give me
% credit.
%% INPUT ARGUMENTS(NECESSARY)
% filenames:
% a CELL ARRAY or STRUCT of filenames, eg
% {'red.png', 'blue.png', 'green.png', 'white.jpg', 'green.tif'}
% splice_count:
% a positive integer less than or equal to length(filenames)^2
%% INPUT ARGUMENTS (OPTIONAL)
% paired input arguments:
% 'trim', trim_size:
% trim both images to trim_size x trim_size (non-negative integer)
% we recommend 128, 256, or 512
% 'splice_size', splice_size
% splice a block of exactly size splice_size
% 'source_compression', source_compression
% JPG compress the SOURCE image to the specified quality level
% (a positive integer < 100)
% 'target_compression', target_compression
% JPG oompress the TARGET image to the specified quality level
% (a positive integer < 100)
% unpaired input:
% 'random_compression':
% compresses both images before splicing at different compression
% levels. image compression quality is between 40 and 90, and the
% compression level difference ranges between 0 and 25.
% 'no_duplicates':
% stops splicing of images into themselves.
% 'duplicates_only':
% only create copy-move forgeries (splice an image into itself)
%% Broken Logging Stuff, Don't Worry About It
% function log_filename = makeLog
% time = (datestr('now', 'mmmm-dd'));
% log_filename = strcat('write_spliced_images_', time);
% log_file = fopen(log_filename, 'w+');
% fprintf(log_file, 'write_spliced_images LOG start');
% fclose(log_file);
% end
%
% function write_log(log, has_been_logged)
% log_file = fopen(log_filename, 'a+');
% for m=1:numel(has_been_logged);
% if not(has_been_logged(m))
% fprintf(log_file, log);
% has_been_logged(m) = 1;
% end
% end
% fclose(log_file);
%
% end
%% INPUT HANDLING
RANDOMLY_COMPRESS_IMAGES = false;
CUSTOM_DESTINATION_FOLDER = false;
DUPLICATES_ONLY = false;
NO_DUPLICATES = false;
TRIM_TO_SIZE = false;
USE_CUSTOM_SPLICE_SIZE = false;
SOURCE_COMPRESSION = false;
TARGET_COMPRESSION = false;
if nargin > 2
for n=1:length(varargin)
if(strcmp(varargin{n}, 'random_compression'))
RANDOMLY_COMPRESS_IMAGES = true;
elseif(strcmp(varargin{n}, 'splice_size'))
USE_CUSTOM_SPLICE_SIZE = true;
splice_size = varargin{n+1};
elseif(strcmp(varargin{n}, 'trim'));
TRIM_TO_SIZE = true;
trim_size = varargin{n+1};
elseif(strcmp(varargin{n}, 'no_duplicates'));
NO_DUPLICATES = true;
elseif(strcmp(varargin{n}, 'duplicates_only'));
DUPLICATES_ONLY = true;
elseif(strcmp(varargin{n}, 'destination_folder'));
CUSTOM_DESTINATION_FOLDER = true;
destination_folder = varargin{n+1};
try mkdir(destination_folder)
catch
%pass%
end
elseif(strcmp(varargin{n}, 'source_compression'))
SOURCE_COMPRESSION = true;
source_quality = varargin{n+1};
elseif(strcmp(varargin{n}, 'target_compression'))
TARGET_COMPRESSION = true;
target_quality = varargin{n+1};
end
end
end
if RANDOMLY_COMPRESS_IMAGES && (SOURCE_COMPRESSION || TARGET_COMPRESSION)
error('COMPRESS_RANDOMLY and specified compression are mutually exclusive')
end
tic
%makeLog
%% Permute Input to Create Source-Target File Pairs
N = numel(filenames);
first_file = repmat((1:N)', N, 1);
second_file = reshape(repmat(1:N, N, 1), [], 1);
N = N^2;
% this gives us a two-column array like this:
% first_file second_file
% 1 1
% 2 1
% 3 1
% .. ..
% n 1
% 1 2
% 2 2
% .. ..
% .. ..
% 1 n
% 2 n
if NO_DUPLICATES && not(DUPLICATES_ONLY);
% remove paired tuples (i.e, [1, 1], [3, 3], [n, n])
non_duplicates = find(first_file ~= second_file);
first_file = first_file(non_duplicates);
second_file = second_file(non_duplicates);
N = numel(first_file);
% first_file second_file
% 2 1
% 3 1
% .. ..
% n 1
% 1 2
% 3 2
% .. ..
elseif not(NO_DUPLICATES) && DUPLICATES_ONLY
N = numel(filenames);
first_file = 1:N;
second_file = first_file;
% first_file %second_file
% 1 1
% 2 2
% .. ..
% n n
elseif NO_DUPLICATES && DUPLICATES_ONLY
error('NO_DUPLICATES & DUPLICATES_ONLY are mutually exclusive.')
end
if strcmp(splice_count, 'all')
splice_count = numel(first_file);
permutation_key = 1:splice_count; %no need to randomize
else
try permutation_key = randperm(N^2, splice_count);
catch ME
if splice_count > N^2;
errorstr = strcat( ...
sprintf('input has more splices %g than', splice_count), ...
sprintf('permutations of filenames %g*%g = %g', N, N, N^2));
error(errorstr)
else
rethrow(ME)
end
end
end
%log = cell(splice_count, 1);
%has_been_logged = zeros(splice_count, 1);
% keep track of percent completion
percent_marker = linspace(0, splice_count);
percent_count = 1;
next_percent_marker = percent_marker(2);
%% MAIN LOOP
% set image quality to 100 if not specified.
if ~SOURCE_COMPRESSION, source_quality = 100; end
if ~TARGET_COMPRESSION, target_quality = 100; end
for n=1:splice_count
% choose source and destination files
key = permutation_key(n);
source_file = filenames{first_file(key)};
target_file = filenames{second_file(key)};
%source quality
if RANDOMLY_COMPRESS_IMAGES
compression_level_difference = randi([0, 25]);
source_quality = randi([40+compression_level_difference, 90]);
target_quality = source_quality - compression_level_difference;
end
% filename string manipulation
splice_string = sprintf('splice_%05g_src_', n);
source_string = strcat(source_file(1:end-4), ...
sprintf('_q1_%g', source_quality));
target_string = strcat('_targ_', target_file(1:end-4), ...
sprintf('_q2_%g', target_quality));
output_file = strcat(splice_string, source_string, ...
target_string, '.png') ;
if CUSTOM_DESTINATION_FOLDER
output_file = strcat(destination_folder, '/', output_file);
end
%% splice images
% in case of failure, we note the exception and move on. (except that
% LOGGING is broken right now)
try % cross_image_splice
if TRIM_TO_SIZE && USE_CUSTOM_SPLICE_SIZE && RANDOMLY_COMPRESS_IMAGES
cross_image_splice(source_file, target_file, output_file, ...
'compress_source', source_quality, ...
'compress_target', target_quality, ...
'trim', trim_size, ...
'splice_size', splice_size);
elseif TRIM_TO_SIZE && USE_CUSTOM_SPLICE_SIZE
cross_image_splice(source_file, dest_file, output_file, ...
'trim', trim_size, ...
'splice_size', splice_size);
elseif TRIM_TO_SIZE && RANDOMLY_COMPRESS_IMAGES
cross_image_splice(source_file, dest_file, output_file, ...
'compress_source', source_quality, ...
'trim', trim_size)
elseif TRIM_TO_SIZE
cross_image_splice(source_file, dest_file, output_file, ...
'trim', trim_size)
elseif RANDOMLY_COMPRESS_IMAGES && USE_CUSTOM_SPLICE_SIZE
cross_image_splice(source_file, target_file, output_file, ...
'compress_source', source_quality, ...
'compress_target', target_quality, ...
'splice_size', splice_size);
elseif RANDOMLY_COMPRESS_IMAGES
cross_image_splice(source_file, target_file, output_file, ...
'compress_source', source_quality, ...
'compress_target', target_quality)
elseif USE_CUSTOM_SPLICE_SIZE
cross_image_splice(source_file, target_file, output_file, ...
'splice_size', splice_size);
else
cross_image_splice(source_file, target_file, output_file);
end
% if successful, note in LOG, but don't write to file to cut down on I/O
% overhead
% log{splice_count} = strcat(num2str(n), ',', source_file, ',', dest_file, ',', ...
% output_file, ',', num2str(source_quality), ',', ...
% num2str(target_quality), ',', 'good', '\n');
catch exception
warning('splice of image %s to image %s failed!', source_file, target_file);
warning(exception.message)
% write the LOG and keep going.
% log{splice_count} = strcat(num2str(n), ',', source_file, ',', dest_file, ',', ...
% output_file, ',', num2str(source_quality), ',', ...
% num2str(target_quality), ',', 'failed', ',', exception.message, '\n');
% write_log(log, has_been_logged);
end
% keep track of percent completion and output to stdout
if n > next_percent_marker
time = toc;
percent_count = percent_count + 1;
next_percent_marker = percent_marker(percent_count+1);
fprintf('\n %0g percent complete after %03f seconds. \n', ...
round(n/splice_count*100), time);
end
end
%% CLEANUP
if RANDOMLY_COMPRESS_IMAGES
delete('temp.jpg')
end
end