Python : Creation de PNG à base de CSV, avec des EXIF de localisation

J’ai fait un petit programme pour faire des PNG à l’aide d’un fichier CSV. Le but est de mettre des balises sur Google Picture.

Voici un exemple de fichier CSV :

$ cat list.csv 
Nom,Année,Mois,Jours,Lieux,Pays
Deplacement sur Paris,2018,11,01,Paris,France
Deplacement sur Londres,2011,11,01,London,UK

Voici le programme :

from PIL import Image, ImageDraw, ImageFont
import piexif
from datetime import datetime
import csv
from geopy.geocoders import Nominatim
from GPSPhoto import gpsphoto

geolocator = Nominatim(user_agent="Your_Name")

with open('list.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            line_count += 1
        else:
            name = '%s \n Le %d/%d/%d \n a %s,%s . :' % (str(row[0]),int(row[1]),int(row[2]),int(row[3]),str(row[4]),str(row[5]))
            print('\t %s ' % name)
            line_count += 1
            filename = 'image-%d.jpg' % (line_count)

            img = Image.new('RGB', (1024, 800), color = (73, 109, 137))

            d = ImageDraw.Draw(img)
            fontsize = 80 
            font = ImageFont.truetype('/usr/share/fonts/truetype/msttcorefonts/Arial.ttf', fontsize)
            d.text((10,10), name, font=font)
            img.save(filename)

            exif_dict = piexif.load(filename)
            new_date = datetime(int(row[1]), int(row[2]), int(row[3]), 0, 0, 0).strftime("%Y:%m:%d %H:%M:%S")
            exif_dict['0th'][piexif.ImageIFD.DateTime] = new_date
            exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = new_date
            exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = new_date
            exif_bytes = piexif.dump(exif_dict)
            piexif.insert(exif_bytes, filename)
            address= '%s,%s' % (row[4], row[5])
            location = geolocator.geocode(address)
            print('\t\t %f %f %d' % (location.latitude, location.longitude, location.altitude))
            photo = gpsphoto.GPSPhoto(filename)
            info = gpsphoto.GPSInfo((location.latitude, location.longitude), alt=int(location.altitude), timeStamp=new_date)
            photo.modGPSData(info, filename)

    print('Processed %d lines.' % line_count)


Pour qu’il fonctionne il faut avoir installé :

# pip install geopy
# pip install GPSPhoto
# pip install exifread

Pour l’installation des fonts sous Ubuntu :

# sudo apt-get install gsfonts gsfonts-other gsfonts-x11 ttf-mscorefonts-installer t1-xfree86-nonfree fonts-alee ttf-ancient-fonts fonts-arabeyes fonts-arphic-bkai00mp fonts-arphic-bsmi00lp fonts-arphic-gbsn00lp fonts-arphic-gkai00mp fonts-atarismall fonts-dustin fonts-f500 fonts-sil-gentium ttf-georgewilliams ttf-isabella fonts-larabie-deco fonts-larabie-straight fonts-larabie-uncommon ttf-sjfonts ttf-staypuft ttf-summersby fonts-ubuntu-title ttf-xfree86-nonfree xfonts-intl-european xfonts-jmk xfonts-terminus

Pour vérifier :

# exiftool image-2.jpg 
ExifTool Version Number         : 11.88
File Name                       : image-2.jpg
Directory                       : .
File Size                       : 36 kB
File Modification Date/Time     : 2021:02:22 14:55:29+01:00
File Access Date/Time           : 2021:02:22 14:55:30+01:00
File Inode Change Date/Time     : 2021:02:22 14:55:29+01:00
File Permissions                : rw-rw-r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
JFIF Version                    : 1.01
Resolution Unit                 : None
X Resolution                    : 1
Y Resolution                    : 1
Exif Byte Order                 : Big-endian (Motorola, MM)
Modify Date                     : 2018:11:01 00:00:00
Date/Time Original              : 2018:11:01 00:00:00
Create Date                     : 2018:11:01 00:00:00
GPS Latitude Ref                : North
GPS Longitude Ref               : East
GPS Altitude Ref                : Above Sea Level
GPS Time Stamp                  : 00:00:00
GPS Processing Method           : GPS
GPS Date Stamp                  : 2018:11:01
Image Width                     : 1024
Image Height                    : 800
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:2:0 (2 2)
Image Size                      : 1024x800
Megapixels                      : 0.819
GPS Altitude                    : 0 m Above Sea Level
GPS Date/Time                   : 2018:11:01 00:00:00Z
GPS Latitude                    : 48 deg 51' 24.11" N
GPS Longitude                   : 2 deg 21' 5.26" E
GPS Position                    : 48 deg 51' 24.11" N, 2 deg 21' 5.26" E

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Time limit is exhausted. Please reload CAPTCHA.