extension.erp5.ERP5ZoomifyImage.py 20.8 KB
Newer Older
1 2
##############################################################################
# Copyright (C) 2005  Adam Smith  asmith@agile-software.com
3
#
4 5 6 7
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
8
#
9 10 11 12
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
13
#
14 15 16 17 18 19 20 21 22 23 24 25
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##############################################################################

import os, sys, shutil, tempfile
from cStringIO import StringIO
from zLOG import LOG,ERROR,INFO,WARNING
from OFS.Image import File, Image
import os, transaction
from AccessControl import getSecurityManager, ClassSecurityInfo
from Globals import package_home
26
import PIL.Image as PIL_Image
27 28 29
import thread
import random
import base64
30
from OFS.Folder import Folder
31 32 33 34 35 36 37 38 39 40 41 42

class ZoomifyBase:

  _v_imageFilename = ''
  format = ''
  originalWidth = 0
  originalHeight = 0
  _v_scaleInfo = []
  numberOfTiles = 0
  _v_tileGroupMappings = {}
  qualitySetting = 80
  tileSize = 256
43
  my_file = StringIO()
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

  def openImage(self):
    """ load the image data """

    pass
    return

  def getImageInfo(self):
    """ """

    image = self.openImage()
    self.format = image.format
    self.originalWidth, self.originalHeight = image.size
    image = None
    width, height = (self.originalWidth, self.originalHeight)
    self._v_scaleInfo = [(width, height)]
    while (width > self.tileSize) or (height > self.tileSize):
      width, height = (width / 2, height / 2)
      self._v_scaleInfo.insert(0, (width, height))
    totalTiles=0
    tier, rows, columns = (0,0,0)
    for tierInfo in self._v_scaleInfo:
      rows = height/self.tileSize
      if height % self.tileSize > 0:
        rows +=1
      columns = width/self.tileSize
      if width%self.tileSize > 0:
        columns += 1
      totalTiles += rows * columns
      tier += 1

  def getImageMetadata(self):
    """ given an image name, load it and extract metadata """

    image = self.openImage()
    self.format = image.format
    self.originalWidth, self.originalHeight = image.size
    image = None
    # get scaling information
    width, height = (self.originalWidth, self.originalHeight)
    self._v_scaleInfo = [(width, height)]
    while (width > self.tileSize) or (height > self.tileSize):
      width, height = (width / 2, height / 2)
      self._v_scaleInfo.insert(0, (width, height))
    # tile and tile group information
    self.preProcess()
    return

  def createDataContainer(self, imageName):
    """ create a container for tile groups and tile metadata """

    pass
    return

  def getAssignedTileContainerName(self, tileFileName=None):
    """ return the name of the tile group for the indicated tile """
    if tileFileName:
      if hasattr(self, '_v_tileGroupMappings') and self._v_tileGroupMappings:
        containerName = self._v_tileGroupMappings.get(tileFileName, None)
        if containerName:
          return containerName
    x = self.getNewTileContainerName()
    return x

  def getNewTileContainerName(self, tileGroupNumber=0):
    """ return the name of the next tile group container """

    return 'TileGroup' + str(tileGroupNumber)

  def createTileContainer(self, tileContainerName=None):
    """ create a container for the next group of tiles within the data container """

    pass
    return

  def getTileFileName(self, scaleNumber, columnNumber, rowNumber):
    """ get the name of the file the tile will be saved as """

    return '%s-%s-%s.jpg' % (str(scaleNumber), str(columnNumber), str(rowNumber))

  def getFileReference(self, scaleNumber, columnNumber, rowNumber):
    """ get the full path of the file the tile will be saved as """

    pass
    return

  def getNumberOfTiles(self):
131 132
    """ get the number of tiles generated
        Should this be implemented as a safeguard, or just use the count of
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        tiles gotten from processing? (This would make subclassing a little
        easier.) """

    return self.numberOfTiles

  def getXMLOutput(self):
    """ create xml metadata about the tiles """

    numberOfTiles = self.getNumberOfTiles()
    xmlOutput = '<IMAGE_PROPERTIES WIDTH="%s" HEIGHT="%s" NUMTILES="%s" NUMIMAGES="1" VERSION="1.8" TILESIZE="%s" />'
    xmlOutput = xmlOutput % (str(self.originalWidth),
            str(self.originalHeight), str(numberOfTiles), str(self.tileSize))
    return xmlOutput

  def saveXMLOutput(self):
    """ save xml metadata about the tiles """

    pass
    return

  def saveTile(self, image, scaleNumber, column, row):
    """ save the cropped region """

    pass
    return

  def processImage(self):
    """ starting with the original image, start processing each row """
    tier=(len(self._v_scaleInfo) -1)
    row = 0
    ul_y, lr_y = (0,0)
    root, ext = os.path.splitext(self._v_imageFilename)
    if not root:
      root = self._v_imageFilename
    ext = '.jpg'
    image = self.openImage()
    while row * self.tileSize < self.originalHeight:
      ul_y = row * self.tileSize
      if (ul_y + self.tileSize) < self.originalHeight:
        lr_y = ul_y + self.tileSize
      else:
        lr_y = self.originalHeight
      print "Going to open image"
      imageRow = image.crop([0, ul_y, self.originalWidth, lr_y])
      saveFilename = root + str(tier) + '-' + str(row) +  ext
      if imageRow.mode != 'RGB':
        imageRow = imageRow.convert('RGB')
      imageRow.save(os.path.join(tempfile.gettempdir(), saveFilename),
                                                        'JPEG', quality=100)
      print "os path exist : %r" % os.path.exists(os.path.join(
                                        tempfile.gettempdir(), saveFilename))
184
      if os.path.exists(os.path.join(tempfile.gettempdir(), saveFilename)):
185 186 187 188 189 190 191 192 193 194 195
        self.processRowImage(tier=tier, row=row)
      row += 1

  def processRowImage(self, tier=0, row=0):
    """ for an image, create and save tiles """

    print '*** processing tier: ' + str(tier) + ' row: ' + str(row)
    tierWidth, tierHeight = self._v_scaleInfo[tier]
    rowsForTier = tierHeight/self.tileSize
    if tierHeight % self.tileSize > 0:
      rowsForTier +=1
196
    root, ext = os.path.splitext(self._v_imageFilename)
197 198 199 200 201 202 203
    if not root:
      root = self._v_imageFilename
    ext = '.jpg'
    imageRow = None
    if tier == (len(self._v_scaleInfo) -1):
      firstTierRowFile = root + str(tier) + '-' + str(row) + ext
      if os.path.exists(os.path.join(tempfile.gettempdir(),firstTierRowFile)):
204
        imageRow = PIL_Image.open(os.path.join(tempfile.gettempdir(),
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
                                                    firstTierRowFile))
    else:
      # create this row from previous tier's rows
      imageRow = PIL_Image.new('RGB', (tierWidth, self.tileSize))
      firstRowFile = root + str(tier+1) + '-' + str(row + row) + ext
      firstRowWidth, firstRowHeight = (0,0)
      secondRowWidth, secondRowHeight = (0,0)
      if os.path.exists(os.path.join(tempfile.gettempdir(),firstRowFile)):
        firstRowImage = PIL_Image.open(os.path.join(tempfile.gettempdir(),
                                                         firstRowFile))
        firstRowWidth, firstRowHeight = firstRowImage.size
        imageRow.paste(firstRowImage, (0,0))
        os.remove(os.path.join(tempfile.gettempdir(), firstRowFile))
      secondRowFile = root + str(tier+1) + '-' + str(row + row +1) + ext
      # there may not be a second row at the bottom of the image...
      if os.path.exists(os.path.join(tempfile.gettempdir(), secondRowFile)):
        secondRowImage = PIL_Image.open(os.path.join(tempfile.gettempdir(),
                                                        secondRowFile))
        secondRowWidth, secondRowHeight = secondRowImage.size
        imageRow.paste(secondRowImage, (0, firstRowHeight))
        os.remove(os.path.join(tempfile.gettempdir(), secondRowFile))
      # the last row may be less than self.tileSize...
      if (firstRowHeight + secondRowHeight) < (self.tileSize*2):
        imageRow = imageRow.crop((0, 0, tierWidth,
                               (firstRowHeight+secondRowHeight)))
    if imageRow:
      # cycle through columns, then rows
      column = 0
      imageWidth, imageHeight = imageRow.size
      ul_x, ul_y, lr_x, lr_y = (0,0,0,0) # final crop coordinates
      while not ((lr_x == imageWidth) and (lr_y == imageHeight)):
        # set lower right cropping point
        if (ul_x + self.tileSize) < imageWidth:
          lr_x = ul_x + self.tileSize
        else:
          lr_x = imageWidth
        if (ul_y + self.tileSize) < imageHeight:
          lr_y = ul_y + self.tileSize
        else:
          lr_y = imageHeight
        self.saveTile(imageRow.crop([ul_x, ul_y, lr_x, lr_y]), tier,
                                                        column, row)
        self.numberOfTiles += 1
        # set upper left cropping point
        if (lr_x == imageWidth):
          ul_x=0
          ul_y = lr_y
          column = 0
        else:
          ul_x = lr_x
          column += 1
      if tier > 0:
        # a bug was discovered when a row was exactly 1 pixel in height
        # this extra checking accounts for that
        if imageHeight > 1:
          tempImage = imageRow.resize((imageWidth/2, imageHeight/2),
                                                     PIL_Image.ANTIALIAS)
          tempImage.save(os.path.join(tempfile.gettempdir(), root + str(tier)
                                       + '-' + str(row) + ext))
          tempImage = None
      rowImage = None
      if tier > 0:
        if row % 2 != 0:
          self.processRowImage(tier=(tier-1), row=((row-1)/2))
        elif row == rowsForTier-1:
          self.processRowImage(tier=(tier-1), row=(row/2))

  def ZoomifyProcess(self, imageNames):
    """ the method the client calls to generate zoomify metadata """

    pass
    return

  def preProcess(self):
    """ plan for the arrangement of the tile groups """

    tier = 0
    tileGroupNumber = 0
    numberOfTiles = 0
    for width, height in self._v_scaleInfo:
      #cycle through columns, then rows
      row, column = (0,0)
      ul_x, ul_y, lr_x, lr_y = (0,0,0,0)  #final crop coordinates
      while not ((lr_x == width) and (lr_y == height)):
        tileFileName = self.getTileFileName(tier, column, row)
        tileContainerName = self.getNewTileContainerName(
                                        tileGroupNumber=tileGroupNumber)
        if numberOfTiles ==0:
          self.createTileContainer(tileContainerName=tileContainerName)
        elif (numberOfTiles % self.tileSize) == 0:
          tileGroupNumber += 1
          tileContainerName = self.getNewTileContainerName(
                                       tileGroupNumber=tileGroupNumber)
          self.createTileContainer(tileContainerName=tileContainerName)
        self._v_tileGroupMappings[tileFileName] = tileContainerName
        numberOfTiles += 1
        # for the next tile, set lower right cropping point
        if (ul_x + self.tileSize) < width:
          lr_x = ul_x + self.tileSize
        else:
          lr_x = width
        if (ul_y + self.tileSize) < height:
          lr_y = ul_y + self.tileSize
        else:
          lr_y = height
        # for the next tile, set upper left cropping point
        if (lr_x == width):
          ul_x=0
          ul_y = lr_y
          column = 0
          row += 1
        else:
          ul_x = lr_x
          column += 1
      tier += 1


class ZoomifyZopeProcessor(ZoomifyBase):
  """ basic functionality to provide Zoomify functionality inside Zope """

  _v_imageObject = None
  _v_saveFolderObject = None
  _v_transactionCount = 0
  security = ClassSecurityInfo()
  security.declareObjectProtected('Add Documents, Images, and Files')

  def openImage(self):
    """ load the image data """

    return PIL_Image.open(self._v_imageObject.name)

  def createDefaultViewer(self):
    """ add the default Zoomify viewer to the Zoomify metadata """

    # also, add the default zoomifyViewer here if a zoomify viewer is acquirable
340
    # (could this be done a better way, like using the 'web methods'
341 342 343 344 345 346 347
    # approach that points to ZMI screens that are DTML or ZPT files
    # in the product package)?
    if not hasattr(self._v_saveFolderObject, 'default_ZoomifyViewer'):
      defaultViewerPath = os.path.join(package_home(globals()), 'www',
                                                  'zoomifyViewer.swf')
      if os.path.isfile(defaultViewerPath):
        fileContent = open(defaultViewerPath).read()
348 349
        self._v_saveFolderObject._setObject('default_ZoomifyViewer',
                               File('default_ZoomifyViewer', '', fileContent,
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
                                    'application/x-shockwave-flash', ''))
    transaction.savepoint()
    return

  def createDataContainer(self):
    """ create a folder that contains all the tiles of the image """

    self._v_saveToLocation = str(self._v_imageObject.getId()) + '_data'
    parent = self._v_imageObject.aq_parent
    if hasattr(parent, self._v_saveToLocation):
      # allow for tiles to be updated from a changed image
      parent._delObject(self._v_saveToLocation)
    if not hasattr(parent, self._v_saveToLocation):
      newFolder = Folder()
      newFolder.id = self._v_saveToLocation
      parent._setObject(self._v_saveToLocation, newFolder)
    self._v_saveFolderObject = parent[self._v_saveToLocation]
    transaction.savepoint()
    return

  def createTileContainer(self, tileContainerName=None):
371
    """ create a container for the next group of tiles within the data container """
372 373 374 375 376 377 378 379 380 381 382 383

    if hasattr(self._v_saveFolderObject, tileContainerName):
      # allow for tiles to be updated from a changed image
      self._v_saveFolderObject._delObject(tileContainerName)
    if not hasattr(self._v_saveFolderObject, tileContainerName):
      newFolder = Folder()
      newFolder.id = tileContainerName
      self._v_saveFolderObject._setObject(tileContainerName, newFolder)
    transaction.savepoint()
    return

  def getNumberOfTiles(self):
384 385
    """ get the number of tiles generated
        Should this be implemented as a safeguard, or just use the count of
386 387 388 389 390 391 392 393 394 395
        tiles gotten from processing? (This would make subclassing a little
        easier.) """
    return self.numberOfTiles

  def saveXMLOutput(self):
    """ save xml metadata about the tiles """

    if hasattr(self._v_saveFolderObject, 'ImageProperties.xml'):
      # allow file to be updated from a changed image, regenerated tiles
      self._v_saveFolderObject._delObject('ImageProperties.xml')
396 397
    self._v_saveFolderObject._setObject('ImageProperties.xml',
                        File('ImageProperties.xml', '', self.getXMLOutput(),
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
                             'text/xml', ''))
    transaction.savepoint()
    return

  def saveTile(self, image, scaleNumber, column, row):
    """ save the cropped region """

    w,h = image.size
    if w != 0 and h != 0:
      tileFileName = self.getTileFileName(scaleNumber, column, row)
      tileContainerName = self.getAssignedTileContainerName(
                                          tileFileName=tileFileName)
      tileImageData = StringIO()
      image.save(tileImageData, 'JPEG', quality=self.qualitySetting)
      tileImageData.seek(0)
      if hasattr(self._v_saveFolderObject, tileContainerName):
        tileFolder = getattr(self._v_saveFolderObject, tileContainerName)
        # if an image of this name already exists, delete and replace it.
        if hasattr(tileFolder, tileFileName):
          tileFolder._delObject(tileFileName)
        # finally, save the image data as a Zope Image object
419
        tileFolder._setObject(tileFileName, Image(tileFileName, '',
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
                                              '', 'image/jpeg', ''))
        tileFolder._getOb(tileFileName).manage_upload(tileImageData)
      self._v_transactionCount += 1
      if self._v_transactionCount % 10 == 0:
        transaction.savepoint()
    return

  def encrypto(self):
    if self.transformed:
      # def encrypto(self,message='Attack at dawn'):
      #   """encrypto each thing"""
      #   key ='abcdefghijklmnop'
      #   iv = Random.new().read(AES.block_size)
      #   EncodeIV = lambda : base64.b64encode(iv)
      #   EncodeAES = lambda c, s: base64.b64encode(c.encrypt(message))
      #   IVcoded  = EncodeIV()
      #   cipher = AES.new(key,AES.MODE_CFB,iv)
      #   msg  = EncodeAES(cipher,key)
438
      #   return msg
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
      pass
    else:
      pass
    return

  def _process(self):
    """ the actual zoomify processing workflow """

    self.createDataContainer()
    self.createDefaultViewer()
    self.openImage()
    self.getImageMetadata()
    self.processImage()
    self.saveTransformedFile()
    self.saveXMLOutput()
    self.encrypto()
    return

  def saveTransformedFile(self):
    pass
    return

  def _ZoomifyProcess(self):
    """ factored out ZODB connection handling """

    #import Zope
    #app = Zope.app()
466
    #transaction.manager.begin()
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
    self._process()
    #app._p_jar.close()
    #del app
    return

  security.declareProtected('Add Documents, Images, and Files', 'ZoomifyProcess')
  def ZoomifyProcess(self, id, imageObject=None):
    """ factored out threading of process (removed for now) """
    if imageObject:
      self._v_imageObject = imageObject
      self._v_imageFilename = id
      self._ZoomifyProcess()
    return

class ERP5ZoomifyZopeProcessor(ZoomifyZopeProcessor):

  def __init__(self, document,transformed=None):
     self.document = document
     self.transformed = transformed
486
     self.count = 0
487 488 489 490

  def createTileContainer(self, tileContainerName=None):
    """ create each TileGroup """

491 492
    self.document.newContent(portal_type="Image Tile Group",
                   title=tileContainerName, id=tileContainerName,
493
                   filename=tileContainerName)
494
    return
495 496 497 498 499 500 501 502 503 504 505 506 507 508

  def createDefaultViewer(self):
    """ add the default Zoomify viewer to the Zoomify metadata """
    pass
    return

  def createDataContainer(self, imageName="None"):
    """Creates nothing coz we are already in the container"""
    pass
    return

  def _updateTransformedFile(self,tile_group_id,tile_title):
    """create and save the transform file"""
    num = random.choice([0,1])
509
    while num >= 0:
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
      algorithm = random.choice(['sepia','brightness','noise','lighten',
                                  'posterize','edge','none'])
      if algorithm == 'lighten':
        param1 = random.choice([-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0.1,0.2,
                                 0.3,0.4,0.5,0.6])
        param2 = 0
      elif algorithm == 'posterize':
        param1 = random.choice([4,5,6,7,8,9,10,11,12,13,14,15,16,17,
                                18,19,20,21])
        param2 = 0
      elif algorithm == 'brightness':
        param1 = random.choice([-80,-60,-40,-20,20,40,60,80])
        param2 = random.choice([-0.3,-0.2,-0.1,0,0.1,0.5,0.9])
      else:
        param1 = 0
        param2 = 0
526
      my_text = '%s %s %s %s %s %s \n' %(tile_group_id, tile_title,
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
                                    algorithm, param1, param2, num)
      self.my_file.write(my_text)
      num = num - 1


  def saveTile(self, image, scaleNumber, column,row):
    """save the cropped region"""
    tileFileName = self.getTileFileName(scaleNumber, column, row)
    tileContainerName = self.getAssignedTileContainerName(
                                       tileFileName=tileFileName)
    namesplit = tileFileName.split('.')
    w,h = image.size
    if w != 0 and h !=0:
      tile_group_id = self.getAssignedTileContainerName()
      tile_group=self.document[tile_group_id]
      tileImageData= StringIO()
      image.save(tileImageData, 'JPEG', quality=self.qualitySetting)
      tileImageData.seek(0)
      if tile_group is None:
        raise AttributeError('unable to fine tile group %r' % tile_group_id)
      w = tile_group.newContent(portal_type='Image', title=namesplit[0],
                 id=namesplit[0], file=tileImageData, filename=namesplit[0])
      if self.transformed:
        self._updateTransformedFile(tile_group_id, namesplit[0])
    return

  def saveXMLOutput(self):
    """save the xml file"""
    my_string = StringIO()
    my_string.write(self.getXMLOutput())
    my_string.seek(0)
    self.document.newContent(portal_type='Embedded File',
                             id='ImageProperties.xml', file=my_string,
                             filename='ImageProperties.xml')
    return

  def saveTransformedFile(self):
    """add in Zope the transform file """
    if self.transformed:
      self.my_file.seek(0)
      self.document.newContent(portal_type='Embedded File',
                             id='TransformFile.txt', file=self.my_file,
                             filename='TransformFile.txt')
    return


def getERP5ZoomifyProcessor(document,transformed=False):
  return ERP5ZoomifyZopeProcessor(document,transformed)