前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Excel催化剂开源第36波-图片Exif信息提取,速度超快,信息超全

Excel催化剂开源第36波-图片Exif信息提取,速度超快,信息超全

作者头像
Excel催化剂
发布2021-08-19 15:50:00
4110
发布2021-08-19 15:50:00
举报
文章被收录于专栏:Excel催化剂

Excel催化剂在文件处理方面,功能做到极致,但其实很大功劳都是引用一些开源社区的轮子库,不敢独占好处,此篇给大家分享下抓取图片的Exif信息的好用的轮子。

此篇对应的Excel催化剂功能实现:第83波-遍历文件夹内文件信息特别是图像、音视频等特有信息 https://www.jianshu.com/p/ad98adc64f0b

当然再次强调,找东西尽量用google,百度是非常低效,找出来的代码,好多也不适用,吐槽下百度最喜欢收录CSDN的文章,只是物以类聚,垃圾对垃圾,广告婊子一个。真要搜索,建议还是加上site:cnblogs.com。

正式介绍主角,图片Exif信息的读取,就靠MetadataExtractor完成,在nuget上直接有。

MetadataExtractor类库

调用方法非常简洁,一句代码完成,其他代码都是用来提取信息,处理字符串、集合之类的。

核心代码如下:

代码语言:javascript
复制
 IEnumerable<MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(filePath);

以上是笔者对Exif感兴趣的内容作的提取,只需linq和正则就可以游刃有余。

代码语言:javascript
复制
   IEnumerable<MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(filePath);

                string widthStr = directories.Where(s => s.Name != "File").Select(k => k.Tags.FirstOrDefault(t => t.Name == "Image Width")).FirstOrDefault().Description;
                dr[ColNameOfImageWidth] = int.Parse(Regex.Match(widthStr, "\\d+").Value);

                string heightStr = directories.Where(s => s.Name != "File").Select(k => k.Tags.FirstOrDefault(t => t.Name == "Image Height")).FirstOrDefault().Description;
                dr[ColNameOfImageHeight] = int.Parse(Regex.Match(heightStr, "\\d+").Value);

                if (Path.GetExtension(filePath).ToLower() == ".jpg" || Path.GetExtension(filePath).ToLower() == ".jpeg")
                {
                    var maker = directories.Where(s => s.Name != "File").Select(k => k.Tags.FirstOrDefault(t => t.Name == "Make")).FirstOrDefault(x => x != null);
                    if (maker != null)
                    {
                        dr[ColNameOfMake] = maker.Description;
                    }

                    var model = directories.Where(s => s.Name != "File").Select(k => k.Tags.FirstOrDefault(t => t.Name == "Model")).FirstOrDefault(x => x != null);
                    if (model != null)
                    {
                        dr[ColNameOfModel] = model.Description;
                    }

                    var picDate = directories.FirstOrDefault(s => s.Name == "Exif SubIFD").Tags.FirstOrDefault(t => t.Name == "Date/Time Original");
                    if (picDate != null)
                    {
                        string str = picDate.Description;
                        if (!string.IsNullOrEmpty(str))
                        {
                            dr[ColNameOfPicDate] = DateTime.Parse(str.Substring(0, 10).Replace(':', '-') + str.Substring(10));

                        }
                    }

                    var jingdu = directories.Where(s => s.Name != "File").Select(k => k.Tags.FirstOrDefault(t => t.Name == "GPS Latitude")).FirstOrDefault(x => x != null);
                    if (jingdu != null)
                    {
                        string str = jingdu.Description;
                        MatchCollection matchCollection = Regex.Matches(str, "\\d+");
                        dr[ColNameOfLatitude] = int.Parse(matchCollection[0].Value) + int.Parse(matchCollection[1].Value) / 60.0 + int.Parse(matchCollection[1].Value) / 60.0 / 60.0;
                    }

                    var weidu = directories.Where(s => s.Name != "File").Select(k => k.Tags.FirstOrDefault(t => t.Name == "GPS Longitude")).FirstOrDefault(x => x != null);
                    if (weidu != null)
                    {
                        string str = weidu.Description;
                        MatchCollection matchCollection = Regex.Matches(str, "\\d+");
                        dr[ColNameOfLongitude] = int.Parse(matchCollection[0].Value) + int.Parse(matchCollection[1].Value) / 60.0 + int.Parse(matchCollection[1].Value) / 60.0 / 60.0;

                    }

                    var haiba = directories.Where(s => s.Name != "File").Select(k => k.Tags.FirstOrDefault(t => t.Name == "GPS Altitude")).FirstOrDefault(x => x != null);
                    if (haiba != null)
                    {
                        string str = haiba.Description;
                        dr[ColNameOfAltitude] = Regex.Match(str, "\\d+").Value;
                    }

简单给大家看下Exif读取到的信息。

有价值的信息都存在这些tag里面

jpeg方面的信息

photoshop处理过的信息都保存在内,所以不想让Adobe告的话,最后一步最好把Exif给删除掉,哈哈。

photoshop处理过的信息都保存在内

摄影师想要的信息也都在里面

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-05-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Excel催化剂 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档