-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdn7toexif.pl
291 lines (239 loc) · 7.73 KB
/
dn7toexif.pl
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
#!/usr/bin/perl -w
#
# dn7toexif.pl
#
# (C) 2008-2010 William Brodie-Tyrrell
# Released under GNU General Public License v3
#
# (C) 2020-2021 Vitor Fonseca
# Released under GNU General Public License v3
# http://www.vitorfonseca.com
#
# Parses DN0*.txt from DS-100 data saver and generates EXIF for scanned jpegs.
#
use Image::ExifTool;
use Image::ExifTool::Minolta;
$script_version = "v2.1";
###################################
# Start of user replaceable values
###################################
$camera_maker = "Minolta";
$camera_model = "Dynax 7"; # Replace by Dynax 7, Maxxum 7 or Alpha 7 depending on your model
$camera_serial = "00000000"; # Replace by your own serial
$artist_name = ""; # Replace by your own name
###################################
# End of user replaceable values
###################################
sub Help {
print "dn7toexif.pl $script_version\n";
print "dn7toexif.pl: Converts DN0 files to EXIF data in scanned jpegs\n";
print "(C) 2008-2010 William Brodie-Tyrrell\n";
print "(C) 2020-2022 Vitor Fonseca\n\n";
print "Usage: dn7toexif.pl pattern dn0-*.txt\n\n";
print "jpegs/tiffs named according to pattern must exist in current directory, with the\n";
print "following substitutions into the pattern:\n";
print "\@F becomes frame number (from 00 to 99)\n";
print "\@U becomes Up-no (from 00000 to 99999)\n";
print "\@R becomes roll number from the DNO-filename (if found)\n";
print "Also generates .xmp files\n";
exit;
}
sub FillPattern {
my($frame)=@_;
my($res);
$frame=sprintf("%02d", $frame);
$res=$pattern;
$res =~ s/\@F/$frame/ge;
$res;
}
# expected header contents
@hdr=('Frame', 'Shutter', 'FNo.', 'Lens', '+/-', 'PASM', 'Meter', 'FL +/-', 'yy/mm/dd', 'Time');
@hdr_field=('Frame', 'Shutter', 'FNo.', 'Lens', '+/-', 'PASM', 'Meter', 'FL', 'yy/mm/dd', 'Time');
# meter modes (Meter field)
%meters=('Multi', 'Multi-segment', 'Ave', 'Center-weighted average', 'Spot', 'Spot', 'OFF', 'Unknown');
# exposure modes (PASM field)
%exposures=('P','Program AE', 'A', 'Aperture-priority AE', 'S', 'Shutter speed priority AE', 'M', 'Manual');
if($#ARGV >= 0 && $ARGV[0] eq '-h' || $#ARGV < 1){
Help;
}
$patternarg=shift(@ARGV);
# discover images in the current directory
@jpegs=(<*.jpg>, <*.tif>, <*.dng>);
# iterate over all the given DNO files
for $dno (@ARGV){
open(DNO, $dno) || die "Can't open $dno: $!\n";
@dno=<DNO>;
close(DNO);
warn "Processing $dno\n";
# pre-fill pattern with roll-number
$pattern=$patternarg;
if($pattern =~ /\@R/){
if($dno =~ /\-0*(\d+)/){
$roll=$1;
$pattern =~ s/\@R/$roll/ge;
}
else{
warn "Roll number (\@R) specified in filename pattern but not parseable from $dno\n";
}
}
# check the file header
$hdr=shift(@dno);
if($hdr =~ /^dn(\d+)-(\d+),ISO:(\d+)/){
$iso= $3;
$hdr=shift(@dno);
warn "\nISO found $iso\n";
} else {
$hdr=shift(@dno);
}
@fields=split("\t", $hdr);
@fields=trim(@fields);
if(!(@fields eq @hdr)){
warn "bad header in $dno \n Fields @fields \n Header @hdr\n";
next;
}
# process all the frames listed in the file
for $frame (@dno){
$frame =~ s/^\s+//;
$frame =~ s/\s+$//;
next if(length($frame) == 0);
@fields=split("\t", $frame);
if($#fields != $#hdr){
warn "bad frame line, wrong field count\n";
warn "$#fields - $#hdr -> $hdr\n";
next;
}
%exif=();
# turn values into a hash
for $i (0 .. $#hdr){
$fname=$hdr_field[ $i ];
$value=$fields[ $i ];
$exif{ $fname } = $value;
}
# build pattern for searching for the matching jpeg,
# it is assumed to be of the form pattern-*.jpg
$fpat=FillPattern($exif{'Frame'});
@matches=grep(/$fpat/, @jpegs);
if($#matches < $[){
warn "No match for, skipping frame number ". $exif{'Frame'} ."\n";
next;
}
if($#matches > $[){
warn "Multiple matches for ". $matches[0] . ", skipping frame ". $exif{'Frame'} ."\n";
}
# decide on filenames
$fname = $matches[0];
$outfile = $fname;
$outfile =~ s/\.jpg$/-exif.jpg/;
$outfile =~ s/\.JPG$/-exif.JPG/;
$outfile =~ s/\.tif$/-exif.tif/;
$outfile =~ s/\.TIF$/-exif.TIF/;
$outfile =~ s/\.dng$/-exif.dng/;
$outfile =~ s/\.DNG$/-exif.DNG/;
$xmpfile=$fname;
$xmpfile =~ s/\.jpg/.xmp/;
$xmpfile =~ s/\.JPG/.xmp/;
$xmpfile =~ s/\.tif/.xmp/;
$xmpfile =~ s/\.TIF/.xmp/;
$xmpfile =~ s/\.dng/.xmp/;
$xmpfile =~ s/\.DNG/.xmp/;
unlink($outfile) if(-e $outfile);
unlink($xmpfile) if(-e $xmpfile);
warn "Modifying $fname into $outfile\n";
my $exifTool = new Image::ExifTool;
# set EXIF for shutter speed
$shutter=$exif{'Shutter'};
if($shutter =~ /^\s*(\d+)\"(\d+)\s*$/){
$value=$1+$2/(10^(length($2)));
$exifTool->SetNewValue('ShutterSpeedValue', $value);
$exifTool->SetNewValue('ExposureTime', $value);
} elsif($shutter =~ /^\s*(\d+)\"\s*$/){
$exifTool->SetNewValue('ShutterSpeedValue', $1);
$exifTool->SetNewValue('ExposureTime', $1);
} elsif($shutter =~ /^\s*(\d+)\s*$/){
$exifTool->SetNewValue('ShutterSpeedValue', 1.0/$1);
$exifTool->SetNewValue('ExposureTime', 1.0/$1);
} elsif($shutter =~ /Bulb/i){
# empty on purpose
} else {
warn "Bad shutter value '$shutter'\n";
next;
}
# f-number
if($exif{'FNo.'} =~ /(\d+\.?\d*)/){
$exifTool->SetNewValue('FNumber', $1);
}
# focal length / max aperture
if($exif{'Lens'} =~ /^\s*(\d+)\ \ \/(\d+\.?\d*)\s*$/){
$focal=$1;
$maxap=$2;
$exifTool->SetNewValue('FocalLength', $focal);
$exifTool->SetNewValue('FocalLengthIn35mmFormat', $focal);
$exifTool->SetNewValue('MaxApertureValue', $maxap);
} elsif($exif{'Lens'} =~ /^\s*(\d+)\ \/(\d+\.?\d*)\s*$/){
$focal=$1;
$maxap=$2;
$exifTool->SetNewValue('FocalLength', $focal);
$exifTool->SetNewValue('FocalLengthIn35mmFormat', $focal);
$exifTool->SetNewValue('MaxApertureValue', $maxap);
} elsif($exif{'Lens'} =~ /^\s*(\d+)\/(\d+\.?\d*)\s*$/){
$focal=$1;
$maxap=$2;
$exifTool->SetNewValue('FocalLength', $focal);
$exifTool->SetNewValue('FocalLengthIn35mmFormat', $focal);
$exifTool->SetNewValue('MaxApertureValue', $maxap);
} else {
warn "Bad lens format\n";
}
# image number
$exifTool->SetNewValue('ImageNumber', $exif{'Frame'});
# exposure compensation
$exifTool->SetNewValue('ExposureCompensation', trim($exif{'+/-'}));
# exposure mode
$exifTool->SetNewValue('ExposureProgram', $exposures{ trim($exif{'PASM'}) });
# metering mode
$exifTool->SetNewValue('MeteringMode', $meters{ trim($exif{'Meter'}) });
# Flash
if($exif{'FL'} =~ /(\d+\.\d)/) {
$exifTool->SetNewValue('Flash', 'Fired'); # Fired
$exifTool->SetNewValue('Minolta:FlashExposureComp', ${1});
} else {
$exifTool->SetNewValue('Flash', 'No Flash'); # No Flash
}
# ISO
$exifTool->SetNewValue('ISO', trim($iso));
# Date/Time
$datestr=$exif{'yy/mm/dd'} . ':' . $exif{'Time'};
# warn "date/time $datestr\n";
if($datestr =~ /^(\d\d\d\d)\/(\d\d)\/(\d\d):(\d+):(\d+)$/){
$datestr="${1}:${2}:${3} ${4}:${5}:00";
#warn "date/time $datestr\n";
$exifTool->SetNewValue('DateTimeOriginal', $datestr);
} else {
warn "Bad date/time format\n";
}
# AF mode - ???
# AF area - ???
# few tags not sure how to set
# global assumed settings
$exifTool->SetNewValue("Make", trim($camera_maker));
$exifTool->SetNewValue("FileSource", "Film Scanner");
$exifTool->SetNewValue("Model", trim($camera_model));
$exifTool->SetNewValue("CameraSerialNumber", trim($camera_serial));
$exifTool->SetNewValue("SerialNumber", trim($camera_serial));
$exifTool->SetNewValue("Artist", trim($artist_name));
$exifTool->SetNewValue("Copyright", trim($artist_name) . ", All rights reserved");
# write the EXIF to a new jpeg and to an XMP file
$exifTool->WriteInfo($fname, $outfile);
$exifTool->WriteInfo(undef, $xmpfile, 'XMP');
}
}
sub trim
{
my @out = @_;
for (@out)
{
s/^\s+//;
s/\s+$//;
}
return wantarray ? @out : $out[0];
}