473,698 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wxPython: images from URLs

Does anyone here know if the wxImage class in wxPython supports dislaying
images from URLs?

--

Jonathan Daugherty
http://www.cprogrammer.org

"It's a book about a Spanish guy called Manual, you should read it."
-- Dilbert

Jul 18 '05 #1
8 7608
"Jonathan Daugherty" <cy****@cprogra mmer.org> wrote in message
news:ma******** *************** *************** @python.org...
Does anyone here know if the wxImage class in wxPython supports dislaying
images from URLs?


Yes, the trick is to use StringIO to convert the data rather than saving to
a file and loading it from disk. Here's a concrete example...

ka
---

import urllib
from wxPython import wx
from cStringIO import StringIO

# I'll assume you already have an app, frame...
# to draw into if that's what you want to do

wx.wxInitAllIma geHandlers()

# here's a real URL for testing purposes
url = 'http://pythoncard.sour ceforge.net/images/addresses_01.pn g'

try:
fp = urllib.urlopen( url)
data = fp.read()
fp.close()
img = wx.wxImageFromS tream(StringIO( data))
except:
# decide what you want to do in case of errors
# there could be a problem getting the data
# or the data might not be a valid jpeg, png...
pass

# now you can do whatever you want with the image
Jul 18 '05 #2
Jonathan Daugherty <cy****@cprogra mmer.org> wrote:

Does anyone here know if the wxImage class in wxPython supports dislaying
images from URLs?


wxImage will read from a file or from a wxWindows stream. It won't
download from a web site, but that's trivially easy using something like
urllib.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #3
I have written some code for this in my PyWiew application
which allows one to open image urls directly.

Copying some relevant code from the application...

self._imgstream = urllib2.urlopen (url).read()
stream=cStringI O.StringIO(self ._imgstream)

try:
img=wxImageFrom Stream(stream)
except:
pass

In short you do the following.

1. Use urllib or urllib2 to open the image data stream
2. Make a cStringIO string buffer from the data stream
3. Pass it to "wxImageFromStr eam()" method to get the
wxImage object.
4. Display the image directly or by converting to
a suitable format using PIL.

In my experience I found that wxWindows tend to
display an error message window when the image is displayed
directly as a wxImage though the image data is quite ok.
(Something like a corrupt stream dialog). So what I have
done in the application is, use PIL to convert the wxImage
to Windows BMP format and then display it. This seems
to work for all images.

HTH.

-Anand

Tim Roberts <ti**@probo.com > wrote in message news:<fg******* *************** **********@4ax. com>...
Jonathan Daugherty <cy****@cprogra mmer.org> wrote:

Does anyone here know if the wxImage class in wxPython supports dislaying
images from URLs?


wxImage will read from a file or from a wxWindows stream. It won't
download from a web site, but that's trivially easy using something like
urllib.

Jul 18 '05 #4
# self._imgstream = urllib2.urlopen (url).read()
# stream=cStringI O.StringIO(self ._imgstream)
#
# try:
# img=wxImageFrom Stream(stream)
# except:
# pass

I have tried this and it appears to work, but once I have
the image (from wxImageFromStre am), I use it as follows:

try:
bm = wxBitmap(img)
self.bitmap.set Bitmap(bm)
except Exception, e:
print e

And the exception (raised by wxBitmap(img)) is:

String or Unicode type required

(The exception is a TypeError exception.)

Any ideas? No exceptions are raised by the block that
creates the image from the data stream. The image is
a JPG image, and I pass wxBITMAP_TYPE_J PEG to
wxImageFromStre am. I have also tried omitting it as
well.

--

Jonathan Daugherty
http://www.cprogrammer.org

"It's a book about a Spanish guy called Manual, you should read it."
-- Dilbert

Jul 18 '05 #5
This is the straight forward way to do this in wxPython
but somehow it always pops up that ugly error window.
I remember trying many options to do this purely using
wxPython (wxWindows), but I failed.

If you use PIL in your program you can conver the
wx Image instance to a PIL image of type BMP and then
display it by reconverting it back to the wxImage
instance. PyWiew has methods to do this. The source code
is available somewhere in my Python page at
http://members.lycos.co.uk/anandpillai . I no longer
maintain that program, but the latest source code should
be available there.

Regards

-Anand

Jonathan Daugherty <cy****@cprogra mmer.org> wrote in message news:<ma******* *************** *************** *@python.org>.. .
# self._imgstream = urllib2.urlopen (url).read()
# stream=cStringI O.StringIO(self ._imgstream)
#
# try:
# img=wxImageFrom Stream(stream)
# except:
# pass

I have tried this and it appears to work, but once I have
the image (from wxImageFromStre am), I use it as follows:

try:
bm = wxBitmap(img)
self.bitmap.set Bitmap(bm)
except Exception, e:
print e

And the exception (raised by wxBitmap(img)) is:

String or Unicode type required

(The exception is a TypeError exception.)

Any ideas? No exceptions are raised by the block that
creates the image from the data stream. The image is
a JPG image, and I pass wxBITMAP_TYPE_J PEG to
wxImageFromStre am. I have also tried omitting it as
well.

Jul 18 '05 #6
"Anand Pillai" <py*******@Hotp op.com> wrote in message
news:84******** *************** **@posting.goog le.com...
I have written some code for this in my PyWiew application
which allows one to open image urls directly.

Copying some relevant code from the application...

self._imgstream = urllib2.urlopen (url).read()
stream=cStringI O.StringIO(self ._imgstream)

try:
img=wxImageFrom Stream(stream)
except:
pass

In short you do the following.

1. Use urllib or urllib2 to open the image data stream
2. Make a cStringIO string buffer from the data stream
3. Pass it to "wxImageFromStr eam()" method to get the
wxImage object.
4. Display the image directly or by converting to
a suitable format using PIL.

In my experience I found that wxWindows tend to
display an error message window when the image is displayed
directly as a wxImage though the image data is quite ok.
(Something like a corrupt stream dialog). So what I have
done in the application is, use PIL to convert the wxImage
to Windows BMP format and then display it. This seems
to work for all images.

HTH.

-Anand

Tim Roberts <ti**@probo.com > wrote in message

news:<fg******* *************** **********@4ax. com>...
Jonathan Daugherty <cy****@cprogra mmer.org> wrote:

Does anyone here know if the wxImage class in wxPython supports dislayingimages from URLs?


wxImage will read from a file or from a wxWindows stream. It won't
download from a web site, but that's trivially easy using something like
urllib.


In general you don't display a wxImage object directly. In wxWindows,
wxImage is the platform-independent image class and wxBitmap is the
platform-specific image class. So, when you set an image to be used with a
wxStaticBitmap or wxBitmapButton or draw to a wxDC you use a wxBitmap.
Certain image operations not yet supported by wxWindows are easier to handle
with PIL in which case your solution of keeping a working image in PIL
format and then converting a wxBitmap prior to display is also a good
solution. Conversion is covered in the wxPython wiki.

http://wiki.wxpython.org/

Here are some of the conversion routines PythonCard uses to deal with PIL,
wxImage, wxBitmap, and NumPy numeric arrays.

def PILToImage(pilI mage):
if (pilImage.mode != 'RGB'):
pilImage = pilImage.conver t('RGB')
imageData = pilImage.tostri ng('raw', 'RGB')
img = wx.wxEmptyImage (pilImage.size[0], pilImage.size[1])
img.SetData(ima geData)
return img

def PILToBitmap(ima ge):
return wx.wxBitmapFrom Image(PILToImag e(image))

def BitmapToPIL(bmp ):
imageData = wx.wxImageFromB itmap(bmp).GetD ata()
imagePIL = fromstring('RGB ', (bmp.GetWidth() , bmp.GetHeight() ),
imageData)
imagePIL = imagePIL.conver t('RGB')
return imagePIL

def numericArrayToI mage(array):
height, width = array.shape[:2]
img = wx.wxEmptyImage (width, height)
img.SetData(arr ay.tostring())
return img

ka
Jul 18 '05 #7
Jonathan Daugherty <cy****@cprogra mmer.org> wrote:
# self._imgstream = urllib2.urlopen (url).read()
# stream=cStringI O.StringIO(self ._imgstream)
#
# try:
# img=wxImageFrom Stream(stream)
# except:
# pass

I have tried this and it appears to work, but once I have
the image (from wxImageFromStre am), I use it as follows:

try:
bm = wxBitmap(img)
self.bitmap.set Bitmap(bm)
except Exception, e:
print e


Why wouldn't you use wxBitmapFromIma ge?
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #8
# Why wouldn't you use wxBitmapFromIma ge?

The api docs suggest that using the wxBitmap(image) constructor
is better. Besides: this is beside the point -- this code fails:

stream = cStringIO.Strin gIO(imgdata)
imgobj = wx.wxImage(stre am)

The second line raises a TypeError: "String or Unicode type
required."

--

Jonathan Daugherty
http://www.cprogrammer.org

"It's a book about a Spanish guy called Manual, you should read it."
-- Dilbert

Jul 18 '05 #9

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

Similar topics

5
2097
by: Andreas Volz | last post by:
Hi, I used SGMLParser to parse all href's in a html file. Now I need to cut some strings. For example: http://www.example.com/dir/example.html Now I like to cut the string, so that only domain and directory is left over. Expected result:
2
3177
by: dr_tyson | last post by:
I am trying to embed images into a wxPython app (created using Boa Constructor), but have not been able to do so. I know how to embed plots, but images seem to be a problem. I've tried using code analogous to the example given at the Matplotlib website to no avail. If anybody has been successful at this could you please post some sample code? That would be greatly appreciated. Thanks! Randy
0
1654
by: Robin Dunn | last post by:
Announcing ---------- The 2.6.3.0 release of wxPython is now available for download at http://wxpython.org/download.php. There have been many enhancements and fixes implemented in this version, many of which are listed below and at http://wxpython.org/recentchanges.php. What is wxPython?
0
1534
by: Robin Dunn | last post by:
Announcing ---------- The 2.6.3.0 release of wxPython is now available for download at http://wxpython.org/download.php. There have been many enhancements and fixes implemented in this version, many of which are listed below and at http://wxpython.org/recentchanges.php. What is wxPython?
2
3017
by: Simon Wigzell | last post by:
I have inherited a database driven website that comes with a table of image links. The images are scattered all of the internet and there are thousands of them. I would like to write an asp script to just run through the table and download every link by it's URL. I thought I had id by faking a form with the "File" element and loading it with all the URLs. Would have worked except you cannot preload the "File" element. As I understand it,...
8
3714
by: Janwillem | last post by:
Is there a way to force the wx.FileDialog to show as default the thumbnails vie in stead of list view? thanks, janwillem
3
1494
by: wongjoekmeu | last post by:
Dear All, I want to write a GUI program with wxPython displaying an image. But the image I have is monochromatic. When I retrieve the data from the image I end up with a list of integer. Starting from a list of integer and knowing the width and height of the image, how do I display such an image on a wx panel or frame ? I have had a look at the wxPython demo but there I see only images where the data is a list of tuple consisting of r,g...
2
2851
tharden3
by: tharden3 | last post by:
Ok, I've been going through these tutorials for both PyGame and wxPython, but I've hit a big speed bump. In each tutorial, I've gotten to the point where I need to "import" or "load" images for various purposes (animation, icons, etc.). Each time I follow the tutorial, I can't quite get it right... please don't tell me "Hey, you can't use the pictures in the tutorial example because those are not the directories, paths, or pics that are on your ...
5
13363
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL http://mghospedagem.com/images/controlpanel.jpg instead of http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg As u see, there's the website URL before the image URL.
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.