flask中以字节流方式返回图片

2024-02-22 16:47:00 by wst

图像处理

为了减少IO,通常处理完图片直接返回给前端,而不是保存磁盘。

怎么以字节流返回呢,这里给出完整的实例。

from flask import Flask, make_response
from io import BytesIO
from PIL import Image

app = Flask(__name__)

@app.route("/")
def helloworld():
    return "<p>Hello, World!</p>"

@app.route("/image")
def get_image():
    buffer = BytesIO()
    fp = "/home/wst/workspace/lxdata/temp/QJ6118689748.jpg"
    image = Image.open(fp)
    image.save(buffer, format="png")
    buffer_bytes = buffer.getvalue()
    response = make_response(buffer_bytes)
    response.headers['Content-Type'] = 'image/png'
    return response

说明:第16行中的图片格式可以做这些设定:

在PIL中,使用save()方法时,可以指定多种格式来保存图像。一些常见的格式包括:

  1. JPEG: 通过指定format="JPEG"来保存为JPEG格式。
  2. PNG: 通过指定format="PNG"来保存为PNG格式。
  3. GIF: 通过指定format="GIF"来保存为GIF格式。
  4. BMP: 通过指定format="BMP"来保存为BMP格式。
  5. TIFF: 通过指定format="TIFF"来保存为TIFF格式。

除了上述常见格式外,PIL还支持诸如WebP、ICO、PDF等其他格式。通过在save()方法中使用format参数,可以灵活地控制输出图像的格式。

启动方式如下,假定以上内容保存到flask_demo.py中。

flask --app flask_demo run

访问地址:http://127.0.0.1:5000/image,即可看到返回的图片。

后续:

既然是字节码,那么直接从磁盘以二进制方式读出图片,是不是也可以呢?

那我们来试一下,改动代码:

from flask import Flask, make_response
from io import BytesIO
from PIL import Image

app = Flask(__name__)

@app.route("/")
def helloworld():
    return "<p>Hello, World!</p>"

@app.route("/image")
def get_image():
    buffer = BytesIO()
    fp = "/home/wst/workspace/lxdata/temp/QJ6118689748.jpg"
    buffer_bytes = open(fp, 'rb')
    response = make_response(buffer_bytes)
    response.headers['Content-Type'] = 'image/png'
    return response

结果:可行!


Comments(132) Add Your Comment

Rknxye
atorvastatin canada <a href="https://lipiws.top/">lipitor 80mg uk</a> lipitor without prescription
Yuxdyw
buy ciprofloxacin generic - <a href="https://metroagyl.top/clindamycin/">buy septra for sale</a> augmentin 375mg oral
Iiiwse
where can i buy ciprofloxacin - <a href="https://cipropro.top/">purchase ciprofloxacin pill</a> buy amoxiclav pill
Qxspxm
metronidazole 400mg cost - <a href="https://metroagyl.top/cefaclor/">order cefaclor 250mg capsules</a> order azithromycin 500mg pills
Jqcxec
order ciplox for sale - <a href="https://septrim.top/erythromycin/">purchase erythromycin</a> order erythromycin pill
Ysxcyv
valtrex 500mg for sale - <a href="https://gnantiviralp.com/nemasole/">buy nemasole pills for sale</a> order zovirax pill
Kyhbrl
ivermectin 2mg - <a href="https://keflexin.top/sumycin/">cheap sumycin generic</a> sumycin tablet
Aymrlp
buy flagyl 400mg for sale - <a href="https://metroagyl.top/keflexcapsule/">order amoxicillin pill</a> azithromycin 500mg price
Vqzkac
acillin pills <a href="https://ampiacil.top/amoxil500/">amoxil sale</a> purchase amoxil generic
Dgdblv
order furosemide generic - <a href="https://antipathogenc.com/atacand/">buy candesartan sale</a> purchase captopril without prescription
Pusrjg
buy glucophage generic - <a href="https://gnantiacidity.com/combivir/">lamivudine price</a> order lincocin 500mg online cheap
Orllrq
order retrovir 300 mg online pill - <a href="https://canadiangnp.com/zyloprim/"></a> order zyloprim 100mg online
Ooiqyo
order clozapine without prescription - <a href="https://genonlinep.com/">clozapine 50mg usa</a> pepcid online order
Dtjddh
order quetiapine 50mg for sale - <a href="https://gnkantdepres.com/eskalith/">buy eskalith pills</a> eskalith online
Usvlmi
clomipramine 25mg cost - <a href="https://antdeponline.com/remeron/">generic mirtazapine 15mg</a> sinequan 75mg drug
Ogeezx
purchase atarax generic - <a href="https://antdepls.com/pamelor/">buy nortriptyline 25 mg generic</a> purchase endep generic
Ilsphi
amoxil medication - <a href="https://atbioxotc.com/">buy amoxil cheap</a> order baycip online cheap
Mfbeif
augmentin 375mg tablet - <a href="https://atbioinfo.com/septra/">purchase septra online</a> buy baycip online cheap
Twhtwx
buy clindamycin online - <a href="https://cadbiot.com/oxytetracycline/">order oxytetracycline online</a> buy chloromycetin generic
Cocycg
order azithromycin 250mg for sale - <a href="https://gncatbp.com/btetracycline/">buy cheap generic tetracycline</a> ciprofloxacin over the counter
Zkzkik
ivermectine online - <a href="https://antibpl.com/szdoryx/">doxycycline brand</a> order cefaclor sale
Rypgek
brand ventolin 2mg - <a href="https://antxallergic.com/oiphenergan/">where to buy promethazine without a prescription</a> order theophylline 400 mg online
Ahpxrp
buy clarinex - <a href="https://rxtallerg.com/flixotidespray/">purchase flixotide nasal sprays</a> albuterol order
Djaerx
medrol cost in usa - <a href="https://ntallegpl.com/montelukast/">buy generic singulair 10mg</a> astelin medication
Nlxkbb
micronase canada - <a href="https://prodeprpl.com/dapagliflozin5/">forxiga 10 mg us</a> forxiga buy online
Vcjbut
buy prandin 2mg online cheap - <a href="https://depressinfo.com/jardiance25/">order jardiance without prescription</a> order generic jardiance 10mg
Rpqlwt
glucophage 1000mg without prescription - <a href="https://arxdepress.com/losartan25/">order losartan 50mg pills</a> order acarbose for sale
Fcbska
terbinafine online buy - <a href="https://treatfungusx.com/fulvicin250mg/">fulvicin online</a> purchase grifulvin v sale
Ypxahk
order rybelsus without prescription - <a href="https://infodeppl.com/">buy semaglutide 14 mg generic</a> desmopressin spray
Snhbav
nizoral over the counter - <a href="https://antifungusrp.com/itraconazole100/">buy sporanox 100mg sale</a> buy itraconazole
Fubenp
buy famvir 250mg pill - <a href="https://amvinherpes.com/">buy famvir medication</a> buy valcivir 1000mg for sale
Brdeun
order digoxin 250 mg without prescription - <a href="https://blpressureok.com/furosemidediuret/">furosemide 40mg price</a> furosemide usa
Tukbhp
hydrochlorothiazide generic - <a href="https://norvapril.com/lisinopril/">zestril cheap</a> buy generic bisoprolol online
Yebpfz
lopressor pills - <a href="https://bloodpresspl.com/">order lopressor generic</a> where to buy nifedipine without a prescription
Hhfuhh
where to buy nitroglycerin without a prescription - <a href="https://nitroproxl.com/valsartan/">order diovan 80mg online cheap</a> valsartan oral
Ptjwbv
zocor rep - <a href="https://canescholest.com/atorvastatin/">atorvastatin answer</a> lipitor secret
Duanoa
rosuvastatin online blast - <a href="https://antcholesterol.com/ezetimibe/">ezetimibe mill</a> caduet pills hurry
Skowgz
viagra professional encounter - <a href="https://edsildps.com/levitraoraljelly/">levitra oral jelly nearest</a> levitra oral jelly online actual
Etsfnz
cenforce always - <a href="https://xcenforcem.com/zenegrasildenafil/">zenegra kingdom</a> brand viagra pills harsh
Ntrnxl
brand cialis crush - <a href="https://probrandtad.com/alprostadilonline/">alprostadil consent</a> penisole wet
Zgtwnx
cialis soft tabs pills frog - <a href="https://supervalip.com/">cialis soft tabs pills council</a> viagra oral jelly ask
Tjjypm
brand cialis wine - <a href="https://probrandtad.com/penisole/">penisole thee</a> penisole hang
Mjowkm
cialis soft tabs online prince - <a href="https://supervalip.com/">cialis soft tabs dwarf</a> viagra oral jelly east
Woufvf
cenforce service - <a href="https://xcenforcem.com/cialistadalafil/">cialis medication</a> brand viagra online influence
Orjmqe
dapoxetine clothe - <a href="https://promedprili.com/">dapoxetine lighter</a> cialis with dapoxetine perfect
Fmwkuc
acne treatment explode - <a href="https://placnemedx.com/">acne treatment crumble</a> acne medication thoughtful
Bkgapt
asthma medication wobbler - <a href="https://bsasthmaps.com/">inhalers for asthma rack</a> asthma medication temple
Neiaqe
uti antibiotics hum - <a href="https://amenahealthp.com/">uti antibiotics horizon</a> uti antibiotics touch
Dvysop
prostatitis medications wet - <a href="https://xprosttreat.com/">pills for treat prostatitis dirty</a> prostatitis pills aim
Ekhbdf
valtrex online gesture - <a href="https://gnantiviralp.com/">valacyclovir pills major</a> valtrex pills armor
Ppsdfn
claritin pills pool - <a href="https://clatadine.top/">loratadine medication satisfy</a> claritin pills true
Suzpru
loratadine bonnet - <a href="https://clatadine.top/">loratadine write</a> claritin pills fresh
Usvrxd
priligy soil - <a href="https://prilixgn.top/">priligy haul</a> priligy sink
Lslqrq
promethazine two - <a href="https://prohnrg.com/">promethazine drop</a> promethazine bargain
Lgydaa
ascorbic acid low - <a href="https://ascxacid.com/">ascorbic acid roll</a> ascorbic acid torment
Hhxzsi
clarithromycin idea - <a href="https://gastropls.com/gncytotec/">cytotec mail</a> cytotec pills enormous
Iuqvqv
florinef pills extreme - <a href="https://gastroplusp.com/omepprilosec/">omeprazole succeed</a> lansoprazole pills fine
Dulgxw
bisacodyl generic - <a href="https://gastroinfop.com/liv52/">buy liv52 10mg online cheap</a> liv52 for sale online
Ghtbbh
buy generic aciphex - <a href="https://gastrointesl.com/reglanmetoclop/">metoclopramide 20mg ca</a> domperidone where to buy
Dbcgog
cotrimoxazole 960mg price - <a href="https://cotrioxazo.com/">order bactrim 960mg online</a> tobramycin oral
Rrsnjv
buy cheap generic eukroma - <a href="https://cerazestrel.com/">cerazette online buy</a> duphaston 10mg uk
Gdwtce
order forxiga 10 mg sale - <a href="https://forpaglif.com/">forxiga 10mg ca</a> precose sale
Wpfjzz
order fulvicin 250 mg without prescription - <a href="https://fulviseoful.com/">purchase griseofulvin generic</a> lopid 300 mg price
Sqvryj
enalapril 10mg uk - <a href="https://doxapisin.com/">buy doxazosin 1mg pill</a> zovirax tubes
Oidjxe
dimenhydrinate brand - <a href="https://actodronate.com/">buy actonel 35 mg</a> risedronate where to buy
Nmxxib
order piroxicam sale - <a href="https://rivastilons.com/">order exelon 6mg pills</a> buy generic exelon
Iakhqs
etodolac 600 mg cost - <a href="https://cilosetal.com/">cilostazol usa</a> order cilostazol 100mg sale
Vcchdd
piracetam over the counter - <a href="https://nootquin.com/efavirenz/">cheap sustiva</a> buy sinemet generic
Qwampi
hydrea online order - <a href="https://hydroydrinfo.com/pentoxifylline/">buy pentoxifylline generic</a> robaxin price
Dnxemj
purchase divalproex - <a href="https://adepamox.com/topiramate/">topiramate 100mg generic</a> buy topamax pills
Nfvlrt
cheap norpace without prescription - <a href="https://anorpica.com/lamivudine/">order epivir online</a> buy generic chlorpromazine over the counter
Nzubxl
buy cytoxan tablets - <a href="https://cycloxalp.com/trimetazidine/">cheap trimetazidine</a> vastarel cheap
Iyshfh
spironolactone drug - <a href="https://aldantinep.com/">generic aldactone 100mg</a> order revia 50mg without prescription
Sntydq
order cyclobenzaprine online cheap - <a href="https://abflequine.com/">flexeril online</a> order vasotec 5mg without prescription
Ruxlgd
brand ondansetron 8mg - <a href="https://azofarininfo.com/ropinirole/">purchase ropinirole pill</a> cost requip 1mg
Ikrunm
brand ascorbic acid - <a href="https://mdacidinfo.com/">order ascorbic acid 500 mg for sale</a> where can i buy prochlorperazine
Vzpqcg
order cheap durex gel - <a href="https://xalaplinfo.com/">order durex gel online cheap</a> zovirax order
Czlyfl
how to get rogaine without a prescription - <a href="https://hairlossmedinfo.com/finasteridehl/">order proscar generic</a> finasteride ca
Sakqwr
order arava without prescription - <a href="https://infohealthybones.com/calciumcarbonate/">buy calcium carbonate generic</a> order cartidin online cheap
Kenyeq
atenolol medication - <a href="https://heartmedinfox.com/clopidogrel/">buy clopidogrel cheap</a> where to buy coreg without a prescription
Ykpyjz
verapamil cost - <a href="https://infoheartdisea.com/cardizem/">diltiazem 180mg for sale</a> order tenoretic online cheap
Xhezkk
buy atorlip paypal - <a href="https://infoxheartmed.com/enalapril/">brand enalapril 5mg</a> generic nebivolol
Wthusb
gasex oral - <a href="https://herbalinfomez.com/">buy gasex paypal</a> buy diabecon generic
Skadmk
buy lasuna paypal - <a href="https://infoherbalmz.com/diarex/">cost diarex</a> where to buy himcolin without a prescription
Fuczob
speman where to buy - <a href="https://spmensht.com/">where can i buy speman</a> purchase finasteride sale
Jpjjns
norfloxacin where to buy - <a href="https://gmenshth.com/">noroxin pills</a> confido usa
Wotkdr
order finasteride sale - <a href="https://finmenura.com/">buy generic finasteride for sale</a> alfuzosin oral
Fwxblq
terazosin online order - <a href="https://hymenmax.com/dutasteride/">dutasteride over the counter</a> dapoxetine 90mg over the counter
Gfiulq
brand oxcarbazepine 600mg - <a href="https://trileoxine.com/">oxcarbazepine 300mg usa</a> order synthroid 75mcg pill
Tiolzx
how to get lactulose without a prescription - <a href="https://duphalinfo.com/betahistine/">order betahistine 16mg pills</a> betahistine brand
Sghskw
cost imusporin - <a href="https://asimusxate.com/gloperba/">buy colcrys 0.5mg generic</a> buy generic gloperba
Rdtmap
purchase deflazacort pill - <a href="https://lazacort.com/">buy generic calcort over the counter</a> order alphagan for sale
Qcmvsh
how to buy besifloxacin - <a href="https://besifxcist.com/">how to get besifloxacin without a prescription</a> buy sildamax tablets
Dccaph
buy generic neurontin for sale - <a href="https://aneutrin.com/sulfasalazine/">purchase azulfidine generic</a> azulfidine uk
Hzclxt
generic probenecid - <a href="https://bendoltol.com/monograph/">purchase etodolac without prescription</a> order tegretol 400mg online
Sqmzhg
buy celebrex 100mg pill - <a href="https://celespas.com/">where can i buy celecoxib</a> buy indomethacin 50mg pill
Ziucop
mebeverine us - <a href="https://coloxia.com/pletal/">buy cilostazol 100mg generic</a> order cilostazol online cheap
Xhvqvw
purchase diclofenac pill - <a href="https://dicloltarin.com/aspirin/">buy generic aspirin</a> buy aspirin online cheap
Zqllox
buy rumalaya sale - <a href="https://rumaxtol.com/amitriptyline/">endep drug</a> buy elavil
Fttaem
oral pyridostigmine - <a href="https://mestonsx.com/asumatriptan/">sumatriptan 50mg price</a> order imuran pills
Kijpjq
buy generic diclofenac over the counter - <a href="https://vovetosa.com/tisosorbide/">imdur 40mg ca</a> buy generic nimodipine over the counter
Tvfljp
buy baclofen 10mg generic - <a href="https://baclion.com/">lioresal buy online</a> purchase feldene online cheap
Eaabwg
purchase meloxicam sale - <a href="https://meloxiptan.com/asrizatriptan/">order rizatriptan 5mg for sale</a> ketorolac tablet
Kqllda
cyproheptadine pills - <a href="https://periheptadn.com/aszanaflex/">purchase tizanidine for sale</a> tizanidine over the counter
Hnwsnm
artane sale - <a href="https://voltapll.com/diclofenacgel/">emulgel online purchase</a> emulgel purchase online
Vufqce
cefdinir pill - <a href="https://omnixcin.com/aminocycline/"></a> where to buy clindamycin without a prescription
Uscrmr
buy accutane pills - <a href="https://aisotane.com/">accutane without prescription</a> deltasone 40mg brand
Dggxzq
order deltasone 10mg online - <a href="https://apreplson.com/permethrin/">buy zovirax creams</a> permethrin creams
Fmndjw
buy cheap generic acticin - <a href="https://actizacs.com/asbenzac/">buy cheap benzac</a> order tretinoin without prescription
Wkyisq
purchase betamethasone - <a href="https://betnoson.com/">betnovate 20gm cheap</a> buy monobenzone paypal
Opdops
buy flagyl 200mg online - <a href="https://cenforcevs.com/">purchase cenforce generic</a> cenforce 100mg over the counter
Htuuzt
amoxiclav pills - <a href="https://alevonted.com/">levothroid over the counter</a> cheap levoxyl
Cvtmej
cleocin 150mg tablet - <a href="https://indometnp.com/">buy indomethacin 50mg for sale</a> indomethacin online
Pmptlx
order losartan 25mg for sale - <a href="https://acephacrsa.com/">buy keflex without prescription</a> keflex order
Yslowr
crotamiton ca - <a href="https://aeuracream.com/caczone/">order aczone sale</a> how to get aczone without a prescription
Ahxcms
buy provigil 200mg without prescription - <a href="https://sleepagil.com/phenergan/">promethazine online order</a> meloset for sale online
Whjoae
order zyban generic - <a href="https://bupropsl.com/ayurslim/">order ayurslim for sale</a> cheap shuddha guggulu sale
Sqktfk
buy xeloda 500 mg sale - <a href="https://xelocap.com/naproxen/">buy cheap naproxen</a> danocrine 100 mg capsules
Bumthn
buy progesterone generic - <a href="https://apromid.com/clomipheneferto/">order generic clomiphene</a> fertomid without prescription
Dlmhne
alendronate 70mg over the counter - <a href="https://pilaxmax.com/tamoxifen/">buy nolvadex 20mg pills</a> medroxyprogesterone drug
Ijacvw
buy aygestin no prescription - <a href="https://norethgep.com/yasmin/">yasmin canada</a> buy yasmin generic
Mwpleb
generic estradiol 2mg - <a href="https://festrolp.com/">purchase estradiol online cheap</a> buy anastrozole 1mg for sale
Udbava
buy cabergoline 0.5mg - <a href="https://adostilin.com/">buy dostinex 0.25mg generic</a> cost alesse
Byrbbt
バイアグラの飲み方と効果 - <a href="https://jpedpharm.com/">シルデナフィル おすすめ</a> シアリス は通販での購
Jznrab
гѓ—гѓ¬гѓ‰гѓ‹гѓі еЂ‹дєєијёе…Ґ гЃЉгЃ™гЃ™г‚Ѓ - <a href="https://jpaonlinep.com/jamoxicillin/">г‚ўгѓўг‚­г‚·г‚·гѓЄгѓі е‰ЇдЅњз”Ё</a> г‚ёг‚№гѓ­гѓћгѓѓг‚Ї гЃЉгЃ™гЃ™г‚Ѓ
Eojdqu
プレドニンの飲み方と効果 - <a href="https://jpanfarap.com/jpadoxycycline/">ドキシサイクリン通販</a> イソトレチノイン錠 10 mg 強さ
Mbnbth
eriacta corner - <a href="https://eriagra.com/asildigrah/">sildigra leave</a> forzest assemble
Vhoewo
crixivan pills - <a href="https://confindin.com/fincar/">buy finasteride sale</a> order diclofenac gel online cheap
Togiff
valif pills serpent - <a href="https://avaltiva.com/rtsinemet/">buy sinemet 20mg sale</a> purchase sinemet pill
Erymhr
modafinil 200mg sale - <a href="https://provicef.com/">modafinil price</a> epivir pills
Fygfka
ivermectin 3mg for people - <a href="https://ivercand.com/candesartan/">candesartan cheap</a> carbamazepine 200mg price
Gwegfe
buy promethazine generic - <a href="https://prometacin.com/jilincomycin/">purchase lincocin for sale</a> lincocin 500mg uk