โดย bom_002 » 18/03/2017 11:52 am
การอัพโหลดรูปภาพ ใน
Python จะแตกต่างจาก
HTML ถ้า HTML สามารถอัพโหลดรูปภาพได้โดยตรง แต่ Python ไม่สามารถอัพโหลดรูปภาพขึ้นแสดงบนหน้าเว็บ Server ได้โดยตรง เราต้องทำการแปลงไฟล์รูปภาพให้อยู่ในรูปแบบ
BASE64 ก่อน ถึงจะสามารถอัพโหลดรูปภาพไปแสดงได้
[
u]โค้ดตัวอย่างการโชว์รูปภาพ[/u]
โค้ด: เลือกทั้งหมด
import base64
fee = os.listdir("images")
image_aa = ''
print fee
for fill in fee:
image = open('/home/com003/PycharmProjects/test_project/work01/images/'+fill, 'rb') open binary file in read mode
image_read = image.read()
image_64_encode = base64.encodestring(image_read)
image_aa += '''<img src="data:image/png;base64,''' + image_64_encode + '''" wicth="300" hight="200">'''
return BaseResponse(image_aa, mimetype='text/html')
การอัพโหลดรูปภาพ ใน [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2417-python-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html?bb_limitstart=2740]Python[/url] จะแตกต่างจาก [url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99-html5.html]HTML[/url] ถ้า HTML สามารถอัพโหลดรูปภาพได้โดยตรง แต่ Python ไม่สามารถอัพโหลดรูปภาพขึ้นแสดงบนหน้าเว็บ Server ได้โดยตรง เราต้องทำการแปลงไฟล์รูปภาพให้อยู่ในรูปแบบ [url=https://www.mindphp.com/forums/viewtopic.php?f=27&t=38855]BASE64[/url] ก่อน ถึงจะสามารถอัพโหลดรูปภาพไปแสดงได้
[[b]u]โค้ดตัวอย่างการโชว์รูปภาพ[/u][/b]
[code]
import base64
fee = os.listdir("images")
image_aa = ''
print fee
for fill in fee:
image = open('/home/com003/PycharmProjects/test_project/work01/images/'+fill, 'rb') open binary file in read mode
image_read = image.read()
image_64_encode = base64.encodestring(image_read)
image_aa += '''<img src="data:image/png;base64,''' + image_64_encode + '''" wicth="300" hight="200">'''
return BaseResponse(image_aa, mimetype='text/html')
[/code]