PILL Image 从 base64 转换图像中检索 exif dict 以进行编辑和保存错误,dict return 为 NULL,有什么想法吗?

PILL Image retrieve exif dict from base64 converted images for editing and save error, dict return is NULL, any ideas?

我是第一次在这里提问,我希望这些信息是您正确理解问题所需的全部信息。提前感谢您抽出时间回答这个问题。

环境:Python 2.7.6, 枕头 3.4.2, Piexif 1.0.8

我有一个小脚本可以将图像从 base_64 转换为图像文件;在执行数组的 for 循环时,图像正确保存在服务器上;

 # write decoded image from base to jpg, works perfectly
 fh = open(image_name_location, "wb")
 fh.write(decodestring(image_src))
 fh.close() 

在这之后我想要相同的图像在 exif 中插入 gps 和日期信息,所以为此我使用 PIL 和 piexif;我使用PIL提取当前exif,使用piexif将新数据插入到图片的exif中;

 im = Image.open(image_name_location)

 #load existing exif to use as carrier
 exif_dict = piexif.load(im.info["exif"])

 #insert new information for gps and date
 exif_dict['GPS']['Latitude']= float(image_gps.split(',')[0])
 exif_dict['GPS']['Longitude']= float(image_gps.split(',')[1])
 exif_dict['0th']['Date and time (digitized)']= datetime.datetime.strptime(image_date, '%d/%m/%Y %H:%M').strftime("%Y:%m:%d %H:%M:%S")

  #convert exif to bytes
  exif_bytes = piexif.dump(exif_dict)

  #add to the image
  piexif.insert(exif_bytes, image_name_location)

  #close the image from memory
  im.close()

错误是,无论我做什么,以及我尝试获取 EXIF 当前字典的任何方式,它都会 return NULL;

有人知道吗?需要提及的是,在现有图像上,在将它们加载到 html 中后,此脚本可以正常工作,但由于某种原因,在制作图像时却没有。

我已更改并添加此代码以获取 exif:

try:
    exif_dict = piexif.load(im.info["exif"])

    #insert new information for gps and date
    exif_dict['GPS']['Latitude']= str(image_gps.split(',')[0])
    exif_dict['GPS']['Longitude']= str(image_gps.split(',')[1])
    exif_dict['0th']['Date and time (digitized)']= datetime.datetime.strptime(image_date, '%d/%m/%Y %H:%M').strftime("%Y:%m:%d %H:%M:%S")

    #close the image from memory
    im.close()

except:
    #create clean exif info for new image if none detected
    zeroth_ifd = {piexif.ImageIFD.Make: "Image maker",
                  piexif.ImageIFD.Software: "software here"
                  }
    exif_ifd = {piexif.ExifIFD.DateTimeOriginal: datetime.datetime.strptime(image_date, '%d/%m/%Y %H:%M').strftime("%Y:%m:%d %H:%M:%S"),
               piexif.ExifIFD.DateTimeDigitized: datetime.datetime.now().strftime("%Y:%m:%d %H:%M:%S")
                }
    gps_ifd = {piexif.GPSIFD.GPSVersionID: (2, 0, 0, 0),
               piexif.GPSIFD.GPSAltitudeRef: 1,
               piexif.GPSIFD.GPSLatitude: float(Fraction(str(image_gps.split(',')[0]))),
               piexif.GPSIFD.GPSLongitude: float(Fraction(str(image_gps.split(',')[1])))
               }

    exif_dict = {"0th":zeroth_ifd, "Exif":exif_ifd, "GPS":gps_ifd}

这解决了 NULL 的错误,因为如果 none,它有干净的 exif,但现在我在将 gps 数据从 float 转换为 tuple 时出现 piexif 错误??

最终解决方案,因此 PILL Image 在 none 时无法检索任何关于 exif 的内容,但 piexif 库的工作原理非常棒,如果找到 none 甚至会创建一个空的 exif。

添加此代码以替换 exif 计算:

            exif_dict = piexif.load(image_name_location)

            #convert GPS to degree, minutes, seconds for touple
            latitude_coo = Latitude(image_gps.split(',')[0]).to_string('d% %M% %S% %H').split()
            longitude_coo = Longitude(image_gps.split(',')[1]).to_string('d% %M% %S% %H').split()

            gps_ifd = {piexif.GPSIFD.GPSVersionID: (2, 0, 0, 0),
                       piexif.GPSIFD.GPSAltitudeRef: 1,
                       piexif.GPSIFD.GPSLatitude: ((int(latitude_coo[0]),1),(int(float(latitude_coo[1])*100),100),(int(float(latitude_coo[2])*100),100)),
                       piexif.GPSIFD.GPSLatitudeRef: latitude_coo[3],
                       piexif.GPSIFD.GPSLongitude: ((int(longitude_coo[0]),1),(int(float(longitude_coo[2])*100),100),(int(float(longitude_coo[2])*100),100)),
                       piexif.GPSIFD.GPSLongitudeRef: longitude_coo[3]
                       }

            #insert new information for gps and date
            exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal]= datetime.datetime.strptime(image_date, '%d/%m/%Y %H:%M').strftime("%Y:%m:%d %H:%M:%S")
            exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized]= datetime.datetime.now().strftime("%Y:%m:%d %H:%M:%S")
            exif_dict['0th'][piexif.ImageIFD.Make]= "Your test here"
            exif_dict['0th'][piexif.ImageIFD.Software]= "the software name"
            exif_dict['0th'][piexif.ImageIFD.DateTime]= datetime.datetime.strptime(image_date, '%d/%m/%Y %H:%M').strftime("%Y:%m:%d %H:%M:%S")
            exif_dict['GPS'] = gps_ifd

            #convert exif to bytes
            exif_bytes = piexif.dump(exif_dict)

            #add to the image
            piexif.insert(exif_bytes, image_name_location)

也用过这个库https://pypi.python.org/pypi/LatLon/1.0.2 to get the coordonates from lat,lon google format to degree, minutes, seconds, reference, and applied accoring to this books instruction on GPS Touple format: http://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf

值得:P