0

I am newbie in object-oriented design.

I try to incorporate a python function into my init . (Function works, as I asked earlier).

Here is my code:

import struct
import urllib2
import StringIO


class wallpaper:
    def __init__(self, url):
        self.url = url
        self.content_type = ''
        self.height = 0
        self.width = 0
        image = urllib2.urlopen(self.url)
        data = str(image.read(2))
        if  data.startswith('\377\330'):
            self.content_type = 'image/jpeg'
            jpeg = StringIO.StringIO(data)
            jpeg.read(2)
            b = jpeg.read(1)
            try:
                while (b and ord(b) != 0xDA):
                    while (ord(b) != 0xFF): b = jpeg.read(1)
                    while (ord(b) == 0xFF): b = jpeg.read(1)
                    if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
                        jpeg.read(3)
                        h, w = struct.unpack(">HH", jpeg.read(4))
                        break
                    else:
                        jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2)
                        b = jpeg.read(1)
                    self.width = int(w)
                    self.height = int(h)
            except struct.error:
                pass
            except ValueError:
                pass


x = wallpaper('http://i.imgur.com/rapwX.jpg')
print x.url, "\t", x.content_type,"\t", x.height,"\t", x.width

The problem is that I cannot initialize height and width properties (they are equal 0).

Where is the problem?

Edit:

I've found problem, here is solution. Thanks for your hints.

One more question: is my approach proper as far as OOP rules are concerned? Should I compute width/height in init or create other method (eg. size, or so)?

Community
  • 1
  • 1
grotos
  • 493
  • 2
  • 6
  • 11
  • 1
    Instead of playing with the binary data your self, you can use the (Python Image Library)[http://www.pythonware.com/products/pil/] to handle all the low-level stuff. – Katriel Sep 18 '11 at 14:51
  • Why do you ignore any ValueError? (and strtuct.error?) – rvs Sep 18 '11 at 14:52

3 Answers3

3

You're only reading 2 bytes into data.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • But that works well - you don't have to download the whole image to get its dimensions. – grotos Sep 18 '11 at 15:06
  • Do you actually believe that? Have you tried reading the code? – Ignacio Vazquez-Abrams Sep 18 '11 at 15:09
  • I did only some testing with large images (about 4MB) - this script is as responsive as with smaller ones. I know this is not the best way to check it, but I don't have other tools to measure precisely. – grotos Sep 18 '11 at 15:59
1

It will be difficult to determine the problem if you simply ignore exceptions:

    except struct.error:
        pass
    except ValueError:
        pass

At least you should print the error messages:

    except struct.error as exc:
        print str(exc)
    except ValueError as exc:
        print str(exc)

This way you should be able to debug your code and thus solve your problem.

Of course, I assume that the indentation is correct in your actual code.

robert
  • 3,484
  • 3
  • 29
  • 38
1

If this condition

ord(b) >= 0xC0 and ord(b) <= 0xC3

is true at the first iteration of the outer while , you break out of the loop before assigning self.width and self.height. Maybe you meant to indent the two assignments one level less than they are in your code?

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55