<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Resize Photos by 50% script erases key geotag metadata in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306204#M68101</link>
    <description>&lt;P&gt;Yikes! Sorry&lt;/P&gt;&lt;P&gt;Try just replacing line 25 of your second script example with image.save(destination_file, exif_data)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To go over what I had posted:&lt;/P&gt;&lt;P&gt;first line establishes the image object, the second line flips it so it's facing up correctly, then lines 7-9 save the image with the exif info from the original image (if there is any), or else saves without it if there isn't any.&lt;/P&gt;</description>
    <pubDate>Thu, 06 Jul 2023 15:25:29 GMT</pubDate>
    <dc:creator>AlfredBaldenweck</dc:creator>
    <dc:date>2023-07-06T15:25:29Z</dc:date>
    <item>
      <title>Resize Photos by 50% script erases key geotag metadata</title>
      <link>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306173#M68098</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I am working on a python script that will later be used as a tool in ArcGIS Pro that will take the photos from a drone, resize them by 50%, then run a geotag-to-point process. Right now, the script is set to copy all the photos into a new folder called "images_resized" as a way to indicate that these are the photos to be used and uploaded while the raw unchanged photos stay in the root folder. The issue I am running into is that when I run the resize part of the script, I end up losing the key location data I need for the script to run properly.&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os
import arcpy
from sys import argv
from datetime import date
from pathlib import Path
from PIL import Image
import shutil
source_folder_path = r"C:\Users\admin\Desktop\UASPhotos"
# Specify the destination folder path
destination_folder_path = r"{}\images_resized".format(source_folder_path)
# Create the destination folder if it doesn't exist
if not os.path.exists(destination_folder_path):
    os.makedirs(destination_folder_path)
# Get a list of all files in the source folder
file_list = os.listdir(source_folder_path)
# Loop through each file in the source folder
for file_name in file_list:
    # Create the source and destination paths
    source_path = os.path.join(source_folder_path, file_name)
    destination_path = os.path.join(destination_folder_path, file_name)

    # Check if the file is an image (you can add more file extensions if needed)
    if file_name.lower().endswith((".jpg", ".jpeg", ".png", ".bmp", ".gif")):
        # Copy the file to the destination folder
        shutil.copyfile(source_path, destination_path)

        # Open the image file from the destination folder
        with Image.open(destination_path) as image:
            # Calculate the new width and height
            new_width = int(image.width * 0.5)
            new_height = int(image.height * 0.5)

            # Resize the image
            resized_image = image.resize((new_width, new_height))&lt;/LI-CODE&gt;&lt;P&gt;I know it's during the resize because I tried just the copy and the photos still had the info, but as soon as I run the resizer the data is gone. Any ideas on how to preserve the essential metadata or any workaround?&lt;/P&gt;&lt;P&gt;Edit: I also found and tried out some EXIF scripts basically running the code below but still the same result(replace after line 16 in the script above with this)&lt;/P&gt;&lt;LI-CODE lang="python"&gt;   for file_name in files:
    # Get the full path of the source file
    source_file = os.path.join(source_folder, file_name)

    # Check if the file is an image
    if not os.path.isfile(source_file) or not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
        continue

    # Open the image using PIL
    image = Image.open(source_file)

    # Resize the image
    width, height = image.size
    new_width = int(width * resize_percentage)
    new_height = int(height * resize_percentage)
    resized_image = image.resize((new_width, new_height), Image.ANTIALIAS)

    # Preserve the geotag information
    exif_data = image.info.get("exif")
    resized_image.info["exif"] = exif_data


    # Save the resized image to the destination folder
    destination_file = os.path.join(destination_folder, file_name)
    resized_image.save(destination_file)

    # Close the image
    image.close()
    resized_image.close()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2023 14:36:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306173#M68098</guid>
      <dc:creator>AFackler_NAPSG</dc:creator>
      <dc:date>2023-07-06T14:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: Resize Photos by 50% script erases key geotag metadata</title>
      <link>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306178#M68099</link>
      <description>&lt;P&gt;I think it's because you're just closing the image, not saving it.&lt;/P&gt;&lt;P&gt;Basically, on line 25, add in the exif tags in the second parameter.&amp;nbsp;&lt;/P&gt;&lt;P&gt;(This is from code I muddled through two years ago, so it may take some more muddling on your end, but it does work for me just fine; I was able to plot each edited photo on a map no issue)&lt;/P&gt;&lt;LI-CODE lang="python"&gt;img = Image.open(output)
img = ImageOps.exif_transpose(img)

#exifdata= img.getexif() # Get general tags.

#Save the output, including the exif tags where possible.
try:
    img.save(output, exif= img.info['exif'])
except:
    img.save(output)&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 06 Jul 2023 14:46:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306178#M68099</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-07-06T14:46:46Z</dc:date>
    </item>
    <item>
      <title>Re: Resize Photos by 50% script erases key geotag metadata</title>
      <link>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306191#M68100</link>
      <description>&lt;P&gt;Would you be able to explain a bit more about which lines from your script to put where? I think I put it in wrong since my images came back corrupted (I think?)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AFackler_NAPSG_0-1688656373912.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/74916i1893F6DEA6D992FE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AFackler_NAPSG_0-1688656373912.png" alt="AFackler_NAPSG_0-1688656373912.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2023 15:14:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306191#M68100</guid>
      <dc:creator>AFackler_NAPSG</dc:creator>
      <dc:date>2023-07-06T15:14:59Z</dc:date>
    </item>
    <item>
      <title>Re: Resize Photos by 50% script erases key geotag metadata</title>
      <link>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306204#M68101</link>
      <description>&lt;P&gt;Yikes! Sorry&lt;/P&gt;&lt;P&gt;Try just replacing line 25 of your second script example with image.save(destination_file, exif_data)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To go over what I had posted:&lt;/P&gt;&lt;P&gt;first line establishes the image object, the second line flips it so it's facing up correctly, then lines 7-9 save the image with the exif info from the original image (if there is any), or else saves without it if there isn't any.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2023 15:25:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306204#M68101</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-07-06T15:25:29Z</dc:date>
    </item>
    <item>
      <title>Re: Resize Photos by 50% script erases key geotag metadata</title>
      <link>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306218#M68102</link>
      <description>&lt;P&gt;Alright had to retrofit a bit by adding "&lt;SPAN&gt;exif_data&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;SPAN&gt;exif_data", otherwise it would fail. However, the XY data still isn't being preserved. See script below (also modified to work outside of a function, so there may be a few small differences from the original above)&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for file_name in file_list:
    # Get the full path of the source file
    source_file = os.path.join(source_folder_path, file_name)
    # Check if the file is an image
    if not os.path.isfile(source_file) or not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
        continue
    # Open the image using PIL
    image = Image.open(source_file)
    # image = source_file.exif_transpose(image)
    # Resize the image
    width, height = image.size
    new_width = int(width * 0.5)
    new_height = int(height * 0.5)
    resized_image = image.resize((new_width, new_height))
    # Preserve the geotag information
    exif_data = image.info.get("exif")
    resized_image.info["exif"] = exif_data
    # Save the resized image to the destination folder
    destination_file = os.path.join(destination_folder_path, file_name)
    resized_image.save(destination_file,exif_data=exif_data)&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 06 Jul 2023 15:46:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306218#M68102</guid>
      <dc:creator>AFackler_NAPSG</dc:creator>
      <dc:date>2023-07-06T15:46:12Z</dc:date>
    </item>
    <item>
      <title>Re: Resize Photos by 50% script erases key geotag metadata</title>
      <link>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306258#M68103</link>
      <description>&lt;P&gt;I think the issue was on your line 20, you put "exif_data" as the parameter, instead of "exif". I changed that and it worked for me.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Full code I used below&lt;/P&gt;&lt;LI-CODE lang="c"&gt;from PIL import Image, ImageDraw, ImageFont, ImageOps
from PIL.ExifTags import TAGS, GPSTAGS
import os

file_list = [r"Example_Photo 1.jpg"]
source_folder_path = r"W:\Downloads\deletelater\ResizeTest"
destination_folder_path = r"W:\Downloads\deletelater\ResizeTest\quaratine"

for file_name in file_list:
    # Get the full path of the source file
    source_file = os.path.join(source_folder_path, file_name)
    # Check if the file is an image
    if not os.path.isfile(source_file) or not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
        continue
    # Open the image using PIL
    image = Image.open(source_file)
    # Resize the image
    width, height = image.size
    new_width = int(width * 0.5)
    new_height = int(height * 0.5)
    resized_image = image.resize((new_width, new_height))
    # Preserve the geotag information
    exif_data = image.info["exif"]
    # Save the resized image to the destination folder
    file_nameR = file_name.split(".")[0] +"_Resized." + file_name.split(".")[1]
    destination_file = os.path.join(destination_folder_path, file_nameR)
    resized_image.save(destination_file,exif=exif_data)

print("Done")&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 06 Jul 2023 16:44:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306258#M68103</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-07-06T16:44:34Z</dc:date>
    </item>
    <item>
      <title>Re: Resize Photos by 50% script erases key geotag metadata</title>
      <link>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306265#M68104</link>
      <description>&lt;P&gt;Still no bueno, images coming back corrupted or not functional like my reply a few hours back. And XY data still isn't coming over when I look at the properties&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2023 16:56:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/resize-photos-by-50-script-erases-key-geotag/m-p/1306265#M68104</guid>
      <dc:creator>AFackler_NAPSG</dc:creator>
      <dc:date>2023-07-06T16:56:38Z</dc:date>
    </item>
  </channel>
</rss>

