ผลที่ได้ เมื่อสั่ง print ไม่มีค่า base64 ออกมาค่ะ
จะได้ผลลัพธ์ตามภาพด้านล่างค่ะ
โค้ด: เลือกทั้งหมด
b''
<class 'bytes'>
b''
<class 'bytes'>
Moderator: mindphp, ผู้ดูแลกระดาน
โค้ด: เลือกทั้งหมด
b''
<class 'bytes'>
b''
<class 'bytes'>
โค้ด: เลือกทั้งหมด
with open("image.jpg", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
จากการนำโค้ดที่ได้มาใช้bolue เขียน: ↑11/08/2022 1:45 pm ลองทำวิธีเซฟเป็นไฟล์
แล้วทำการเปิดไฟล์รูปและให้แปลงไฟล์เป็น base64
โค้ด: เลือกทั้งหมด
with open("image.jpg", "rb") as image_file: encoded_string = base64.b64encode(image_file.read())
โค้ด: เลือกทั้งหมด
@app.post('/api_pdftopic')
async def read_pdf(request: Pdf_base64):
with open(r'C:\Users\fonfo\pdf-toolkits\lernpython.pdf', 'wb') as theFile:
theFile.write(base64.b64decode(request.pdf_base64))
process = subprocess.Popen(r"Magick convert C:\Users\fonfo\pdf-toolkits\lernpython.pdf C:\Users\fonfo\pdf-toolkits\lernpython.jpg", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
#ส่วนที่เพิ่มเข้ามา ในการแปลงไฟล์ภาพเป็น base64
with open("lernpython.jpg", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return (encoded_string)
โค้ด: เลือกทั้งหมด
@app.post('/api_pdftopic')
async def read_pdf(request: Pdf_base64):
with open(r'C:\Users\fonfo\pdf-toolkits\pythonPDF.pdf', 'wb') as theFile:
theFile.write(base64.b64decode(request.pdf_base64))
process = subprocess.Popen(r"Magick convert C:\Users\fonfo\pdf-toolkits\pythonPDF.pdf C:\Users\fonfo\pdf-toolkits\pythonPDF.jpg", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
#ในส่วนของการส่งค่ากลับ เป็น base64
with open("pythonPDF.jpg", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return (encoded_string)
โค้ด: เลือกทั้งหมด
json.dumps({'data': [{'base64': '0.0.0.0' , 'type':'jpg'}, {'base64': '0.0.0.0' , 'type':'jpg'}, {'base64': '0.0.0.0' , 'type':'jpg'},]})
โค้ด: เลือกทั้งหมด
@app.post('/api_pdftopic')
async def read_pdf(request: Pdf_base64):
with open(r'C:\Users\fonfo\pdf-toolkits\pythonPDF.pdf', 'wb') as theFile:
theFile.write(base64.b64decode(request.pdf_base64))
process = subprocess.Popen(r"Magick convert C:\Users\fonfo\pdf-toolkits\pythonPDF.pdf C:\Users\fonfo\pdf-toolkits\pythonPDF.jpg",
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
with open("pythonPDF.jpg", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
โค้ด: เลือกทั้งหมด
json.dumps({'data': [{'base64': '0.0.0.0' , 'type':'jpg'}, {'base64': '0.0.0.0' , 'type':'jpg'}, {'base64': '0.0.0.0' , 'type':'jpg'},]})
โค้ด: เลือกทั้งหมด
@app.post('/api_pdftopic')
async def read_pdf(request: Pdf_base64):
with open(r'C:\Users\fonfo\pdf-toolkits\pythonPDF.pdf', 'wb') as theFile:
theFile.write(base64.b64decode(request.pdf_base64))
process = subprocess.Popen(r"Magick convert C:\Users\fonfo\pdf-toolkits\pythonPDF.pdf C:\Users\fonfo\pdf-toolkits\pythonPDF.jpg",
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
return json.dumps({'data': [{'base64': '0.0.0.0' , 'type':'jpg'},
{'base64': '0.0.0.0' , 'type':'jpg'},
{'base64': '0.0.0.0' , 'type':'jpg'},]})
โค้ด: เลือกทั้งหมด
"{\"data\": [{\"base64\": \"0.0.0.0\", \"type\": \"jpg\"}, {\"base64\": \"0.0.0.0\", \"type\": \"jpg\"}, {\"base64\": \"0.0.0.0\", \"type\": \"jpg\"}]}"
โค้ด: เลือกทั้งหมด
[{\"base64\": \"0.0.0.0\", \"type\": \"jpg\"}, {\"base64\": \"0.0.0.0\", \"type\": \"jpg\"}, {\"base64\": \"0.0.0.0\", \"type\": \"jpg\"}]
สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 80