• <legend id='R7y5y'><style id='R7y5y'><dir id='R7y5y'><q id='R7y5y'></q></dir></style></legend>

        <small id='R7y5y'></small><noframes id='R7y5y'>

          <bdo id='R7y5y'></bdo><ul id='R7y5y'></ul>
      1. <i id='R7y5y'><tr id='R7y5y'><dt id='R7y5y'><q id='R7y5y'><span id='R7y5y'><b id='R7y5y'><form id='R7y5y'><ins id='R7y5y'></ins><ul id='R7y5y'></ul><sub id='R7y5y'></sub></form><legend id='R7y5y'></legend><bdo id='R7y5y'><pre id='R7y5y'><center id='R7y5y'></center></pre></bdo></b><th id='R7y5y'></th></span></q></dt></tr></i><div id='R7y5y'><tfoot id='R7y5y'></tfoot><dl id='R7y5y'><fieldset id='R7y5y'></fieldset></dl></div>
        <tfoot id='R7y5y'></tfoot>

        如何使用 PIL 调整大小并将旋转 EXIF 信息应用于文件?

        时间:2023-07-03

        <i id='pFi2s'><tr id='pFi2s'><dt id='pFi2s'><q id='pFi2s'><span id='pFi2s'><b id='pFi2s'><form id='pFi2s'><ins id='pFi2s'></ins><ul id='pFi2s'></ul><sub id='pFi2s'></sub></form><legend id='pFi2s'></legend><bdo id='pFi2s'><pre id='pFi2s'><center id='pFi2s'></center></pre></bdo></b><th id='pFi2s'></th></span></q></dt></tr></i><div id='pFi2s'><tfoot id='pFi2s'></tfoot><dl id='pFi2s'><fieldset id='pFi2s'></fieldset></dl></div>

            <small id='pFi2s'></small><noframes id='pFi2s'>

            • <bdo id='pFi2s'></bdo><ul id='pFi2s'></ul>
            • <legend id='pFi2s'><style id='pFi2s'><dir id='pFi2s'><q id='pFi2s'></q></dir></style></legend>
                1. <tfoot id='pFi2s'></tfoot>
                    <tbody id='pFi2s'></tbody>
                  本文介绍了如何使用 PIL 调整大小并将旋转 EXIF 信息应用于文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Python 来调整图片大小.用我的相机,文件都是横向写的.

                  I am trying to use Python to resize picture. With my camera, files are all written is landscape way.

                  exif 信息处理一个标签以要求图像查看器以某种方式旋转.由于大多数浏览器不理解此信息,我想使用此 EXIF 信息旋转图像并保留所有其他 EXIF 信息.

                  The exif information handle a tag to ask the image viewer to rotate in a way or another. Since most of the browser doesn't understand this information, I want to rotate the image using this EXIF information and keeping every other EXIF information.

                  你知道我如何使用 Python 做到这一点吗?

                  Do you know how I can do that using Python ?

                  阅读 EXIF.py 源代码,我发现了类似的东西:

                  Reading the EXIF.py source code, I found something like that :

                  0x0112: ('Orientation',
                           {1: 'Horizontal (normal)',
                            2: 'Mirrored horizontal',
                            3: 'Rotated 180',
                            4: 'Mirrored vertical',
                            5: 'Mirrored horizontal then rotated 90 CCW',
                            6: 'Rotated 90 CW',
                            7: 'Mirrored horizontal then rotated 90 CW',
                            8: 'Rotated 90 CCW'})
                  

                  我如何使用这些信息和 PIL 来应用它?

                  How can I use this information and PIL to apply it ?

                  推荐答案

                  终于用上了pyexiv2,但在 GNU 以外的其他平台上安装有点棘手.

                  I finally used pyexiv2, but it is a bit tricky to install on other platforms than GNU.

                  #!/usr/bin/python
                  # -*- coding: utf-8 -*-
                  # Copyright (C) 2008-2009 Rémy HUBSCHER <natim@users.sf.net> - http://www.trunat.fr/portfolio/python.html
                  
                  # This program is free software; you can redistribute it and/or modify
                  # it under the terms of the GNU General Public License as published by
                  # the Free Software Foundation; either version 2 of the License, or
                  # (at your option) any later version.
                  
                  # This program is distributed in the hope that it will be useful,
                  # but WITHOUT ANY WARRANTY; without even the implied warranty of
                  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
                  # GNU General Public License for more details.
                  
                  # You should have received a copy of the GNU General Public License along
                  # with this program; if not, write to the Free Software Foundation, Inc.,
                  # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
                  
                  # Using :
                  #   - Python Imaging Library PIL    http://www.pythonware.com/products/pil/index.htm
                  #   - pyexiv2                       http://tilloy.net/dev/pyexiv2/
                  
                  ###
                  # What is doing this script ?
                  #
                  #  1. Take a directory of picture from a Reflex Camera (Nikon D90 for example)
                  #  2. Use the EXIF Orientation information to turn the image
                  #  3. Remove the thumbnail from the EXIF Information
                  #  4. Create 2 image one maxi map in 600x600, one mini map in 200x200
                  #  5. Add a comment with the name of the Author and his Website
                  #  6. Copy the EXIF information to the maxi and mini image
                  #  7. Name the image files with a meanful name (Date of picture)
                  
                  import os, sys
                  try:
                      import Image
                  except:
                      print "To use this program, you need to install Python Imaging Library - http://www.pythonware.com/products/pil/"
                      sys.exit(1)
                  
                  try:
                      import pyexiv2
                  except:
                      print "To use this program, you need to install pyexiv2 - http://tilloy.net/dev/pyexiv2/"
                      sys.exit(1)
                  
                  ############# Configuration ##############
                  size_mini = 200, 200
                  size_maxi = 1024, 1024
                  
                  # Information about the Photograph should be in ASCII
                  COPYRIGHT="Remy Hubscher - http://www.trunat.fr/"
                  ARTIST="Remy Hubscher"
                  ##########################################
                  
                  def listJPEG(directory):
                      "Retourn a list of the JPEG files in the directory"
                      fileList = [os.path.normcase(f) for f in os.listdir(directory)]
                      fileList = [f for f in fileList if os.path.splitext(f)[1]  in ('.jpg', '.JPG')]
                      fileList.sort()
                      return fileList
                  
                  def _mkdir(newdir):
                      """
                      works the way a good mkdir should :)
                        - already exists, silently complete
                        - regular file in the way, raise an exception
                        - parent directory(ies) does not exist, make them as well
                      """
                      if os.path.isdir(newdir):
                          pass
                      elif os.path.isfile(newdir):
                          raise OSError("a file with the same name as the desired " 
                                        "dir, '%s', already exists." % newdir)
                      else:
                          head, tail = os.path.split(newdir)
                          if head and not os.path.isdir(head):
                              _mkdir(head)
                          if tail:
                              os.mkdir(newdir)
                  
                  if len(sys.argv) < 3:
                      print "USAGE : python %s indir outdir [comment]" % sys.argv[0]
                      exit
                  
                  indir  = sys.argv[1]
                  outdir = sys.argv[2]
                  
                  if len(sys.argv) == 4:
                      comment = sys.argv[1]
                  else:
                      comment = COPYRIGHT
                  
                  agrandie = os.path.join(outdir, 'agrandie')
                  miniature = os.path.join(outdir, 'miniature')
                  
                  print agrandie, miniature
                  
                  _mkdir(agrandie)
                  _mkdir(miniature)
                  
                  for infile in listJPEG(indir):
                      mini  = os.path.join(miniature, infile)
                      grand = os.path.join(agrandie, infile)
                      file_path = os.path.join(indir, infile)
                  
                      image = pyexiv2.Image(file_path)
                      image.readMetadata()
                  
                      # We clean the file and add some information
                      image.deleteThumbnail()
                  
                      image['Exif.Image.Artist'] = ARTIST
                      image['Exif.Image.Copyright'] = COPYRIGHT
                  
                      image.setComment(comment)
                  
                      # I prefer not to modify the input file
                      # image.writeMetadata()
                  
                      # We look for a meanful name
                      if 'Exif.Image.DateTime' in image.exifKeys():
                          filename = image['Exif.Image.DateTime'].strftime('%Y-%m-%d_%H-%M-%S.jpg')
                          mini  = os.path.join(miniature, filename)
                          grand = os.path.join(agrandie, filename)
                      else:
                          # If no exif information, leave the old name
                          mini  = os.path.join(miniature, infile)
                          grand = os.path.join(agrandie, infile)
                  
                      # We create the thumbnail
                      #try:
                      im = Image.open(file_path)
                      im.thumbnail(size_maxi, Image.ANTIALIAS)
                  
                      # We rotate regarding to the EXIF orientation information
                      if 'Exif.Image.Orientation' in image.exifKeys():
                          orientation = image['Exif.Image.Orientation']
                          if orientation == 1:
                              # Nothing
                              mirror = im.copy()
                          elif orientation == 2:
                              # Vertical Mirror
                              mirror = im.transpose(Image.FLIP_LEFT_RIGHT)
                          elif orientation == 3:
                              # Rotation 180°
                              mirror = im.transpose(Image.ROTATE_180)
                          elif orientation == 4:
                              # Horizontal Mirror
                              mirror = im.transpose(Image.FLIP_TOP_BOTTOM)
                          elif orientation == 5:
                              # Horizontal Mirror + Rotation 90° CCW
                              mirror = im.transpose(Image.FLIP_TOP_BOTTOM).transpose(Image.ROTATE_90)
                          elif orientation == 6:
                              # Rotation 270°
                              mirror = im.transpose(Image.ROTATE_270)
                          elif orientation == 7:
                              # Horizontal Mirror + Rotation 270°
                              mirror = im.transpose(Image.FLIP_TOP_BOTTOM).transpose(Image.ROTATE_270)
                          elif orientation == 8:
                              # Rotation 90°
                              mirror = im.transpose(Image.ROTATE_90)
                  
                          # No more Orientation information
                          image['Exif.Image.Orientation'] = 1
                      else:
                          # No EXIF information, the user has to do it
                          mirror = im.copy()
                  
                      mirror.save(grand, "JPEG", quality=85)
                      img_grand = pyexiv2.Image(grand)
                      img_grand.readMetadata()
                      image.copyMetadataTo(img_grand)
                      img_grand.writeMetadata()
                      print grand
                  
                      mirror.thumbnail(size_mini, Image.ANTIALIAS)
                      mirror.save(mini, "JPEG", quality=85)
                      img_mini = pyexiv2.Image(mini)
                      img_mini.readMetadata()
                      image.copyMetadataTo(img_mini)
                      img_mini.writeMetadata()
                      print mini
                  
                      print
                  

                  如果您发现有什么可以改进的(除了它仍然适用于 Python 2.5),请告诉我.

                  If you see something that could be improved (except the fact that it is still for Python 2.5) then please let me know.

                  这篇关于如何使用 PIL 调整大小并将旋转 EXIF 信息应用于文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在pygame中旋转时,船向上和向左移动比向下和向右移动更快 下一篇:每次启动应用程序时轮换日志文件(Python)

                  相关文章

                  <small id='2ZwUJ'></small><noframes id='2ZwUJ'>

                  1. <legend id='2ZwUJ'><style id='2ZwUJ'><dir id='2ZwUJ'><q id='2ZwUJ'></q></dir></style></legend>

                    • <bdo id='2ZwUJ'></bdo><ul id='2ZwUJ'></ul>
                  2. <tfoot id='2ZwUJ'></tfoot>

                    1. <i id='2ZwUJ'><tr id='2ZwUJ'><dt id='2ZwUJ'><q id='2ZwUJ'><span id='2ZwUJ'><b id='2ZwUJ'><form id='2ZwUJ'><ins id='2ZwUJ'></ins><ul id='2ZwUJ'></ul><sub id='2ZwUJ'></sub></form><legend id='2ZwUJ'></legend><bdo id='2ZwUJ'><pre id='2ZwUJ'><center id='2ZwUJ'></center></pre></bdo></b><th id='2ZwUJ'></th></span></q></dt></tr></i><div id='2ZwUJ'><tfoot id='2ZwUJ'></tfoot><dl id='2ZwUJ'><fieldset id='2ZwUJ'></fieldset></dl></div>