ต้องการส่งชื่อรูปไปยังอีก url หนึ่ง และนำภาพจาก url มาแสดง

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

bom_002
PHP Super Member
PHP Super Member
โพสต์: 443
ลงทะเบียนเมื่อ: 06/03/2017 10:51 am

ต้องการส่งชื่อรูปไปยังอีก url หนึ่ง และนำภาพจาก url มาแสดง

โพสต์ที่ยังไม่ได้อ่าน โดย bom_002 »

เขียนโปรแกรมอัพโหลดไฟล์ pdf แล้วแปลง เป็น png
ให้ส่งค่ารูปภาพไปแสดงผลอีก url แล้วโชว์รูปภาพ ติด error จากโค้ดครับ
Selection_008.png
หน้า upload.py

โค้ด: เลือกทั้งหมด

#!/usr/bin/env python
#-*-coding:utf-8 -*-
"""
    Simple Upload Application
    ~~~~~~~~~~~~~~~~~~~~~~~~~
    All uploaded files are directly send back to the client.

    :copyright: (c) 2009 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""
from werkzeug.serving import run_simple
from werkzeug.wrappers import BaseRequest, BaseResponse , Response
from werkzeug.wsgi import wrap_file
import os
from wand.image import Image, Color
from flask import Flask, request, redirect, url_for
import base64


UPLOAD_FOLDER = '/openerp7/custom/addons/learning_test/file/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

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

def view_file(req):
    if not 'uploaded_file' in req.files:
        return BaseResponse('no file uploaded')

    f = req.files['uploaded_file']
    # print vars(file)
    # print f.filename
    f.save(os.path.join(app.config['UPLOAD_FOLDER'], f.filename))

#แปลงPDF to PNG
    def convert_pdf(filename, output_path, resolution=150):
        """ Convert a PDF into images.

            All the pages will give a single png file with format:
            {pdf_filename}-{page_number}.png

            The function removes the alpha channel from the image and
            replace it with a white background.
        """
        all_pages = Image(filename=filename, resolution=resolution)
        # print all_pages
        for i, page in enumerate(all_pages.sequence):
            with Image(page) as img:
                img.format = 'png'
                img.background_color = Color('white')
                img.alpha_channel = 'remove'

                image_filename = os.path.splitext(os.path.basename(filename))[0]
                image_filename = '{}{}.png'.format(image_filename, i + 1)
                image_filename = os.path.join(output_path, image_filename)
                img.save(filename=image_filename)

                app.route('127.0.0.1:5002/img/', methods=['GET', 'POST'])

                print image_filename
    convert_pdf("file/"+f.filename, "image/")

    # fee = os.listdir("image")
    image_aa = ''
    #header ='''<!DOCTYPE html><html><body><title>test_server</title><table border="1" style="border-collapse:collapse;" align="center">'''
    #fotor =''' </table><body><html>'''
    # print fee
    # i = 0
    # for fill in fee:
    #     image = open('/openerp7/custom/addons/learning_test/image/'+fill, 'rb') #openbinary file in read mode
    #     image_read = image.read()
    #     image_64_encode = base64.encodestring(image_read)
    add = Flask(__name__)
#        print fill    <img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
    image_img = add.route('http://127.0.0.1:5002')
        #image_aa += '''<tr><td>
        #<img src = "data:image/png;base64,''' + image_img + '''" width = "300" height = "500" >
        #</tr></td>'''

    #return BaseResponse(header+image_aa+fotor, mimetype='text/html')
    return BaseResponse(image_img, mimetype='text/html')

def upload_file(req):  #returnหน้า form เป็น code html ออกไป
    return BaseResponse('''
    <h1>Upload File</h1>
    <form action="" method="post" enctype="multipart/form-data">
        <input type="file" name="uploaded_file">
        <input type="submit" value="Upload">
    </form>
    ''', mimetype='text/html')

def application(environ, start_response):
    req = BaseRequest(environ)
    if req.method == 'POST':
        resp = view_file(req)
    else:
        resp = upload_file(req)
    return resp(environ, start_response)

if __name__ == '__main__':
    run_simple('localhost',5004, application, use_debugger=True)
หน้า view_img.py

โค้ด: เลือกทั้งหมด

#!/usr/bin/env python
#-*-coding:utf-8 -*-

from werkzeug.serving import run_simple
from werkzeug.wrappers import BaseRequest, BaseResponse , Response
import os
from flask import Flask, request, redirect, url_for
import base64

app = Flask(__name__)

def view_file(req):
    if not 'uploaded_file' in req.files:
        return BaseResponse('no file uploaded')
    
    app.route('/img/',methods = ['POST', 'GET'])
    def img(imgs):
        fee = os.listdir(imgs)
        image_aa = ''
        header ='''<!DOCTYPE html><html><body><title>test_server</title><table border="1" style="border-collapse:collapse;" align="center">'''
        fotor =''' </table><body><html>'''

        i = 0
        for fill in fee:
            image = open('/openerp7/custom/addons/learning_test/'+fill, 'rb') #openbinary file in read mode
            image_read = image.read()
            image_64_encode = base64.encodestring(image_read)
            image_aa += '''<tr><td>
            <img src = "data:image/png;base64,''' + image_64_encode + '''" width = "300" height = "500" >
            </tr></td>'''

        return BaseResponse(header+image_aa+fotor, mimetype='text/html')

def upload_file(req):  #returnหน้า form เป็น code html ออกไป
    return BaseResponse('''
    <h1>Upload File</h1>
    <form action="" method="post" enctype="multipart/form-data">
        <input type="file" name="uploaded_file">
        <input type="submit" value="Upload">
    </form>
    ''', mimetype='text/html')

def application(environ, start_response):
    req = BaseRequest(environ)
    if req.method == 'POST':
        resp = view_file(req)
    else:
        resp = upload_file(req)
    return resp(environ, start_response)

if __name__ == '__main__':
    run_simple('localhost',5002, application, use_debugger=True)
error code

โค้ด: เลือกทั้งหมด

/usr/bin/python2.7 /openerp7/custom/addons/learning_test/upload_img.py
 * Running on http://localhost:5004/ (Press CTRL+C to quit)
127.0.0.1 - - [28/Mar/2017 14:42:20] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [28/Mar/2017 14:42:20] "GET /favicon.ico HTTP/1.1" 200 -
image/python_1.png
image/python_2.png
image/python_3.png
image/python_4.png
Debugging middleware caught exception in streamed response at a point where response headers were already sent.
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 693, in __next__
    return self._next()
  File "/usr/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 81, in _iter_encoded
    for item in iterable:
TypeError: 'function' object is not iterable
127.0.0.1 - - [28/Mar/2017 14:42:46] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [28/Mar/2017 14:42:46] "GET /favicon.ico HTTP/1.1" 200 -
Process finished with exit code 0
ผลการรันหน้าเว็บ
ว่างเปล่า
แก้ไขล่าสุดโดย bom_002 เมื่อ 28/03/2017 3:21 pm, แก้ไขไปแล้ว 1 ครั้ง.
:plusone:
ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

Re: ต้องการส่งชื่อรูปไปยังอีก url หนึ่ง และนำภาพจาก url มาแสดง

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

ให้ส่งค่ารูปภาพไปแสดงผลอีก url แล้วโชว์รูปภาพ ติด error จากโค้ดครับ
งงคำถามจังเลย
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ต้องการส่งชื่อรูปไปยังอีก url หนึ่ง และนำภาพจาก url มาแสดง

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ตาม Error มีการส่ง response ผิด
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
bom_002
PHP Super Member
PHP Super Member
โพสต์: 443
ลงทะเบียนเมื่อ: 06/03/2017 10:51 am

Re: ต้องการส่งชื่อรูปไปยังอีก url หนึ่ง และนำภาพจาก url มาแสดง

โพสต์ที่ยังไม่ได้อ่าน โดย bom_002 »

thatsawan เขียน:
ให้ส่งค่ารูปภาพไปแสดงผลอีก url แล้วโชว์รูปภาพ ติด error จากโค้ดครับ
งงคำถามจังเลย
ส่งชื่อรูปไปอีก url ครับ แล้ว url นั้นทำการดึงรูปจากคอมพิวเตอร์ของเราโดยอ้างอิงจากชื่อภาพครับ
:plusone:
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 93