[reportlab-users] Drawing a remote Image from a URL without writing to disk
Robin Becker
robin at reportlab.com
Wed May 30 11:49:30 EDT 2012
Hi Damian,
I made these changes to trunk
===================================================================
--- renderPDF.py (revision 3901)
+++ renderPDF.py (working copy)
@@ -78,10 +78,11 @@
)
def drawImage(self, image):
+ path = image.path
# currently not implemented in other renderers
- if image.path and os.path.exists(image.path):
+ if path and (hasattr(path,'mode') or os.path.exists(image.path)):
self._canvas.drawInlineImage(
- image.path,
+ path,
image.x, image.y,
image.width, image.height
)
Index: renderPM.py
===================================================================
--- renderPM.py (revision 3901)
+++ renderPM.py (working copy)
@@ -123,19 +123,22 @@
self._canvas.line(line.x1,line.y1,line.x2,line.y2)
def drawImage(self, image):
- if image.path and os.path.exists(image.path):
- if type(image.path) is type(''):
- im = _getImage().open(image.path).convert('RGB')
- else:
- im = image.path.convert('RGB')
- srcW, srcH = im.size
- dstW, dstH = image.width, image.height
- if dstW is None: dstW = srcW
- if dstH is None: dstH = srcH
- self._canvas._aapixbuf(
- image.x, image.y, dstW, dstH,
- im.tostring(), srcW, srcH, 3,
- )
+ path = image.path
+ if isinstance(path,basestring):
+ if not (path and os.path.isfile(path)): return
+ im = _getImage().open(path).convert('RGB')
+ elif hasattr(path,'convert'):
+ im = path.convert('RGB')
+ else:
+ return
+ srcW, srcH = im.size
+ dstW, dstH = image.width, image.height
+ if dstW is None: dstW = srcW
+ if dstH is None: dstH = srcH
+ self._canvas._aapixbuf(
+ image.x, image.y, dstW, dstH,
+ im.tostring(), srcW, srcH, 3,
+ )
def drawCircle(self, circle):
c = self._canvas
and then this script appears to work fine to produce png and pdf (twice).
####################################################################
from cStringIO import StringIO
import urllib2 as urllib
from reportlab.pdfgen import canvas
from reportlab.graphics import renderPDF
from reportlab.lib.pagesizes import A4
from reportlab.platypus import Frame
from reportlab.graphics.shapes import Drawing, Image
from PIL import Image as PILImage
def map_image(lat, lon):
url =
'http://maps.googleapis.com/maps/api/staticmap?center=%s,%s&zoom=16&size=400x300&maptype=roadmap&markers=%s,%s&sensor=false'
% (lat, lon, lat, lon)
img_file = urllib.urlopen(url)
im = StringIO(img_file.read())
return PILImage.open(im)
c = canvas.Canvas('map.pdf', pagesize=A4)
story = []
drawing = Drawing(400, 300)
drawing.add(Image(0, 0, 200, 150, map_image(40.711614, -74.012318)))
story.append(drawing)
margin = 72
f = Frame(margin, margin, A4[0]-(margin*2), A4[1]-(margin*2))
f.addFromList(story, c)
c.showPage()
c.save()
drawing = Drawing(400, 300)
drawing.add(Image(0, 0, 200, 150, map_image(40.711614, -74.012318)))
drawing.save(formats=['png','pdf'],outDir='.',fnRoot='drawingmap')
####################################################################
I have run all the tests OK and it will be in the daily build in the morning.
On 30/05/2012 15:45, Damian Moore wrote:
> Hi Robin,
>> ..try creating a PIL Image from your data source and try assigning that to the
>> shapes.Image instance path attribute.
>>
>> That will work for renderPDF, but perhaps you want to use this for another
>> renderer.
>
> Thanks for the insight. Unfortunately it seems a PIL Image doesn't work either.
> I get 'TypeError: coercing to Unicode: need string or buffer, instance found.'.
> I've cut it down to the relevant code. Any ideas?
>
>
>..........
--
Robin Becker
More information about the reportlab-users
mailing list