使用Paperclip从图片中获取GPS元数据

时间:2022-01-16 16:52:04

I have a question about Paperclip. Many modern cameras and phones add GPS metadata to a photo when taken:

我有一个关于Paperclip的问题。拍摄时,许多现代相机和手机都会为照片添加GPS元数据:

使用Paperclip从图片中获取GPS元数据

Users of my Rails app can upload photos and manually add the location data to them. However, this sucks as users don't want to enter that: they want it to be there automatically.

我的Rails应用程序的用户可以上传照片并手动将位置数据添加到他们。但是,这很糟糕,因为用户不想输入它:他们希望它自动存在。

Can, and how can I get GPS metadata from a photo (always JPEG) with Paperclip and Ruby on Rails? Can it be done with Paperclip or do I need another gem?

可以,如何通过Paperclip和Ruby on Rails从照片(总是JPEG)中获取GPS元数据?可以用Paperclip完成还是需要另一个宝石?

4 个解决方案

#1


18  

What you need is being able to extract EXIF information from the picture.

您需要的是能够从图片中提取EXIF信息。

There is a gem that aims to do just that: exifr

有一颗宝石旨在做到这一点:exifr

You can install it automatically with the following bundler line:

您可以使用以下捆绑线自动安装它:

gem 'exifr'

EDIT:

编辑:

I have personally switched to this fork of the same gem, which contains GPS helpers:

我亲自切换到同一个宝石的叉子,其中包含GPS助手:

gem 'exifr', :git => 'git://github.com/picuous/exifr.git'

Use example:

使用示例:

EXIFR::JPEG.new('IMG_6841.JPG').gps?                # => true
EXIFR::JPEG.new('IMG_6841.JPG').gps                 # => [37.294112,-122.789422]
EXIFR::JPEG.new('IMG_6841.JPG').gps_lat             # => 37.294112
EXIFR::JPEG.new('IMG_6841.JPG').gps_lng             # =>-122.789422

#2


1  

you can't do it with paperclip. rmagick will help you neither. The only way I see is:

你不能用回形针做到这一点。 rmagick也不会帮助你。我看到的唯一方法是:

raw_props = %x[identify -format '%[exif:*]' #{path_to_image}].split("\n").collect{|a| a.split("=")}
properties = Hash[*raw_props.collect { |v| [v, v*2]}.flatten]

(pre: imagemagick installed and in the path, check with 'which identify')

(pre:imagemagick已安装并在路径中,请使用'which identify'进行检查)

#3


1  

Just an update to the accepted answer above, which greatly helped me to get EXIFR working but has few issues:

只是上面接受的答案的更新,这极大地帮助我让EXIFR工作,但几乎没有问题:

  1. To check if an image has GPS, I had to use EXIFR::JPEG.new('IMG_6841.JPG').exif?

    要检查图像是否有GPS,我必须使用EXIFR :: JPEG.new('IMG_6841.JPG')。exif?

  2. To pull the latitude I used EXIFR::JPEG.new('IMG_6841.JPG').gps_latitude

    为了拉纬度,我使用了EXIFR :: JPEG.new('IMG_6841.JPG')。gps_latitude

  3. And longitude is EXIFR::JPEG.new('IMG_6841.JPG').gps_longitude

    经度是EXIFR :: JPEG.new('IMG_6841.JPG')。gps_longitude

Small issues but they had me spinning for a bit and might save someone time down the road.

小问题,但他们让我旋转一点,可能会节省一些时间。

Also, once I got my GPS coordinates from my photo I found this blog entry to help convert them from an array to Google Map friendly coordinates (i.e. 34.360826, -110.239368): http://www.cloudspace.com/blog/2009/02/16/decoding-gps-latitude-and-longitude-from-exif-in-ruby/

此外,一旦我从我的照片中获得GPS坐标,我发现此博客条目有助于将它们从阵列转换为Google Map友好坐标(即34.360826,-110.239368):http://www.cloudspace.com/blog/2009/ 2月16日/解码-GPS纬度和经度从 - EXIF合红宝石/

#4


0  

If you want use "higher level" solution (no exif directly), there is a gem https://github.com/aufi/photo_geoloader which loads latitude, longitude and altitude and can place it into rails model (in callback)

如果你想使用“更高级别”的解决方案(没有exif直接),有一个宝石https://github.com/aufi/photo_geoloader,它可以加载纬度,经度和海拔高度,并可以将它放入rails模型中(在回调中)

class Photo < ActiveRecord::Base
  before_create do
    PhotoGeoloader.new(photo.path).place_attributes self
  end
  ...

#1


18  

What you need is being able to extract EXIF information from the picture.

您需要的是能够从图片中提取EXIF信息。

There is a gem that aims to do just that: exifr

有一颗宝石旨在做到这一点:exifr

You can install it automatically with the following bundler line:

您可以使用以下捆绑线自动安装它:

gem 'exifr'

EDIT:

编辑:

I have personally switched to this fork of the same gem, which contains GPS helpers:

我亲自切换到同一个宝石的叉子,其中包含GPS助手:

gem 'exifr', :git => 'git://github.com/picuous/exifr.git'

Use example:

使用示例:

EXIFR::JPEG.new('IMG_6841.JPG').gps?                # => true
EXIFR::JPEG.new('IMG_6841.JPG').gps                 # => [37.294112,-122.789422]
EXIFR::JPEG.new('IMG_6841.JPG').gps_lat             # => 37.294112
EXIFR::JPEG.new('IMG_6841.JPG').gps_lng             # =>-122.789422

#2


1  

you can't do it with paperclip. rmagick will help you neither. The only way I see is:

你不能用回形针做到这一点。 rmagick也不会帮助你。我看到的唯一方法是:

raw_props = %x[identify -format '%[exif:*]' #{path_to_image}].split("\n").collect{|a| a.split("=")}
properties = Hash[*raw_props.collect { |v| [v, v*2]}.flatten]

(pre: imagemagick installed and in the path, check with 'which identify')

(pre:imagemagick已安装并在路径中,请使用'which identify'进行检查)

#3


1  

Just an update to the accepted answer above, which greatly helped me to get EXIFR working but has few issues:

只是上面接受的答案的更新,这极大地帮助我让EXIFR工作,但几乎没有问题:

  1. To check if an image has GPS, I had to use EXIFR::JPEG.new('IMG_6841.JPG').exif?

    要检查图像是否有GPS,我必须使用EXIFR :: JPEG.new('IMG_6841.JPG')。exif?

  2. To pull the latitude I used EXIFR::JPEG.new('IMG_6841.JPG').gps_latitude

    为了拉纬度,我使用了EXIFR :: JPEG.new('IMG_6841.JPG')。gps_latitude

  3. And longitude is EXIFR::JPEG.new('IMG_6841.JPG').gps_longitude

    经度是EXIFR :: JPEG.new('IMG_6841.JPG')。gps_longitude

Small issues but they had me spinning for a bit and might save someone time down the road.

小问题,但他们让我旋转一点,可能会节省一些时间。

Also, once I got my GPS coordinates from my photo I found this blog entry to help convert them from an array to Google Map friendly coordinates (i.e. 34.360826, -110.239368): http://www.cloudspace.com/blog/2009/02/16/decoding-gps-latitude-and-longitude-from-exif-in-ruby/

此外,一旦我从我的照片中获得GPS坐标,我发现此博客条目有助于将它们从阵列转换为Google Map友好坐标(即34.360826,-110.239368):http://www.cloudspace.com/blog/2009/ 2月16日/解码-GPS纬度和经度从 - EXIF合红宝石/

#4


0  

If you want use "higher level" solution (no exif directly), there is a gem https://github.com/aufi/photo_geoloader which loads latitude, longitude and altitude and can place it into rails model (in callback)

如果你想使用“更高级别”的解决方案(没有exif直接),有一个宝石https://github.com/aufi/photo_geoloader,它可以加载纬度,经度和海拔高度,并可以将它放入rails模型中(在回调中)

class Photo < ActiveRecord::Base
  before_create do
    PhotoGeoloader.new(photo.path).place_attributes self
  end
  ...