本文整理汇总了Python中piexif.insert方法的典型用法代码示例。如果您正苦于以下问题:Python piexif.insert方法的具体用法?Python piexif.insert怎么用?Python piexif.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类piexif
的用法示例。
在下文中一共展示了piexif.insert方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_remove_m
# 需要导入模块: import piexif [as 别名]
# 或者: from piexif import insert [as 别名]
def test_remove_m(self):
"""'remove' on memory.
"""
o = io.BytesIO()
with self.assertRaises(ValueError):
piexif.remove(I1)
piexif.remove(I1, o)
exif_dict = piexif.load(o.getvalue())
none_dict = {"0th":{},
"Exif":{},
"GPS":{},
"Interop":{},
"1st":{},
"thumbnail":None}
self.assertEqual(exif_dict, none_dict)
Image.open(o).close()
# insert ------
示例2: add_gps_tags
# 需要导入模块: import piexif [as 别名]
# 或者: from piexif import insert [as 别名]
def add_gps_tags(path: str, gps_tags: {str: any}):
"""This method will add gps tags to the photo found at path"""
exif_dict = piexif.load(path)
for tag, tag_value in gps_tags.items():
exif_dict["GPS"][tag] = tag_value
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, path)
示例3: set_photo_exif
# 需要导入模块: import piexif [as 别名]
# 或者: from piexif import insert [as 别名]
def set_photo_exif(path, date):
"""Set EXIF date on a photo, do nothing if there is an error"""
try:
exif_dict = piexif.load(path)
exif_dict.get("1st")[306] = date
exif_dict.get("Exif")[36867] = date
exif_dict.get("Exif")[36868] = date
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, path)
except (ValueError, InvalidImageDataError):
logger = setup_logger()
logger.debug("Error setting EXIF data for %s", path)
return
示例4: write
# 需要导入模块: import piexif [as 别名]
# 或者: from piexif import insert [as 别名]
def write(self, filename=None):
"""Save exif data to file."""
if filename is None:
filename = self._filename
exif_bytes = piexif.dump(self._ef)
with open(self._filename, "rb") as fin:
img = fin.read()
try:
piexif.insert(exif_bytes, img, filename)
except IOError:
type, value, traceback = sys.exc_info()
print >> sys.stderr, "Error saving file:", value
示例5: test_roundtrip_files
# 需要导入模块: import piexif [as 别名]
# 或者: from piexif import insert [as 别名]
def test_roundtrip_files(self):
files = glob.glob(os.path.join("tests", "images", "r_*.jpg"))
for input_file in files:
print(input_file)
exif = piexif.load(input_file)
exif_bytes = piexif.dump(exif)
o = io.BytesIO()
piexif.insert(exif_bytes, input_file, o)
e = piexif.load(o.getvalue())
t = e.pop("thumbnail")
thumbnail = exif.pop("thumbnail")
if t is not None:
if not (b"\xe0" <= thumbnail[3:4] <= b"\xef"):
self.assertEqual(t, thumbnail)
else:
print("Given JPEG doesn't follow exif thumbnail standard. "
"APPn segments in thumbnail should be removed, "
"whereas thumbnail JPEG has it. \n: " +
input_file)
exif["1st"].pop(513)
e["1st"].pop(513)
exif["1st"].pop(514)
e["1st"].pop(514)
for ifd in e:
if ifd == "0th":
if ImageIFD.ExifTag in exif["0th"]:
exif["0th"].pop(ImageIFD.ExifTag)
e["0th"].pop(ImageIFD.ExifTag)
if ImageIFD.GPSTag in exif["0th"]:
exif["0th"].pop(ImageIFD.GPSTag)
e["0th"].pop(ImageIFD.GPSTag)
elif ifd == "Exif":
if ExifIFD.InteroperabilityTag in exif["Exif"]:
exif["Exif"].pop(ExifIFD.InteroperabilityTag)
e["Exif"].pop(ExifIFD.InteroperabilityTag)
for key in exif[ifd]:
self.assertEqual(exif[ifd][key], e[ifd][key])
print(" - pass")
# transplant ------
示例6: test_insert
# 需要导入模块: import piexif [as 别名]
# 或者: from piexif import insert [as 别名]
def test_insert(self):
exif_dict = {"0th":ZEROTH_IFD, "Exif":EXIF_IFD, "GPS":GPS_IFD}
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, INPUT_FILE1, "insert.jpg")
exif = load_exif_by_PIL("insert.jpg")
piexif.insert(exif_bytes, NOEXIF_FILE, "insert.jpg")
with self.assertRaises(ValueError):
piexif.insert(b"dummy", io.BytesIO())
piexif.insert(exif_bytes, "insert.jpg")
os.remove("insert.jpg")
示例7: test_insert_m
# 需要导入模块: import piexif [as 别名]
# 或者: from piexif import insert [as 别名]
def test_insert_m(self):
"""'insert' on memory.
"""
exif_dict = {"0th":ZEROTH_IFD, "Exif":EXIF_IFD, "GPS":GPS_IFD}
exif_bytes = piexif.dump(exif_dict)
o = io.BytesIO()
piexif.insert(exif_bytes, I1, o)
self.assertEqual(o.getvalue()[0:2], b"\xff\xd8")
exif = load_exif_by_PIL(o)
示例8: test_insert_fail1
# 需要导入模块: import piexif [as 别名]
# 或者: from piexif import insert [as 别名]
def test_insert_fail1(self):
with open(INPUT_FILE1, "rb") as f:
data = f.read()
with open("insert.jpg", "wb+") as f:
f.write(data)
exif_dict = {"0th":ZEROTH_IFD, "Exif":EXIF_IFD, "GPS":GPS_IFD}
exif_bytes = piexif.dump(exif_dict)
with self.assertRaises(ValueError):
piexif.insert(exif_bytes, INPUT_FILE_TIF)
os.remove("insert.jpg")
示例9: test_insert_exif
# 需要导入模块: import piexif [as 别名]
# 或者: from piexif import insert [as 别名]
def test_insert_exif(self):
"""Can PIL open WebP that is inserted exif?"""
IMAGE_DIR = "tests/images/"
OUT_DIR = "tests/images/out/"
files = [
"tool1.webp",
"pil1.webp",
"pil2.webp",
"pil3.webp",
"pil_rgb.webp",
"pil_rgba.webp",
]
exif_dict = {
"0th":{
piexif.ImageIFD.Software: b"PIL",
piexif.ImageIFD.Make: b"Make",
}
}
for filename in files:
try:
Image.open(IMAGE_DIR + filename)
except:
print("Pillow can't read {}".format(filename))
continue
with open(IMAGE_DIR + filename, "rb") as f:
data = f.read()
exif_bytes = piexif.dump(exif_dict)
exif_inserted = _webp.insert(data, exif_bytes)
with open(OUT_DIR + "i_" + filename, "wb") as f:
f.write(exif_inserted)
Image.open(OUT_DIR + "i_" + filename)
示例10: download_photos
# 需要导入模块: import piexif [as 别名]
# 或者: from piexif import insert [as 别名]
def download_photos():
ssl._create_default_https_context = ssl._create_unverified_context
#Prep the download folder
folder = 'photos/'
if not os.path.exists(folder):
os.makedirs(folder)
print("Saving photos to " + folder)
#Download the photos
with open('tagged.json') as json_file:
data = json.load(json_file)
for i,d in enumerate(data['tagged']):
if d['media_type'] == 'image':
#Save new file
if d['fb_date'] == "Today":
filename_date = datetime.today().strftime('%Y-%m-%d')
elif d['fb_date'] == "Yesterday":
filename_date = datetime.today() - timedelta(days=1)
filename_date = filename_date.strftime('%Y-%m-%d')
else:
filename_date = parse(d['fb_date']).strftime("%Y-%m-%d")
img_id = d['media_url'].split('_')[1]
new_filename = folder + filename_date + '_' + img_id + '.jpg'
if os.path.exists(new_filename):
print("Already Exists (Skipping): %s" % (new_filename))
else:
delay = 1
while True:
try:
print("Downloading " + d['media_url'])
img_file = wget.download(d['media_url'], new_filename, False)
break
except (TimeoutError, urllib.error.URLError) as e:
print("Sleeping for {} seconds".format(delay))
time.sleep(delay)
delay *= 2
#Update EXIF Date Created
exif_dict = piexif.load(img_file)
if d['fb_date'] == "Today":
exif_date = datetime.today().strftime("%Y:%m:%d %H:%M:%S")
elif d['fb_date'] == "Yesterday":
exif_date = datetime.today() - timedelta(days=1)
exif_date = exif_date.strftime("%Y:%m:%d %H:%M:%S")
else:
exif_date = parse(d['fb_date']).strftime("%Y:%m:%d %H:%M:%S")
img_desc = d['fb_caption'] + '\n' + d['fb_tags'] + '\n' + d['fb_url'].split("&")[0]
exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = exif_date
exif_dict['0th'][piexif.ImageIFD.Copyright] = (d['user_name'] + ' (' + d['user_url']) + ')'
exif_dict['0th'][piexif.ImageIFD.ImageDescription] = img_desc.encode('utf-8')
piexif.insert(piexif.dump(exif_dict), img_file)
print(str(i+1) + ') Added '+ new_filename)
示例11: recover
# 需要导入模块: import piexif [as 别名]
# 或者: from piexif import insert [as 别名]
def recover(self):
print("recovering:", self.file_dir)
# 0th #
self.copy_exif("0th", piexif.ImageIFD.Make,
self.raw_info, "exif", "make", piexif.TYPES.Ascii)
self.copy_exif("0th", piexif.ImageIFD.Model,
self.raw_info, "exif", "model", piexif.TYPES.Ascii)
# Exif #
self.copy_exif("Exif", piexif.ExifIFD.ExposureBiasValue,
self.raw_info, "exif", "exposureCompensation", piexif.TYPES.SRational)
self.copy_exif("Exif", piexif.ExifIFD.ExposureMode,
self.raw_info, "exif", "exposureMode", piexif.TYPES.Short)
self.copy_exif("Exif", piexif.ExifIFD.ExposureProgram,
self.raw_info, "exif", "exposureProgram", piexif.TYPES.Short)
self.copy_exif("Exif", piexif.ExifIFD.ExposureTime,
self.raw_info, "exif", "exposureTime", piexif.TYPES.SRational)
self.copy_exif("Exif", piexif.ExifIFD.Flash,
self.raw_info, "exif", "flash", piexif.TYPES.Short)
self.copy_exif("Exif", piexif.ExifIFD.FNumber,
self.raw_info, "exif", "fnumber", piexif.TYPES.Rational)
self.copy_exif("Exif", piexif.ExifIFD.FocalLength,
self.raw_info, "exif", "focalLength", piexif.TYPES.Rational)
self.copy_exif("Exif", piexif.ExifIFD.ISOSpeed,
self.raw_info, "exif", "iso", piexif.TYPES.Long)
self.copy_exif("Exif", piexif.ExifIFD.LensModel,
self.raw_info, "exif", "lensModel", piexif.TYPES.Ascii)
self.copy_exif("Exif", piexif.ExifIFD.MeteringMode,
self.raw_info, "exif", "meteringMode", piexif.TYPES.Short)
# Exif: OriginalTime #
if not self.copy_exif("Exif", piexif.ExifIFD.DateTimeOriginal,
self.raw_info, "exif", "originalTime", piexif.TYPES.Ascii):
self.coyp_DateTimeOriginal_from_uploadtime() # if originalTime is missing
# GPS #
if self.copy_exif("GPS", piexif.GPSIFD.GPSLongitude,
self.floatview_info, "shootGeo", "pos_x", "GPSPos"):
# 拍摄地点在东半球是参考东经的;在西半球如参考东经则经度是负数
self.add_exif("GPS", piexif.GPSIFD.GPSLongitudeRef, "E")
if self.copy_exif("GPS", piexif.GPSIFD.GPSLatitude,
self.floatview_info, "shootGeo", "pos_y", "GPSPos"):
# 拍摄地点在北半球是参考北纬的;在南半球如参考北纬则维度是负数
self.add_exif("GPS", piexif.GPSIFD.GPSLatitudeRef, "N")
if self.is_dirty:
exif_bytes = piexif.dump(self.exif_dict)
piexif.insert(exif_bytes, self.file_dir)