473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Writing an image

Hi all, I was trying to hava a CGI script return an image, but it was coming
back corrupted. So I tried this simple non-CGI test and still get errors:

import sys
image = open('pic.jpg','rb').read()
sys.stdout.write(image)

When I run this like "python pic.py > pic2.jpg"
The resulting pic2.jpg is not identical to the original. I looked at it in
a hex editor, and it seems to be identical with the original pic.jpg, except
for several 0D (carraige return) characters inserted throughout the file.

I tried this with Python 2.2.1 and 2.3.2. What's going on?
Thanks in advance,
-Noah


Jul 18 '05 #1
4 2806
"Mr. Costington" <no***@juno.no.spam.com> writes:
Hi all, I was trying to hava a CGI script return an image, but it was coming
back corrupted. So I tried this simple non-CGI test and still get errors:

import sys
image = open('pic.jpg','rb').read()
sys.stdout.write(image)

When I run this like "python pic.py > pic2.jpg"
The resulting pic2.jpg is not identical to the original. I looked at it in
a hex editor, and it seems to be identical with the original pic.jpg, except
for several 0D (carraige return) characters inserted throughout the file.


sys.stdout is not usually a binary-mode file.

Cheers,
mwh

--
It could be argued that since Suitespot is infinitely terrible,
that anything else, by very definition of being anything else,
is infinitely superior. -- ".", alt.sysadmin.recovery
Jul 18 '05 #2
Hi,

Mr. Costington wrote:

When I run this like "python pic.py > pic2.jpg"
The resulting pic2.jpg is not identical to the original. I looked at it in
a hex editor, and it seems to be identical with the original pic.jpg, except
for several 0D (carraige return) characters inserted throughout the file.

I tried this with Python 2.2.1 and 2.3.2. What's going on?


The sys.stdout is obviously open in text (not binary) mode.

When you write "\n" (linefeed) you'll get "\r\n" (carriage return +
linefeed) in the destination.

hth

Werner

Jul 18 '05 #3
Michael Hudson wrote:
"Mr. Costington" <no***@juno.no.spam.com> writes:
Hi all, I was trying to hava a CGI script return an image, but it was
... sys.stdout is not usually a binary-mode file.


Right; one can change that with the -u switch to python (e.g., starting
the cgi script with
#!c:\python23\python.exe -u
if one's webserver recognizes and applies hashbang conventions).
Alex

Jul 18 '05 #4

"Alex Martelli" <al***@aleax.it> wrote in message
news:pQ*********************@news1.tin.it...
Michael Hudson wrote:
"Mr. Costington" <no***@juno.no.spam.com> writes:
Hi all, I was trying to hava a CGI script return an image, but it was

...
sys.stdout is not usually a binary-mode file.


Right; one can change that with the -u switch to python (e.g., starting
the cgi script with
#!c:\python23\python.exe -u
if one's webserver recognizes and applies hashbang conventions).


Thanks. My server ignores hashbangs, but I was able to set sys.stdout to
binary-mode with the following line:
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: news.skynet.be | last post by:
Hello, pretty new to java so bear with me. I am trying to write an image over an HttpURLConnection. I have an example of how to write text but I am having real problems trying to write an...
5
by: Ben Jeurissen | last post by:
Hello, I have to deal with the following issue in C++: Two threads are started from the main thread, both capturing images from a different firewire camera. Both threads take shots of 460800...
9
by: Raj | last post by:
Hello Members, I wrote a program to convert a greyscale bitmap image in to monochrome bitmap image, a simple thresholding. Input:Greyscale image; Expected Output:Monochrome image Pseudocode:...
17
by: Eric Lindsay | last post by:
Is learning to write CSS a better use of time than finding and using a package that produces complete web pages? I've moved to a new platform (Macintosh), taking with me about 400 personal web...
2
by: | last post by:
Hello All, I am writing a web application that reads a bitmap from a file and outputing it to a HTTP response stream to return the image to the requesting client. The image file is a regular...
1
by: Bruce D | last post by:
I have two methods to write to the database. The first one saves the file to disk the re-reads it and then inserts it. This way works...at least I'm able to read the image when I retrieve it....
1
by: kals | last post by:
i have written the code for reading and writing an image file but the problem is that its output image is not the same as input......i.e output is some random image consisting of scribbled black and...
4
by: cagdasal | last post by:
Hello there, i am having the following problem, when i read an image the following way: public void readBitmapImage (String imagePath){ ImageDir = imagePath; //Read in the image file...
2
by: ThatsIT.net.au | last post by:
I have this code that writes a pie chart in a asp.net page, but I want to use it in a server control. When I try I get a error on the last line "Response.OutputStream" Obviously there is no...
6
by: bradyounie | last post by:
I'm writing a program that displays a user-supplied Bitmap and then writes text fields to it. These "text fields" are things that the user can move around on the image, but to render them...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.