admin管理员组文章数量:1122832
For many years I have used a small Python2 script which sends thumbnails of images to a webpage.
A minimal working version is:
Webpage:
<img src="./sendthumb.cgi" />
Python2 script sendthumb.cgi
:
#! /usr/bin/python
from PIL import Image
from StringIO import StringIO
import sys
pic = "testpic.jpg"
im = Image.open(pic)
im.thumbnail((256, 256))
buf = StringIO()
im.save(buf, "JPEG")
sys.stdout.write('Status: 200 OK\r\n')
sys.stdout.write('Content-type: image/jpeg\r\n')
sys.stdout.write('\r\n')
sys.stdout.write(buf.getvalue())
However, my service providers are now switching off Python2, so it's time to amend the script to work with Python3. A bit of research suggests that I need to switch StringIO to BytesIO, and send to sys.stdout.buffer. So the new script is:
#! /usr/bin/python3
from PIL import Image
from io import BytesIO
import sys
pic = "testpic.jpg"
im = Image.open(pic)
im.thumbnail((256, 256))
buf = BytesIO()
im.save(buf, "JPEG")
sys.stdout.write('Status: 200 OK\r\n')
sys.stdout.write('Content-type: image/jpeg\r\n')
sys.stdout.write('\r\n')
sys.stdout.buffer.write(buf.getvalue())
This doesn't seem to trigger any errors - but it also doesn't display the image on the webpage. Could somebody point out how to correct this?
For many years I have used a small Python2 script which sends thumbnails of images to a webpage.
A minimal working version is:
Webpage:
<img src="./sendthumb.cgi" />
Python2 script sendthumb.cgi
:
#! /usr/bin/python
from PIL import Image
from StringIO import StringIO
import sys
pic = "testpic.jpg"
im = Image.open(pic)
im.thumbnail((256, 256))
buf = StringIO()
im.save(buf, "JPEG")
sys.stdout.write('Status: 200 OK\r\n')
sys.stdout.write('Content-type: image/jpeg\r\n')
sys.stdout.write('\r\n')
sys.stdout.write(buf.getvalue())
However, my service providers are now switching off Python2, so it's time to amend the script to work with Python3. A bit of research suggests that I need to switch StringIO to BytesIO, and send to sys.stdout.buffer. So the new script is:
#! /usr/bin/python3
from PIL import Image
from io import BytesIO
import sys
pic = "testpic.jpg"
im = Image.open(pic)
im.thumbnail((256, 256))
buf = BytesIO()
im.save(buf, "JPEG")
sys.stdout.write('Status: 200 OK\r\n')
sys.stdout.write('Content-type: image/jpeg\r\n')
sys.stdout.write('\r\n')
sys.stdout.buffer.write(buf.getvalue())
This doesn't seem to trigger any errors - but it also doesn't display the image on the webpage. Could somebody point out how to correct this?
Share Improve this question asked Nov 21, 2024 at 17:29 Andy31Andy31 1231 silver badge7 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 1To answer my own question, in case it helps others.
The solution is to send all the output to sys.stdout.buffer
:
sys.stdout.buffer.write(b'Status: 200 OK\r\n')
sys.stdout.buffer.write(b'Content-type: image/jpeg\r\n')
sys.stdout.buffer.write(b'\r\n')
sys.stdout.buffer.write(buf.getvalue())
本文标签: Sending image to webpagehow to change Python 2 code to Python 3Stack Overflow
版权声明:本文标题:Sending image to webpage - how to change Python 2 code to Python 3? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308458a1933668.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
flush=True
to parameters of last line. – Mark Setchell Commented Nov 21, 2024 at 17:36sys.stdout.flush()
as a new final line. – Mark Setchell Commented Nov 21, 2024 at 17:43flush=True
triggers an error;sys.stdout.flush()
doesn't give an error but there's still no image displayed. – Andy31 Commented Nov 21, 2024 at 18:00./sendthumb.cgi > junk
and see if the filejunk
is 40-50 bytes longer thantestpic.jpg
- the additional bytes being the HTTP header you added. – Mark Setchell Commented Nov 21, 2024 at 18:04junk
it opens in Image Viewer as a smaller copy of the original image. – Andy31 Commented Nov 21, 2024 at 18:43