author | rptlab |
Tue, 30 Apr 2013 14:28:14 +0100 | |
branch | py33 |
changeset 3723 | 99aa837b6703 |
parent 3721 | 0c93dd8ff567 |
child 3731 | b233dd0577ff |
permissions | -rw-r--r-- |
3617 | 1 |
#Copyright ReportLab Europe Ltd. 2000-2012 |
494 | 2 |
#see license.txt for license details |
2332 | 3 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/pdfgen/pdfimages.py |
4 |
__version__=''' $Id$ ''' |
|
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
5 |
__doc__=""" |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
6 |
Image functionality sliced out of canvas.py for generalization |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
7 |
""" |
562 | 8 |
|
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
9 |
import os |
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
10 |
import reportlab |
3359
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
11 |
from reportlab import rl_config |
491 | 12 |
from reportlab.pdfbase import pdfutils |
1683 | 13 |
from reportlab.pdfbase import pdfdoc |
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset
|
14 |
from reportlab.lib.utils import fp_str, getBytesIO, isStrType |
2046 | 15 |
from reportlab.lib.utils import import_zlib, haveImages |
3092 | 16 |
from reportlab.lib.boxstuff import aspectRatioFix |
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
1683
diff
changeset
|
17 |
|
562 | 18 |
|
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
19 |
class PDFImage: |
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
1683
diff
changeset
|
20 |
"""Wrapper around different "image sources". You can make images |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
1683
diff
changeset
|
21 |
from a PIL Image object, a filename (in which case it uses PIL), |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
1683
diff
changeset
|
22 |
an image we previously cached (optimisation, hardly used these |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
1683
diff
changeset
|
23 |
days) or a JPEG (which PDF supports natively).""" |
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2046
diff
changeset
|
24 |
|
2639
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
25 |
def __init__(self, image, x,y, width=None, height=None, caching=0): |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
26 |
self.image = image |
2639
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
27 |
self.x = x |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
28 |
self.y = y |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
29 |
self.width = width |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
30 |
self.height = height |
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
31 |
self.filename = None |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
32 |
self.imageCaching = caching |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
33 |
# the following facts need to be determined, |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
34 |
# whatever the source. Declare what they are |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
35 |
# here for clarity. |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
36 |
self.colorSpace = 'DeviceRGB' |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
37 |
self.bitsPerComponent = 8 |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
38 |
self.filters = [] |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
39 |
self.source = None # JPEG or PIL, set later |
2639
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
40 |
self.getImageData() |
1683 | 41 |
|
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
42 |
def jpg_imagedata(self): |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
43 |
#directly process JPEG files |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
44 |
#open file, needs some error handling!! |
2229 | 45 |
fp = open(self.image, 'rb') |
2854
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
46 |
try: |
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
47 |
result = self._jpg_imagedata(fp) |
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
48 |
finally: |
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
49 |
fp.close() |
2229 | 50 |
return result |
51 |
||
52 |
def _jpg_imagedata(self,imageFile): |
|
2854
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
53 |
info = pdfutils.readJPEGInfo(imageFile) |
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
54 |
self.source = 'JPEG' |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
55 |
imgwidth, imgheight = info[0], info[1] |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
56 |
if info[2] == 1: |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
57 |
colorSpace = 'DeviceGray' |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
58 |
elif info[2] == 3: |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
59 |
colorSpace = 'DeviceRGB' |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
60 |
else: #maybe should generate an error, is this right for CMYK? |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
61 |
colorSpace = 'DeviceCMYK' |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
62 |
imageFile.seek(0) #reset file pointer |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
63 |
imagedata = [] |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
64 |
#imagedata.append('BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /DCTDecode] ID' % (info[0], info[1], colorSpace)) |
3359
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
65 |
imagedata.append('BI /W %d /H %d /BPC 8 /CS /%s /F [%s/DCT] ID' % (imgwidth, imgheight, colorSpace, rl_config.useA85 and '/A85 ' or '')) |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
66 |
#write in blocks of (??) 60 characters per line to a list |
3359
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
67 |
data = imageFile.read() |
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
68 |
if rl_config.useA85: |
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
69 |
data = pdfutils._AsciiBase85Encode(data) |
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
70 |
pdfutils._chunker(data,imagedata) |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
71 |
imagedata.append('EI') |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
72 |
return (imagedata, imgwidth, imgheight) |
1683 | 73 |
|
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
74 |
def cache_imagedata(self): |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
75 |
image = self.image |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
76 |
if not pdfutils.cachedImageExists(image): |
673 | 77 |
zlib = import_zlib() |
78 |
if not zlib: return |
|
2046 | 79 |
if not haveImages: return |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
80 |
pdfutils.cacheImageFile(image) |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
81 |
|
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
82 |
#now we have one cached, slurp it in |
3359
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
83 |
cachedname = os.path.splitext(image)[0] + (rl_config.useA85 and '.a85' or '.bin') |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
84 |
imagedata = open(cachedname,'rb').readlines() |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
85 |
#trim off newlines... |
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset
|
86 |
imagedata = list(map(str.strip, imagedata)) |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
87 |
return imagedata |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
88 |
|
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
89 |
def PIL_imagedata(self): |
2229 | 90 |
image = self.image |
91 |
if image.format=='JPEG': |
|
92 |
fp=image.fp |
|
93 |
fp.seek(0) |
|
94 |
return self._jpg_imagedata(fp) |
|
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
95 |
self.source = 'PIL' |
888 | 96 |
zlib = import_zlib() |
673 | 97 |
if not zlib: return |
3100
2987b1bd86ac
pdfimages.py: CMYK support patch contributed by Ian Stevens <IStevens@globeandmail.com>
rgbecker
parents:
3092
diff
changeset
|
98 |
|
3103 | 99 |
# Use the colorSpace in the image |
3100
2987b1bd86ac
pdfimages.py: CMYK support patch contributed by Ian Stevens <IStevens@globeandmail.com>
rgbecker
parents:
3092
diff
changeset
|
100 |
if image.mode == 'CMYK': |
2987b1bd86ac
pdfimages.py: CMYK support patch contributed by Ian Stevens <IStevens@globeandmail.com>
rgbecker
parents:
3092
diff
changeset
|
101 |
myimage = image |
3103 | 102 |
colorSpace = 'DeviceCMYK' |
3100
2987b1bd86ac
pdfimages.py: CMYK support patch contributed by Ian Stevens <IStevens@globeandmail.com>
rgbecker
parents:
3092
diff
changeset
|
103 |
bpp = 4 |
2987b1bd86ac
pdfimages.py: CMYK support patch contributed by Ian Stevens <IStevens@globeandmail.com>
rgbecker
parents:
3092
diff
changeset
|
104 |
else: |
2987b1bd86ac
pdfimages.py: CMYK support patch contributed by Ian Stevens <IStevens@globeandmail.com>
rgbecker
parents:
3092
diff
changeset
|
105 |
myimage = image.convert('RGB') |
3103 | 106 |
colorSpace = 'RGB' |
3100
2987b1bd86ac
pdfimages.py: CMYK support patch contributed by Ian Stevens <IStevens@globeandmail.com>
rgbecker
parents:
3092
diff
changeset
|
107 |
bpp = 3 |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
108 |
imgwidth, imgheight = myimage.size |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
109 |
|
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
110 |
# this describes what is in the image itself |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
111 |
# *NB* according to the spec you can only use the short form in inline images |
3359
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
112 |
imagedata=['BI /W %d /H %d /BPC 8 /CS /%s /F [%s/Fl] ID' % (imgwidth, imgheight,colorSpace, rl_config.useA85 and '/A85 ' or '')] |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
113 |
|
3359
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
114 |
#use a flate filter and, optionally, Ascii Base 85 to compress |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
115 |
raw = myimage.tostring() |
3100
2987b1bd86ac
pdfimages.py: CMYK support patch contributed by Ian Stevens <IStevens@globeandmail.com>
rgbecker
parents:
3092
diff
changeset
|
116 |
assert len(raw) == imgwidth*imgheight*bpp, "Wrong amount of data for image" |
3359
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
117 |
data = zlib.compress(raw) #this bit is very fast... |
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
118 |
if rl_config.useA85: |
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
119 |
data = pdfutils._AsciiBase85Encode(data) #...sadly this may not be |
2229 | 120 |
#append in blocks of 60 characters |
3359
171fa15695a8
reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents:
3103
diff
changeset
|
121 |
pdfutils._chunker(data,imagedata) |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
122 |
imagedata.append('EI') |
1683 | 123 |
return (imagedata, imgwidth, imgheight) |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
124 |
|
2854
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
125 |
def non_jpg_imagedata(self,image): |
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
126 |
if not self.imageCaching: |
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
127 |
imagedata = pdfutils.cacheImageFile(image,returnInMemory=1) |
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
128 |
else: |
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
129 |
imagedata = self.cache_imagedata() |
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset
|
130 |
words = imagedata[1].split() |
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset
|
131 |
imgwidth = int(words[1]) |
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset
|
132 |
imgheight = int(words[3]) |
2854
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
133 |
return imagedata, imgwidth, imgheight |
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
134 |
|
2638 | 135 |
def getImageData(self,preserveAspectRatio=False): |
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
136 |
"Gets data, height, width - whatever type of image" |
1683 | 137 |
image = self.image |
138 |
||
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset
|
139 |
if isStrType(image): |
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
140 |
self.filename = image |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
141 |
if os.path.splitext(image)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']: |
2854
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
142 |
try: |
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
143 |
imagedata, imgwidth, imgheight = self.jpg_imagedata() |
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
144 |
except: |
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
145 |
imagedata, imgwidth, imgheight = self.non_jpg_imagedata(image) #try for normal kind of image |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
146 |
else: |
2854
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
147 |
imagedata, imgwidth, imgheight = self.non_jpg_imagedata(image) |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
148 |
else: |
2210 | 149 |
import sys |
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
1683
diff
changeset
|
150 |
if sys.platform[0:4] == 'java': |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
1683
diff
changeset
|
151 |
#jython, PIL not available |
2854
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
152 |
imagedata, imgwidth, imgheight = self.JAVA_imagedata() |
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
1683
diff
changeset
|
153 |
else: |
2854
4c3d4c68812c
pdfimages.py: attempt to allow for jpeg spoofing images
rgbecker
parents:
2680
diff
changeset
|
154 |
imagedata, imgwidth, imgheight = self.PIL_imagedata() |
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
155 |
self.imageData = imagedata |
2639
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
156 |
self.imgwidth = imgwidth |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
157 |
self.imgheight = imgheight |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
158 |
self.width = self.width or imgwidth |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
159 |
self.height = self.height or imgheight |
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
160 |
|
3092 | 161 |
def drawInlineImage(self, canvas, preserveAspectRatio=False,anchor='sw'): |
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
162 |
"""Draw an Image into the specified rectangle. If width and |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
163 |
height are omitted, they are calculated from the image size. |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
164 |
Also allow file names as well as images. This allows a |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
165 |
caching mechanism""" |
2639
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
166 |
width = self.width |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
167 |
height = self.height |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
168 |
if width<1e-6 or height<1e-6: return False |
2680 | 169 |
x,y,self.width,self.height, scaled = aspectRatioFix(preserveAspectRatio,anchor,self.x,self.y,width,height,self.imgwidth,self.imgheight) |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
170 |
# this says where and how big to draw it |
2638 | 171 |
if not canvas.bottomup: y = y+height |
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
172 |
canvas._code.append('q %s 0 0 %s cm' % (fp_str(self.width), fp_str(self.height, x, y))) |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
173 |
# self._code.extend(imagedata) if >=python-1.5.2 |
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
174 |
for line in self.imageData: |
490
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
175 |
canvas._code.append(line) |
03c081d12447
image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff
changeset
|
176 |
canvas._code.append('Q') |
2550
51b0c2a321ee
reportlab: fix test_graphics_charts and eliminate 0 x 0 image bug in Acrobat 7.0
rgbecker
parents:
2332
diff
changeset
|
177 |
return True |
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
178 |
|
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
179 |
def format(self, document): |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
180 |
"""Allow it to be used within pdfdoc framework. This only |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
181 |
defines how it is stored, not how it is drawn later.""" |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
182 |
|
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
183 |
dict = pdfdoc.PDFDictionary() |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
184 |
dict['Type'] = '/XObject' |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
185 |
dict['Subtype'] = '/Image' |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
186 |
dict['Width'] = self.width |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
187 |
dict['Height'] = self.height |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
188 |
dict['BitsPerComponent'] = 8 |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
189 |
dict['ColorSpace'] = pdfdoc.PDFName(self.colorSpace) |
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset
|
190 |
content = '\n'.join(self.imageData[3:-1]) + '\n' |
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
191 |
strm = pdfdoc.PDFStream(dictionary=dict, content=content) |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
192 |
return strm.format(document) |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
193 |
|
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
194 |
if __name__=='__main__': |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
195 |
srcfile = os.path.join( |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
196 |
os.path.dirname(reportlab.__file__), |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
197 |
'test', |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
198 |
'pythonpowered.gif' |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
199 |
) |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
200 |
assert os.path.isfile(srcfile), 'image not found' |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
201 |
pdfdoc.LongFormat = 1 |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
202 |
img = PDFImage(srcfile, 100, 100) |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
203 |
import pprint |
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
888
diff
changeset
|
204 |
doc = pdfdoc.PDFDocument() |
3721 | 205 |
print('source=',img.source) |
206 |
print(img.format(doc)) |