コード例 #1
0
ファイル: image_io.py プロジェクト: mario-s/exif_edit
class Writer:
    """This class writes the edited Exif Tags back to the image."""
    def __init__(self, image, origin_dict=None):
        self.image = image
        self.origin_dict = {} if origin_dict is None else origin_dict
        self.converter = Converter()

    def save(self, rows, img_path):
        """
        Saves the the collection of Exif tags to a file given by the path.
        """
        self.__set_values(Converter.to_dict(rows))
        self.__save(img_path)

    def __set_values(self, dic):
        for key, value in dic.items():
            if Writer.__is_valid(key, value):
                self.__set_value(key, value)
                # updated an existing element, so remove from source
                if key in self.origin_dict:
                    self.origin_dict.pop(key)

        #the elements which survived above loop, are those which were deleted
        self.__delete_tags()

    def __set_value(self, key, value):
        try:
            val = self.converter.to_exif(key, value)
            self.image[key] = val
        except Exception as exc:
            logging.warning("exception while setting new value: %s", exc)

    @staticmethod
    def __is_valid(key, value):
        return key not in ExifFilter.read_only() and value is not None

    def __delete_tags(self):
        #we need to iterate through each and check if we allowed to delete it
        locked = ExifFilter.locked()
        for key, _ in self.origin_dict.items():
            if key not in locked:
                self.image.delete(key)

    def __save(self, img_path):
        with open(img_path, 'wb') as file:
            file.write(self.image.get_file())
コード例 #2
0
 def test_to_exif_fallback(self):
     res = Converter.to_exif('', 'a')
     self.assertEqual(res, 'a')
コード例 #3
0
 def test_to_exif_int(self):
     res = Converter.to_exif('', 12)
     self.assertIsInstance(res, int)
コード例 #4
0
 def test_to_exif_dms(self):
     loc = DegreeFormatFactory.create([78.0, 55.0, 44.33324])
     res = Converter.to_exif('', loc)
     self.assertIsInstance(res, tuple)
コード例 #5
0
 def test_convert_orientations_none(self):
     self.assertEqual(Orientation.TOP_LEFT,
                      Converter.to_exif("orientation", None))
コード例 #6
0
 def test_convert_orientations_default(self):
     self.assertEqual(Orientation.TOP_LEFT,
                      Converter.to_exif("orientation", "9"))
コード例 #7
0
 def test_convert_orientations_numeric(self):
     self.assertEqual(Orientation.TOP_RIGHT,
                      Converter.to_exif("orientation", "2"))
コード例 #8
0
 def test_convert_orientations(self):
     self.assertEqual(Orientation.LEFT_BOTTOM,
                      Converter.to_exif("orientation", "LEFT_BOTTOM"))
コード例 #9
0
 def test_convert_resolution_unit_inches(self):
     self.assertEqual(ResolutionUnit.INCHES,
                      Converter.to_exif("resolution_unit", "1"))
コード例 #10
0
 def test_convert_resolution_unit_centimeters(self):
     self.assertEqual(ResolutionUnit.CENTIMETERS,
                      Converter.to_exif("resolution_unit", "CENTIMETERS"))
コード例 #11
0
 def test_convert_color_space_uncalibrated(self):
     self.assertEqual(ColorSpace.UNCALIBRATED,
                      Converter.to_exif("color_space", "UNCALIBRATED"))
コード例 #12
0
 def test_convert_color_space_srgb(self):
     self.assertEqual(ColorSpace.SRGB,
                      Converter.to_exif("color_space", "SRGB"))
コード例 #13
0
 def test_convert_unknown(self):
     self.assertEqual(1, Converter.to_exif('foo', 1))