Keep time stamp when compressing image

My digital camera takes very high resolution pictures, and I have a PIL script to compress them to 800x600 (or 600x800). However, it would be nice if the resulting file retains the original timestamp. I noticed in the docs that I can use the File object instead of the name in the PIL image save method, but I don't know if this will help or not.

My code is basically name, ext = os.path.splitext (file name)

# open an image file (.bmp,.jpg,.png,.gif) you have in the working folder
image = Image.open(filename)

width = 800
height = 600

w, h = image.size
if h > w:
    width = 600
    height = 800

name = name + ".jpg"
shunken = image.resize((width, height), Image.ANTIALIAS)
shunken.save(name)

Thanks for any help you can give!

+3
source share
1 answer

Use shutil.copystat

, PIL EXIF. EXIF ​​ Python, pyexiv2. Phatch, , Python, , EXIF.

, Ubuntu, , , pyexiv2 python-pyexiv2.

: EXIF ​​ EXIF ​​ , pyexiv2 , . :

import os
import time
import Image
import ExifTags   # This is provided by PIL
img=Image.open(filename,'r')

PIL EXIF, EXIF. _getexif():

d = dict((ExifTags.TAGS[k], v) for k, v in img._getexif().items())
print(d['DateTimeOriginal'])

, . ; YMMV. dateutils , .

timestamp=time.strptime(d['DateTimeOriginal'],"%Y:%m:%d %H:%M:%S")

:

w, h = img.size
width,height = 800,600
if h > w: width,height = height,width

os.utime atime mtime:

filename = filename + "-800x600.jpg"
shunken = img.resize((width, height), Image.ANTIALIAS)
shunken.save(filename)
st = os.stat(filename)
os.utime(filename,(st.st_atime,time.mktime(timestamp)))
+5

Source: https://habr.com/ru/post/1741547/


All Articles