0

I am trying to receive an image in python to use it in my program.

Here is the sever code:

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("127.0.0.1", 5005))
server_socket.listen(5)

data = ' ' 
client_socket, address = server_socket.accept()
print "Conencted to - ",address,"\n"
while (1):
    data = client_socket.recv(1024)
    print "The following data was received - ",data
    print "Opening file - ",data
    img = open(data,'r')
    while True:
      strng = img.readline(512)
      if not strng:
        break
      client_socket.send(strng)
      img.close()
      print "Data sent successfully"
      exit()
      #data = 'viewnior '+data
      #os.system(data)

And here is the client code:

import socket,os
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("127.0.0.1", 5005))
size = 1024

while(1):
    print "Enter file name of the image with extentsion (example: filename.jpg,filename.png or if a video file then filename.mpg etc) - "
    fname = raw_input()
    client_socket.send(fname)
    #fname = 'documents/'+fname
    fp = open(fname,'w')
    while True:
        strng = client_socket.recv(512)
        if not strng:
            break
        fp.write(strng)
    fp.close()
    print "Data Received successfully"
    exit()
    #data = 'viewnior '+fname
    #os.system(data)

The received should now be read to be able to use it. I am opening it like this:

input_image = Image.open('data').convert('L').resize((100, 100))

but when I run both codes in cmd the output is: The following data was received - + path Opening file - + path Then nothing happens although the image should be used and the final output should be shown.

Anyone can help?

3
  • 1
    I assume you are using PIL? Anyway, is the received JPEG valid, or has it been corrupted during the transfer? Are you able to open it with an other program? Commented Jun 7, 2013 at 18:22
  • Yes I am using PIL. How can I know if it has been corrupted during transfer or not? Commented Jun 7, 2013 at 18:28
  • Please read more carefully my previous comment: "Are you able to open [your received file] with an other program?" Gimp, Photoshop or whatever will tell you if it is still a valid JPEG or not. You might still want to compare the size of both the original and received file. But maybe your problem is not here? I'm just guessing -- or at least naming the first few things that I would have checked if I were you... Commented Jun 7, 2013 at 18:35

1 Answer 1

1

I don't know if this is your (only) problem, but when working with binary files, you should pass the b flag to the built-in function open:

img = open(data, 'rb')
Sign up to request clarification or add additional context in comments.

1 Comment

This was indeed not the only problem. By looking more closely to your code, and given the output you observed, I assume the variable data (which should contain the file name) is empty. Are you sure sockets.recv is working as you expect in data = client_socket.recv(1024)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.