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
  • Maybe try adding flush=True to parameters of last line. – Mark Setchell Commented Nov 21, 2024 at 17:36
  • Or maybe it's sys.stdout.flush() as a new final line. – Mark Setchell Commented Nov 21, 2024 at 17:43
  • @MarkSetchell: Thank you for the suggestions. flush=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
  • Try running your script in the Terminal like this ./sendthumb.cgi > junk and see if the file junk is 40-50 bytes longer than testpic.jpg - the additional bytes being the HTTP header you added. – Mark Setchell Commented Nov 21, 2024 at 18:04
  • @MarkSetchell Well of course it's much smaller, because it has been thumbnailed. But if I double-click on junk it opens in Image Viewer as a smaller copy of the original image. – Andy31 Commented Nov 21, 2024 at 18:43
 |  Show 2 more comments

1 Answer 1

Reset to default 1

To 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