關於python的一些應用疑問

105 views
Skip to first unread message

球先生

unread,
Nov 17, 2017, 12:54:56 AM11/17/17
to python.tw
以下是小弟寫的程式,大部分參照網路上的寫法,想透過flask創一個web,但顯示如下,<數字>這些我不知道該放入哪些資訊才能將圖片裡的資料放入裡面,跪求大大幫忙!!!!!

import os,sys                                                          
from flask import Flask, request, redirect, url_for,send_from_directory 
from werkzeug.utils import secure_filename
from werkzeug import SharedDataMiddleware


UPLOAD_FOLDER = 'C:/Users/Administrator/Desktop/照片/lena'             
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])    

app = Flask(__name__)                                                    
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER                           

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file',
                                    filename=filename))
        
    return '''
    <!doctype html>
    <title>上傳照片並顯示</title>
    <body>
    <h1>選擇檔案</h1>
    <p>顯示照片位置</p>
    <form method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=輸出>
    </form>
    </body>
    </html>
    '''
@app.route('/uploads/img/<filename>') 
def uploaded_file(filename):
    html = '''
<!DOCTYPE html>
<html>
<body>

<img id="imgObject" src="%s" alt="Smiley face" width="100" height="100"><BR>
<button onclick="setImgSize('imgObject',500, 500)">放大</button><button onclick="setImgSize('imgObject', 300, 300)">復原</button><button onclick="setImgSize('imgObject', 100, 100)">縮小</button><br>               
<style>
table, th, td {
    border: 1px solid black;
}
</style>
</head>
<body>

<table>
  <colgroup>                                      
    <col span="1" style="background-color:red">
    <col style="background-color:yellow">
  </colgroup>
  <tr>
    <th>檔名</th>   
    <th>經度</th>
    <th>緯度</th>
    <th>影像大小</th>
    <th>曝光時間</th>
    <th>拍攝時間</th>
    <th>像素</th>
    <th>格式</th>
  </tr>
  <tr>                                                                 
    <td><數字></td>                
    <td><數字></td>
    <td><數字></td>
    <td><數字></td>
    <td><數字></td>
    <td><數字></td>
    <td><數字></td>
    <td><數字></td>
  </tr>
</table>

<p >以上為圖片exif資訊</p>
<script>
// 設定影像大小
function setImgSize(id, imgWidth, imgHeight){
    document.getElementById(id).width = imgWidth;
    document.getElementById(id).height = imgHeight;
}
</script>
</body>
</html>

    ''' % ("/uploads/" + filename)
    return html

app.add_url_rule('/uploads/<filename>', 'uploaded_file',
                 build_only=True)
 
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
    '/uploads':  app.config['UPLOAD_FOLDER']
})


from PIL import Image                                  #顯示出經緯度
from PIL.ExifTags import TAGS, GPSTAGS

def get_exif_data(image):
    """Returns a dictionary from the exif data of an PIL Image item. Also converts the GPS Tags"""
    exif_data = {}
    info = image._getexif()
    if info:
        for tag, value in info.items():
            decoded = TAGS.get(tag, tag)
            if decoded == "GPSInfo":
                gps_data = {}
                for t in value:
                    sub_decoded = GPSTAGS.get(t, t)
                    gps_data[sub_decoded] = value[t]

                exif_data[decoded] = gps_data
            else:
                exif_data[decoded] = value

    return exif_data

def _get_if_exist(data, key):
    if key in data:
        return data[key]

    return None

def _convert_to_degress(value):
    """Helper function to convert the GPS coordinates stored in the EXIF to degress in float format"""
    d0 = value[0][0]
    d1 = value[0][1]
    d = float(d0) / float(d1)

    m0 = value[1][0]
    m1 = value[1][1]
    m = float(m0) / float(m1)

    s0 = value[2][0]
    s1 = value[2][1]
    s = float(s0) / float(s1)

    return d + (m / 60.0) + (s / 3600.0)

def get_lat_lon(exif_data):
    """Returns the latitude and longitude, if available, from the provided exif_data (obtained through get_exif_data above)"""
    lat = None
    lon = None

    if "GPSInfo" in exif_data:
        gps_info = exif_data["GPSInfo"]

        gps_latitude = _get_if_exist(gps_info, "GPSLatitude")
        gps_latitude_ref = _get_if_exist(gps_info, 'GPSLatitudeRef')
        gps_longitude = _get_if_exist(gps_info, 'GPSLongitude')
        gps_longitude_ref = _get_if_exist(gps_info, 'GPSLongitudeRef')

        if gps_latitude and gps_latitude_ref and gps_longitude and gps_longitude_ref:
            lat = _convert_to_degress(gps_latitude)
            if gps_latitude_ref != "N":                     
                lat = 0 - lat

            lon = _convert_to_degress(gps_longitude)
            if gps_longitude_ref != "E":
                lon = 0 - lon
    return lat, lon

if __name__ == "__main__":
    image = Image.open('C:/Users/Administrator/Desktop/照片/lena/DJI_0239.JPG')
    exif_data = get_exif_data(image)
    print (get_lat_lon(exif_data))
    
if __name__ == "__main__":                                                      
  app.run(port=80)
Reply all
Reply to author
Forward
0 new messages