Skip to content

Instantly share code, notes, and snippets.

@bgyarfas
Created September 15, 2023 14:44
Show Gist options
  • Save bgyarfas/70f25a29adc79ed5359f12d149a20968 to your computer and use it in GitHub Desktop.
Save bgyarfas/70f25a29adc79ed5359f12d149a20968 to your computer and use it in GitHub Desktop.
Fix DJI JPEGs for instant-ngp
import sys
import itertools
from pathlib import Path
from PIL import Image
def fix_dji_jpegs(path):
""" Load and re-save with Pillow.
This removes the JPEG Marker 0 that STB doesn't like. """
# Directory containing the JPEG images
p = Path(path)
try:
for filename in itertools.chain(p.glob("*.JPG"), p.glob("*.jpg"), p.glob("*.JPEG"), p.glob("*.jpeg")):
with Image.open(filename) as img:
# You can perform any image processing operations here if needed.
# For example, resizing:
# img = img.resize((new_width, new_height))
# Save the image as JPEG, overwriting the original file
img.save(filename, "JPEG")
print(f"Processed and overwritten: {filename}")
except FileNotFoundError:
print(f"Input directory '{path}' not found.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python fix_dji_jpegs.py <path>")
sys.exit(1)
fix_dji_jpegs(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment