title: How to keep JPEG metadata using PIL?
body: <p>I am using the library PIL to resize an image to automate the inputs for other software in my job and this software needs this metadata.</p>
  <p>The image is correctly resized, but loses all metadata contained in it.</p>
  <p>I used the code below to resize.</p>
  <pre><code>from PIL import Image
  filename = r'input.jpg'
  ratio=0.2
  im = Image.open(filename)
  out = im.resize([int(ratio * s) for s in im.size], Image.ANTIALIAS)
  out.save(&quot;out.jpg&quot;, format=im.format, optimize=True)
  </code></pre>
  <p>I can see all metadata into the dict of image using:</p>
  <pre><code>im.__dict__
  </code></pre>
  <p>Libraries like pyexif, pyexif2, etc return Null values. I check the tag exif into im.<strong>dict</strong> and this tag is empty.</p>
  <p>Is there any method of adding this dictionary to my resized image?</p>
  <p>link to image download: <a href="https://1drv.ms/u/s!Av58o7S5NWNXjP4QCPuyZ2PzJGlonQ?e=tgRohl" rel="nofollow noreferrer">Image</a></p>
  <p>Thank you very much.</p>
  <p>EDIT: Results for im.<strong>dict</strong> (Inside this dictionary has a XML code. It is responsible for transfer metadata for the software that my company uses). We can see The xml script in 3 tags info--&gt;comment, app--&gt;com and applist<a href="https://1drv.ms/u/s!Av58o7S5NWNXjP4QCPuyZ2PzJGlonQ?e=tgRohl" rel="nofollow noreferrer">1</a>.</p>
  <pre><code>{'im': None,
  'mode': 'RGB',
  '_size': (4096, 2304),
  'palette': None,
  'info': {'jfif': 258,
       'jfif_version': (1, 2),
           'jfif_unit': 0, 'jfif_density': (1, 1),
           'comment': b'&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;image time=&quot;09:04:10.271222&quot; date=&quot;2020.06.15&quot; acq_index=&quot;7666&quot;&gt;\n\t&lt;Position time=&quot;20200615T090410.834&quot; received=&quot;2020-Jun-15 09:04:10.267082&quot; extrapolated=&quot;false&quot; age=&quot;4&quot; transponder_id=&quot;0&quot;&gt;\n\t\t&lt;Coords long=&quot;-40.9273182&quot; lat=&quot;-22.7837963&quot;/&gt;\n\t\t&lt;Depth altitude=&quot;5.14&quot; depth=&quot;85.57&quot;/&gt;\n\t\t&lt;Direction pitch=&quot;-3.09&quot; roll=&quot;0.03&quot; yaw=&quot;180.02&quot;/&gt;\n\t&lt;/Position&gt;\n\t&lt;acquisition&gt;\n\t\t&lt;exposure&gt;5000&lt;/exposure&gt;\n\t\t&lt;digital_gain&gt;1.19&lt;/digital_gain&gt;\n\t\t&lt;analog_gain&gt;6&lt;/analog_gain&gt;\n\t\t&lt;sensor_gain&gt;4&lt;/sensor_gain&gt;\n\t\t&lt;aperture&gt;1.4&lt;/aperture&gt;\n\t\t&lt;focus&gt;498&lt;/focus&gt;\n\t\t&lt;name&gt;ColorCamera&lt;/name&gt;\n\t\t&lt;camera_session_name&gt;start_1&lt;/camera_session_name&gt;\n\t\t&lt;camera_sub_session_name/&gt;\n\t\t&lt;focus_enc&gt;3945&lt;/focus_enc&gt;\n\t\t&lt;width&gt;4096&lt;/width&gt;\n\t\t&lt;height&gt;2304&lt;/height&gt;\n\t\t&lt;seq_slot&gt;0&lt;/seq_slot&gt;\n\t\t&lt;dequeue_time&gt;2020-06-15T09:04:10.887848&lt;/dequeue_time&gt;\n\t&lt;/acquisition&gt;\n\t&lt;errors/&gt;\n\t&lt;versions&gt;\n\t\t&lt;software&gt;0.968s4&lt;/software&gt;\n\t\t&lt;fpga&gt;0x02d1&lt;/f                    pga&gt;\n\t\t&lt;pic&gt;210&lt;/pic&gt;\n\t\t&lt;serial_number&gt;191&lt;/serial_number&gt;\n\t&lt;/versions&gt;\n\t&lt;ntp&gt;\n\t\t&lt;ntpq&gt;*192.168.99.100                   1 u   69  128  377    0.196   -3.329   0.521&lt;/ntpq&gt;\n\t\t&lt;state&gt;within_limits&lt;/state&gt;\n\t\t&lt;sync_level&gt;excellent_sync&lt;/sync_level&gt;\n\t&lt;/ntp&gt;\n\t&lt;pps/&gt;\n&lt;/image&gt;\n'}, 'category': 0, 'readonly': 1, 'pyaccess': None, '_exif': None, '_min_frame': 0, 'custom_mimetype': None, 'tile': [('jpeg', (0, 0, 4096, 2304), 0, ('RGB', ''))], 
  'decoderconfig': (),
  'decodermaxblock': 65536,
  'fp': &lt;_io.BufferedReader name='input.jpg'&gt;,
  'filename': 'input.jpg',
  '_exclusive_fp': True, 'bits': 8,
  'layers': 3,
  'layer': [(1, 2, 2, 0), (2, 1, 1, 1), (3, 1, 1, 1)],
  'huffman_dc': {},
  'huffman_ac': {},
  'quantization': {0: array('B', [3, 2, 2, 3, 2, 2, 3, 3, 3, 3, 4, 3, 3, 4, 5, 8, 5, 5, 4, 4, 5, 10, 7, 7, 6, 8, 12, 10, 12, 12, 11, 10, 11, 11, 13, 14, 18, 16, 13, 14, 17, 14, 11, 11, 16, 22, 16, 17, 19, 20, 21, 21, 21, 12, 15, 23, 24, 22, 20, 24, 18, 20, 21, 20]), 
                   1: array('B', [3, 4, 4, 5                    , 4, 5, 9, 5, 5, 9, 20, 13, 11, 13, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20])}, 
  
  'app': {'APP0': b'JFIF\x00\x01\x02\x00\x00\x01\x00\x01\x00\x00', 
          'COM': b'&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;image time=&quot;09:04:10.271222&quot; date=&quot;2020.06.15&quot; acq_index=&quot;7666&quot;&gt;\n\t&lt;Position time=&quot;20200615T090410.834&quot; received=&quot;2020-Jun-15 09:04:10.267082&quot; extrapolated=&quot;false&quot; age=&quot;4&quot; transponder_id=&quot;0&quot;&gt;\n\t\t&lt;Coords long=&quot;-40.9273182&quot; lat=&quot;-22.7837963&quot;/&gt;\n\t\t&lt;Depth altitude=&quot;5.14&quot; depth=&quot;85.57&quot;/&gt;\n\t\t&lt;Direction pitch=&quot;-3.09&quot; roll=&quot;0.03&quot; yaw=&quot;180.02&quot;/&gt;\n\t&lt;/Position&gt;\n\t&lt;acquisition&gt;\n\t\t&lt;exposure&gt;5000&lt;/exposure&gt;\n\t\t&lt;digital_gain&gt;1.19&lt;/digital_gain&gt;\n\t\t&lt;analog_gain&gt;6&lt;/analog_gain&gt;\n\t\t&lt;sensor_gain&gt;4&lt;/sensor_gain&gt;\n\t\t&lt;aperture&gt;1.4&lt;/aperture&gt;\n\t\t&lt;focus&gt;498&lt;/focus&gt;\n\t\t&lt;name&gt;ColorCamera&lt;/name&gt;\n\t\t&lt;camera_session_name&gt;start_1&lt;/camera_session_name&gt;\n\t\t&lt;camera_sub_session_name/&gt;\n\t\t&lt;focus_enc&gt;3945&lt;/focus_enc&gt;\n\t\t&lt;width&gt;4096&lt;/width&gt;\n\t\t&lt;height&gt;2304&lt;/height&gt;\n\t\t&lt;seq_slot&gt;0&lt;/seq_slot&gt;\n\t\t&lt;dequeue_time&gt;2020-06-15T09:04:10.887848&lt;/dequeue_time&gt;\n\t&lt;/acquisition&gt;\n\t&lt;errors/&gt;\n\t&lt;versions&gt;\n\t\t&lt;software&gt;0.968s4&lt;/software&gt;\n\t\t&lt;fpga&gt;0x02d1&lt;/fpga&gt;\               n\t\t&lt;pic&gt;210&lt;/pic&gt;\n\t\t&lt;serial_number&gt;191&lt;/serial_number&gt;\n\t&lt;/versions&gt;\n\t&lt;ntp&gt;\n\t\t&lt;ntpq&gt;*192.168.99.100                   1 u   69  128  377    0.196   -3.329   0.521&lt;/ntpq&gt;\n\t\t&lt;state&gt;within_limits&lt;/state&gt;\n\t\t&lt;sync_level&gt;excellent_sync&lt;/sync_level&gt;\n\t&lt;/ntp&gt;\n\t&lt;pps/&gt;\n&lt;/image&gt;\n'},
  
  'applist': [('APP0', b'JFIF\x00\x01\x02\x00\x00\x01\x00\x01\x00\x00'), ('COM', b'&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;image time=&quot;09:04:10.271222&quot; date=&quot;2020.06.15&quot; acq_index=&quot;7666&quot;&gt;\n\t&lt;Position time=&quot;20200615T090410.834&quot; received=&quot;2020-Jun-15 09:04:10.267082&quot; extrapolated=&quot;false&quot; age=&quot;4&quot; transponder_id=&quot;0&quot;&gt;\n\t\t&lt;Coords long=&quot;-40.9273182&quot; lat=&quot;-22.7837963&quot;/&gt;\n\t\t&lt;Depth altitude=&quot;5.14&quot; depth=&quot;85.57&quot;/&gt;\n\t\t&lt;Direction pitch=&quot;-3.09&quot; roll=&quot;0.03&quot; yaw=&quot;180.02&quot;/&gt;\n\t&lt;/Position&gt;\n\t&lt;acquisition&gt;\n\t\t&lt;exposure&gt;5000&lt;/exposure&gt;\n\t\t&lt;digital_gain&gt;1.19&lt;/digital_gain&gt;\n\t\t&lt;analog_gain&gt;6&lt;/analog_gain&gt;\n\t\t&lt;sensor_gain&gt;4&lt;/sensor_gain&gt;\n\t\t&lt;aperture&gt;1.4&lt;/aperture&gt;\n\t\t&lt;focus&gt;498&lt;/focus&gt;\n\t\t&lt;name&gt;ColorCamera&lt;/name&gt;\n\t\t&lt;camera_session_name&gt;start_1&lt;/camera_session_name&gt;\n\t\t&lt;camera_sub_session_name/&gt;\n\t\t&lt;focus_enc&gt;3945&lt;/focus_enc&gt;\n\t\t&lt;width&gt;4096&lt;/width&gt;\n\t\t&lt;height&gt;2304&lt;/height&gt;\n\t\t&lt;seq_slot&gt;0&lt;/seq_slot&gt;\n\t\t&lt;dequeue_time&gt;2020-06-15T09:04:10.887848&lt;/dequeue_time&gt;\n\t&lt;/acquisition&gt;\n\t&lt;errors/&gt;\n\t&lt;versions&gt;\n\t\t&lt;software&gt;0.968s4&lt;/software&gt;\n\t\t&lt;fpga&gt;0x02d1&lt;/fpga&gt;\n\t\t&lt;pic&gt;210&lt;/pic&gt;\n\t\t&lt;serial_number&gt;191&lt;/serial_number&gt;\n\t&lt;/versions&gt;\n\t&lt;ntp&gt;\n\t\t&lt;ntpq&gt;*192.168.99.100                   1 u   69  128  377    0.196   -3.329   0.521&lt;/ntpq&gt;\n\t\t&lt;state&gt;within_limits&lt;/state&gt;\n\t\t&lt;sync_level&gt;excellent_sync&lt;/sync_level&gt;\n\t&lt;/ntp&gt;\n\t&lt;pps/&gt;\n&lt;/image&gt;\n')],
  
  'icclist': []}
  </code></pre>