src/reportlab/pdfgen/pdfimages.py
author rptlab
Tue, 30 Apr 2013 14:28:14 +0100
branchpy33
changeset 3723 99aa837b6703
parent 3721 0c93dd8ff567
child 3731 b233dd0577ff
permissions -rw-r--r--
second stage of port to Python 3.3; working hello world
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3617
ae5744e97c42 reportlab: copyright date changes
robin
parents: 3359
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2012
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 491
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2229
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/pdfgen/pdfimages.py
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2229
diff changeset
     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
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 556
diff changeset
     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
6bf7da76b8fd Fix zlib import bug
rgbecker
parents: 490
diff changeset
    12
from reportlab.pdfbase import pdfutils
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1582
diff changeset
    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
4843e1709d5b hack to ImageReader
rgbecker
parents: 2044
diff changeset
    15
from reportlab.lib.utils import import_zlib, haveImages
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2993
diff changeset
    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
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 556
diff changeset
    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
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1582
diff changeset
    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
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2223
diff changeset
    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
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2223
diff changeset
    50
        return result
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2223
diff changeset
    51
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2223
diff changeset
    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
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1582
diff changeset
    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
d244d448eef4 Imporved ImportError handling
rgbecker
parents: 562
diff changeset
    77
            zlib = import_zlib()
d244d448eef4 Imporved ImportError handling
rgbecker
parents: 562
diff changeset
    78
            if not zlib: return
2046
4843e1709d5b hack to ImageReader
rgbecker
parents: 2044
diff changeset
    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
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2223
diff changeset
    90
        image = self.image
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2223
diff changeset
    91
        if image.format=='JPEG':
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2223
diff changeset
    92
            fp=image.fp
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2223
diff changeset
    93
            fp.seek(0)
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2223
diff changeset
    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
a9d1a568f4df Fixed typo zlib (not lib)
rgbecker
parents: 760
diff changeset
    96
        zlib = import_zlib()
673
d244d448eef4 Imporved ImportError handling
rgbecker
parents: 562
diff changeset
    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
0acdc9f2161c pdfimages.py: firm up the CMYK patch
rgbecker
parents: 3100
diff changeset
    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
0acdc9f2161c pdfimages.py: firm up the CMYK patch
rgbecker
parents: 3100
diff changeset
   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
0acdc9f2161c pdfimages.py: firm up the CMYK patch
rgbecker
parents: 3100
diff changeset
   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
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2223
diff changeset
   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
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1582
diff changeset
   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
ee3e8ad6c3cb reportlab: added first stage boxstuff for images
rgbecker
parents: 2550
diff changeset
   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
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1582
diff changeset
   137
        image = self.image
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1582
diff changeset
   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
0e0dbaae2937 Fix missing import found by Ulrich Schreiner
rgbecker
parents: 2200
diff changeset
   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
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2993
diff changeset
   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
4a251726da2c fixed drawInlineImage
andy
parents: 2639
diff changeset
   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
ee3e8ad6c3cb reportlab: added first stage boxstuff for images
rgbecker
parents: 2550
diff changeset
   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
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   205
    print('source=',img.source)
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   206
    print(img.format(doc))