Python exam help: Difference between revisions

From ICO wiki
Jump to navigationJump to search
(Created page with "==To get GPS coordinates and image orientation:== <source lang="python"> from exifread import process_file def degrees(j): return j.values[0].num + (j.values[1].num + j...")
 
No edit summary
Line 178: Line 178:
   </body>
   </body>
</html>
</html>
</source>
==Final result all in one file==
<source lang="python">
# I need to mention my path like this - python dexif2.py /home/sheela/Documents/Python/pics /home/sheela/Documents/Python
import codecs, os,sys
from exifread import process_file
from PIL import Image
from jinja2 import Template
# Grab arguments specified on the command line after: python paranoia.py <input_dir> <output_dir>
input_directory, output_directory = sys.argv[1:]
# Create output directories if necessary
if not os.path.exists(output_directory):
    os.makedirs(os.path.join(output_directory, "thumbnails"))
HEADER = """<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <style>
        body { background-color: #444; }
        img.thumb { position: relative; display: inline; margin: 1em;
            padding: 0; width: 192; height: 192;
            box-shadow: 0px 0px 10px rgba(0,0,0,1); }
    </style>
  </head>
  <body>
"""
# Open index.html in output diretory and write it line by line
with open(os.path.join(output_directory, "index.html"), "w") as fh:
    fh.write(HEADER)
    for filename in os.listdir(input_directory):       
        # Read EXIF tags
        tags = process_file(open(os.path.join(input_directory, filename)))
        # Read image data
        original = Image.open(os.path.join(input_directory, filename))
        # Rotate as necessary
        rotated = original # Not rotated at all
        orientation = tags.get("Image Orientation") # Parse image orientation       
        if orientation:
            j, = orientation.values
            if j == 6:
                rotated = original.transpose(Image.ROTATE_270)               
            elif j == 8:
                rotated = original.transpose(Image.ROTATE_90)
            elif j == 3:
                rotated = original.transpose(Image.ROTATE_180)
        rotated.save(os.path.join(output_directory, filename))
        # Save thumbnail       
        rotated.thumbnail((192,192), Image.ANTIALIAS)
        rotated.save(os.path.join(output_directory, "thumbnails", filename))
        fh.write("""    <a href="%s">""" % filename)
        fh.write("""<img class="thumb" src="thumbnails/%s"/>""" % filename)
        fh.write("""</a>\n""")
    fh.write("  </body>\n")
    fh.write("</html>\n")
</source>
</source>

Revision as of 13:50, 26 May 2016

To get GPS coordinates and image orientation:

from exifread import process_file
 
def degrees(j):
    return j.values[0].num + (j.values[1].num + j.values[2].den * 1.0 / j.values[2].num) / 60.0
 
tags = process_file(open("/home/sheela/Documents/Python/pics/IMG_20131019_201706.jpg"))
print tags
lat, lng = tags.get("GPS GPSLatitude"), tags.get("GPS GPSLongitude")
if lat and lng:
    print "%.4f,%.4f" % (degrees(lat), degrees(lng)),
 
# Parse datetime of the photo
timestamp = tags.get("EXIF DateTimeOriginal")
if timestamp:
    print timestamp.values,
 
# Parse image orientation
orientation = tags.get("Image Orientation")
 
if orientation:
    j, = orientation.values
    if j == 6:
        print "rotated 270 degrees",
    elif j == 8:
        print "rotated 90 degrees",
    elif j == 3:
        print "rotated 180 degrees",
 
print

This code is to remove the exif data - metadata

import Image
from exifread import process_file

image_file = open('/home/sheela/Documents/Python/pics/IMG_20150619_120040.jpg')
image = Image.open(image_file)

# next 3 lines strip exif
data = list(image.getdata())
image_without_exif = Image.new(image.mode, image.size)
image_without_exif.putdata(data)

image_without_exif.save('without_exif_data.jpeg')

This code is to rotate the picture (which do not have metadata) to original view

import Image
orig = Image.open("original.jpg")
rotated = orig.transpose(Image.ROTATE_90) # This happens only in the RAM!
rotated.save("rotated.jpg")
import Image
from exifread import process_file

#input_path, output_path = sys.argv[1:]

image_file = open('/home/sheela/Documents/Python/pics/IMG_20131019_201706.jpg')
image = Image.open(image_file)

# next 3 lines strip exif
data = list(image.getdata())
image_without_exif = Image.new(image.mode, image.size)
image_without_exif.putdata(data)

image_without_exif.save('without_exif_data.jpeg')


im = Image.open("/home/sheela/Documents/Python/pics/IMG_20131019_201706.jpg")
rotated = im.transpose(Image.ROTATE_90)
im.save("rotated.jpg")

#orig = Image.open(output_path)
#rotated = orig.transpose(Image.ROTATE_90) # This happens only in the RAM!
#rotated.save(output_path)

dexif.py Lauri's code

import codecs, os, sys
from exifread import process_file
from PIL import Image
from jinja2 import Template

input_path, output_path = sys.argv[1:] 
if not os.path.exists(output_path):
    os.makedirs(os.path.join(output_path, "thumbnails"))
template = Template(codecs.open("gallery.html", "r", encoding="utf-8").read())
with codecs.open("/tmp/bulid/index.html", "w", encoding="utf-8") as fh:
     fh.write(template.render(files = os.listdir(input_path)))

for filename in os.listdir(input_path):
        if not filename.endswith(".jpg"): continue 
        print filename
        tags = EXIF.process_file(open(os.path.join(input_path, filename)))
        im = Image.open(os.path.join(input_path, filname))
        rotated = im # Not roated at all
        orientation = tags.get("Image orientation") # paese image orientation
        if orientation:
            j, = orientation.values
            if j == 6:
                rotated = im.transpose(Image.ROTATE_270)
            elif j == 8:
                rotated = im.transpose(Image.ROTATE_90)
            elif j == 3:
                rotated = im.transpose(Image.ROTATE_180)
        rotated.save(os.path.join(output_path, filename))
        rotated.thumbnail((192,192), Image.ANTIALIAS)
        rotated.save(os.path.join(output_path, "thumbnails", filename))

Artur code A.py

import Image
import sys
from exifread import process_file

input_path, output_path = sys.argv[1:]

image_file = open(input_path)
image = Image.open(image_file)

data = list(image.getdata())
no_exif = Image.new(image.mode, image.size)
no_exif.putdata(data)

no_exif.save(output_path)

orig = Image.open(output_path)
rotated = orig.transpose(Image.ROTATE_90) # This happens only in the RAM!
rotated.save(output_path)

To get image thumbnail

<<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>Images</title>
    <style>
        body {
            background-color: #444;
        }

        .thumbnail {
            position: relative;
            display: inline;
            margin: 1em;
            padding: 0;
            width: 192;
            height: 192;
            box-shadow: 0px 0px 10px rgba(0,0,0,1);
        }
    </style>
  </head>
  <body>
    {% for filename in files % 
        <a href= "{{filename}}">
            <img class="thumbnail" src="thumbnails/{{filename}}"/>  
       </a>
    {% endfor %}
  </body>
</html>

Final result all in one file

# I need to mention my path like this - python dexif2.py /home/sheela/Documents/Python/pics /home/sheela/Documents/Python 

import codecs, os,sys
from exifread import process_file
from PIL import Image
from jinja2 import Template
 
# Grab arguments specified on the command line after: python paranoia.py <input_dir> <output_dir>
input_directory, output_directory = sys.argv[1:]
 
# Create output directories if necessary
if not os.path.exists(output_directory):
    os.makedirs(os.path.join(output_directory, "thumbnails"))
 
HEADER = """<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <style>
        body { background-color: #444; }
        img.thumb { position: relative; display: inline; margin: 1em;
            padding: 0; width: 192; height: 192;
            box-shadow: 0px 0px 10px rgba(0,0,0,1); }
    </style>
  </head>
  <body>
"""

# Open index.html in output diretory and write it line by line
with open(os.path.join(output_directory, "index.html"), "w") as fh:
    fh.write(HEADER)
 
    for filename in os.listdir(input_directory):        
        # Read EXIF tags
        tags = process_file(open(os.path.join(input_directory, filename)))
 
        # Read image data
        original = Image.open(os.path.join(input_directory, filename))
 
        # Rotate as necessary
        rotated = original # Not rotated at all
        orientation = tags.get("Image Orientation") # Parse image orientation        
        if orientation:
            j, = orientation.values
            if j == 6:
                rotated = original.transpose(Image.ROTATE_270)                
            elif j == 8:
                rotated = original.transpose(Image.ROTATE_90)
            elif j == 3:
                rotated = original.transpose(Image.ROTATE_180)
        rotated.save(os.path.join(output_directory, filename))
 
        # Save thumbnail        
        rotated.thumbnail((192,192), Image.ANTIALIAS)
        rotated.save(os.path.join(output_directory, "thumbnails", filename))
        fh.write("""    <a href="%s">""" % filename)
        fh.write("""<img class="thumb" src="thumbnails/%s"/>""" % filename)
        fh.write("""</a>\n""")
    fh.write("  </body>\n")
    fh.write("</html>\n")