1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from mediagoblin
import mg_globals
as mgg
21 from mediagoblin
.processing
import BadMediaFail
, \
22 create_pub_filepath
, THUMB_SIZE
, MEDIUM_SIZE
23 from mediagoblin
.tools
.exif
import exif_fix_image_orientation
, \
24 extract_exif
, clean_exif
, get_gps_data
, get_useful
26 def process_image(entry
):
28 Code to process an image
30 workbench
= mgg
.workbench_manager
.create_workbench()
31 # Conversions subdirectory to avoid collisions
32 conversions_subdir
= os
.path
.join(
33 workbench
.dir, 'conversions')
34 os
.mkdir(conversions_subdir
)
36 queued_filepath
= entry
.queued_media_file
37 queued_filename
= workbench
.localized_file(
38 mgg
.queue_store
, queued_filepath
,
41 filename_bits
= os
.path
.splitext(queued_filename
)
42 basename
= os
.path
.split(filename_bits
[0])[1]
43 extension
= filename_bits
[1].lower()
46 exif_tags
= extract_exif(queued_filename
)
47 gps_data
= get_gps_data(exif_tags
)
50 thumb
= Image
.open(queued_filename
)
54 thumb
= exif_fix_image_orientation(thumb
, exif_tags
)
56 thumb
.thumbnail(THUMB_SIZE
, Image
.ANTIALIAS
)
58 # Copy the thumb to the conversion subdir, then remotely.
59 thumb_filename
= 'thumbnail' + extension
60 thumb_filepath
= create_pub_filepath(entry
, thumb_filename
)
62 tmp_thumb_filename
= os
.path
.join(
63 conversions_subdir
, thumb_filename
)
65 with
file(tmp_thumb_filename
, 'w') as thumb_file
:
66 thumb
.save(thumb_file
)
68 mgg
.public_store
.copy_local_to_storage(
69 tmp_thumb_filename
, thumb_filepath
)
71 # If the size of the original file exceeds the specified size of a `medium`
72 # file, a `medium.jpg` files is created and later associated with the media
74 medium
= Image
.open(queued_filename
)
77 medium
= exif_fix_image_orientation(medium
, exif_tags
)
79 if medium
.size
[0] > MEDIUM_SIZE
[0] or medium
.size
[1] > MEDIUM_SIZE
[1]:
80 medium
.thumbnail(MEDIUM_SIZE
, Image
.ANTIALIAS
)
82 medium_filename
= 'medium' + extension
83 medium_filepath
= create_pub_filepath(entry
, medium_filename
)
85 tmp_medium_filename
= os
.path
.join(
86 conversions_subdir
, medium_filename
)
88 with
file(tmp_medium_filename
, 'w') as medium_file
:
89 medium
.save(medium_file
)
91 mgg
.public_store
.copy_local_to_storage(
92 tmp_medium_filename
, medium_filepath
)
94 # we have to re-read because unlike PIL, not everything reads
95 # things in string representation :)
96 queued_file
= file(queued_filename
, 'rb')
99 #create_pub_filepath(entry, queued_filepath[-1])
100 original_filepath
= create_pub_filepath(entry
, basename
+ extension
)
102 with mgg
.public_store
.get_file(original_filepath
, 'wb') \
104 original_file
.write(queued_file
.read())
106 # Remove queued media file from storage and database
107 mgg
.queue_store
.delete_file(queued_filepath
)
108 entry
.queued_media_file
= []
110 # Insert media file information into database
111 media_files_dict
= entry
.setdefault('media_files', {})
112 media_files_dict
['thumb'] = thumb_filepath
113 media_files_dict
['original'] = original_filepath
114 media_files_dict
['medium'] = medium_filepath
116 # Insert exif data into database
117 media_data
= entry
.setdefault('media_data', {})
118 media_data
['exif'] = {
119 'clean': clean_exif(exif_tags
)}
120 media_data
['exif']['useful'] = get_useful(
121 media_data
['exif']['clean'])
122 media_data
['gps'] = gps_data
125 workbench
.destroy_self()
127 if __name__
== '__main__':
131 pp
= pprint
.PrettyPrinter()
133 result
= extract_exif(sys
.argv
[1])
134 gps
= get_gps_data(result
)
135 clean
= clean_exif(result
)
136 useful
= get_useful(clean
)