[Image-SIG] urlopen for webpage image file

Richard Boyd richard-boyd@worldnet.att.net
Fri, 26 Jul 2002 09:13:03 -0700


--=====================_7670769==_.ALT
Content-Type: text/plain; charset="us-ascii"; format=flowed

I need to dynamically access a webpage and find the largest graphic used on 
that page, so I tried the following ...
-----------------------------
import Image
import urllib

filename = "http://www.python.org/pics/PythonPoweredSmall.gif"
filelocation = urllib.urlopen(filename)
img = Image.open(filelocation)
img.size
-----------------------------


But I got the an error msg ...
------------error msg-----------------
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "F:\Dev\Python22\Lib\site-packages\PIL\Image.py", line 955, in open
fp.seek(0)
AttributeError: addinfourl instance has no attribute 'seek'
---------------------------------


I browsed the Python docs and found the following ...
---------------------------------
11.4 urllib -- Open arbitrary resources by URL

This module provides a high-level interface for fetching data across the 
World Wide Web.
In particular, the urlopen() function is similar to the built-in function 
open(),
but accepts Universal Resource Locators (URLs) instead of filenames.
Some restrictions apply -- it can only open URLs for reading, and no seek 
operations
are available.
.......
(It is not a built-in file object, however, so it can not be used at those 
few places
where a true built-in file object is required.)
---------------------------------


I then noticed that in the PIL Manual it says for Image.open() that, "You 
can use either a string (giving the filename) or a file object. In the 
latter case, the file object must implement read, seek, and tell methods, 
and be opened in binary mode."

Since urllib.urlopen() does not provide a seek operation ... I get the 
error msg above.

Is there a way to use PIL image functions to access the size info of a web 
graphic image file ... without downloading (too time consuming) the whole 
image?

Thanx.

-richard-


P.S.    What I had to do was switch to REBOL [www.rebol.com]. It is very 
simple in REBOL ...
         print size? http://www.python.org/pics/PythonPoweredSmall.gif

returns the size ...
         361

--=====================_7670769==_.ALT
Content-Type: text/html; charset="us-ascii"

<html>
I need to dynamically access a webpage and find the largest graphic used
on that page, so I tried the following ...<br>
-----------------------------<br>
import Image<br>
import urllib<br><br>
filename =
&quot;<a href="http://www.python.org/pics/PythonPoweredSmall.gif" eudora="autourl"><font color="#0000FF"><u>http://www.python.org/pics/PythonPoweredSmall.</a><a href="http://www.python.org/pics/PythonPoweredSmall.gif" eudora="autourl">gif</a></u></font>&quot;<br>
filelocation = urllib.urlopen(filename)<br>
img = Image.open(filelocation)<br>
img.size<br>
-----------------------------<br><br>
<br>
But I got the an error msg ...<br>
------------error msg-----------------<br>
Traceback (most recent call last):<br>
File &quot;&lt;interactive input&gt;&quot;, line 1, in ?<br>
File &quot;F:\Dev\Python22\Lib\site-packages\PIL\Image.py&quot;, line
955, in open<br>
fp.seek(0)<br>
AttributeError: addinfourl instance has <b>no attribute 'seek'<br>
</b>---------------------------------<br><br>
<br>
I browsed the Python docs and found the following ...<br>
---------------------------------<br>
11.4 urllib -- Open arbitrary resources by URL <br><br>
This module provides a high-level interface for fetching data across the
World Wide Web. <br>
In particular, the urlopen() function is similar to the built-in function
open(), <br>
but accepts Universal Resource Locators (URLs) instead of filenames.
<br>
<b>Some restrictions apply</b> -- it can only open URLs for reading, and
<b>no seek operations</b> <br>
are available. <br>
.......<br>
(It is not a built-in file object, however, so it <b>can not be used</b>
at those few places <br>
where a true built-in file object is required.) <br>
---------------------------------<br><br>
<br>
I then noticed that in the PIL Manual it says for <b>Image.open()</b>
that, &quot;You can use either a string (giving the filename) or a file
object. In the latter case, the file object <b>must implement
<i>read</i>, <i>seek</i>, and <i>tell </i>methods,</b> and be opened in
binary mode.&quot;<br><br>
Since <b>urllib.urlopen() </b>does not provide a <b>seek </b>operation
... I get the error msg above.<br><br>
Is there a way to use PIL image functions to access the <b>size </b>info
of a web graphic image file ... without downloading (too time consuming)
the whole image?<br><br>
Thanx.<br><br>
-richard-<br><br>
<br>
P.S.<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>What I had to do was switch to
REBOL
[<a href="http://www.rebol.com/" eudora="autourl">www.rebol.com</a>]. It
is very simple in REBOL ...<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>print
size?
<a href="http://www.python.org/pics/PythonPoweredSmall.gif" eudora="autourl">http://www.python.org/pics/PythonPoweredSmall.gif</a><br><br>
returns the size ... <br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>361<br>
</html>

--=====================_7670769==_.ALT--