|
@@ -1,29 +1,21 @@
|
|
|
-import io, os, sys
|
|
|
-import errno
|
|
|
-from skimage import img_as_ubyte, img_as_float
|
|
|
-from skimage.io import Image, imsave, use_plugin, imshow
|
|
|
+import io
|
|
|
+from skimage import img_as_ubyte
|
|
|
from skimage.transform import resize
|
|
|
|
|
|
-from PIL import Image
|
|
|
+from Pillow import Image
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
from multiprocessing import Process, cpu_count, Queue
|
|
|
|
|
|
-from array import array
|
|
|
import numpy as np
|
|
|
import math
|
|
|
-import argparse
|
|
|
-import json
|
|
|
import copy
|
|
|
|
|
|
import logging
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
-import pdb
|
|
|
-
|
|
|
-#use_plugin('freeimage')
|
|
|
|
|
|
class Sprite(object):
|
|
|
imagesAsArrays = None
|
|
@@ -40,6 +32,7 @@ class Sprite(object):
|
|
|
numberOfSprites = 0
|
|
|
slicesPerSprite = 0
|
|
|
|
|
|
+
|
|
|
def __initializeSprite(imageFrom, imageTo, sliceShape, dtype):
|
|
|
|
|
|
imagesPerRow, totalRows, imagesToProcess, numberOfSprites, slicesPerSprite, zDownScalingFactor = __calculateRowsAndColsOfSpriteMap(imageFrom, imageTo)
|
|
@@ -49,10 +42,9 @@ def __initializeSprite(imageFrom, imageTo, sliceShape, dtype):
|
|
|
outputImageArrays = []
|
|
|
for counter in range(0, numberOfSprites):
|
|
|
outputImageArray = np.empty((sliceShape[0] * totalRows, sliceShape[1] * imagesPerRow), dtype=dtype)
|
|
|
- outputImageArray.setflags(write = True)
|
|
|
+ outputImageArray.setflags(write=True)
|
|
|
outputImageArrays.append(outputImageArray)
|
|
|
|
|
|
-
|
|
|
sprite = Sprite()
|
|
|
sprite.imagesToProcess = imagesToProcess
|
|
|
sprite.imagesPerRow = imagesPerRow
|
|
@@ -63,12 +55,13 @@ def __initializeSprite(imageFrom, imageTo, sliceShape, dtype):
|
|
|
|
|
|
return sprite
|
|
|
|
|
|
+
|
|
|
def __calculateRowsAndColsOfSpriteMap(imageFrom, imageTo):
|
|
|
imagesToProcess = imageTo - imageFrom + 1
|
|
|
slicesPerSprite = imagesToProcess
|
|
|
|
|
|
# upperbound are 25 ^ 2 slices per map
|
|
|
- maxSlicesPerMap = settings.MAX_NUMBER_SLICES_PER_MAP;
|
|
|
+ maxSlicesPerMap = settings.MAX_NUMBER_SLICES_PER_MAP
|
|
|
|
|
|
zDownScalingActivated = settings.Z_DOWNSCALING_ACTIVATED
|
|
|
zDownScalingFactor = 1
|
|
@@ -80,7 +73,6 @@ def __calculateRowsAndColsOfSpriteMap(imageFrom, imageTo):
|
|
|
numberOfSprites = 1
|
|
|
zDownScalingFactor = int(math.ceil(imagesToProcess / maxSlicesPerMap))
|
|
|
|
|
|
-
|
|
|
imagesToProcess = int(math.floor(imagesToProcess / float(zDownScalingFactor)))
|
|
|
|
|
|
# stretch the whole number of images on to the sprites
|
|
@@ -99,6 +91,7 @@ def __calculateRowsAndColsOfSpriteMap(imageFrom, imageTo):
|
|
|
|
|
|
return (imagesPerRow, totalRows, imagesToProcess, numberOfSprites, slicesPerSprite, zDownScalingFactor)
|
|
|
|
|
|
+
|
|
|
def __copyImageToSprite(sliceImage, destArray, sprite):
|
|
|
numY, numX = sliceImage.shape
|
|
|
|
|
@@ -109,6 +102,7 @@ def __copyImageToSprite(sliceImage, destArray, sprite):
|
|
|
|
|
|
return destArray
|
|
|
|
|
|
+
|
|
|
def processFrames(frames, frameFrom, frameTo, imageFrom, imageTo, imgFormat, destSize):
|
|
|
|
|
|
if len(frames.shape) != 4:
|
|
@@ -117,10 +111,6 @@ def processFrames(frames, frameFrom, frameTo, imageFrom, imageTo, imgFormat, des
|
|
|
sprites = []
|
|
|
imagesPerRow, totalRows, imagesToProcess, numberOfSprites, slicesPerSprite, zDownScalingFactor = __calculateRowsAndColsOfSpriteMap(imageFrom, imageTo)
|
|
|
|
|
|
- frameShapeLength = len(frames.shape)
|
|
|
- indexX = frameShapeLength - 1
|
|
|
- indexY = indexX - 1
|
|
|
-
|
|
|
upperBoundPixels = 16384
|
|
|
|
|
|
if frames.shape[3] * imagesPerRow > upperBoundPixels:
|
|
@@ -132,7 +122,7 @@ def processFrames(frames, frameFrom, frameTo, imageFrom, imageTo, imgFormat, des
|
|
|
logger.debug('sliceMapCreator resizing slices to (%d, %d)' % newImageShape)
|
|
|
for frameCounter in range(0, frames.shape[0]):
|
|
|
imageCounter = 0
|
|
|
- cpus=cpu_count()
|
|
|
+ cpus = cpu_count()
|
|
|
imagesPerCpu = int(math.ceil(frames.shape[1] / float(cpus)))
|
|
|
processes = []
|
|
|
queues = [Queue() for i in range(0, cpus)]
|
|
@@ -153,7 +143,6 @@ def processFrames(frames, frameFrom, frameTo, imageFrom, imageTo, imgFormat, des
|
|
|
for process in processes:
|
|
|
process.start()
|
|
|
|
|
|
-
|
|
|
resultFrames = []
|
|
|
logger.debug('sliceMapCreator empty queues')
|
|
|
for queue in queues:
|
|
@@ -169,11 +158,9 @@ def processFrames(frames, frameFrom, frameTo, imageFrom, imageTo, imgFormat, des
|
|
|
newFrames[frameCounter][imageCounter] = image
|
|
|
imageCounter += 1
|
|
|
|
|
|
-
|
|
|
for process in processes:
|
|
|
process.join()
|
|
|
|
|
|
-
|
|
|
'''
|
|
|
old sequential implementation
|
|
|
|
|
@@ -205,11 +192,12 @@ def processFrames(frames, frameFrom, frameTo, imageFrom, imageTo, imgFormat, des
|
|
|
|
|
|
return __resizeSprites(sprites, destSize, imgFormat)
|
|
|
|
|
|
+
|
|
|
def processFrame(frame, frameNumber, imageFrom, imageTo, zDownScalingFactor):
|
|
|
logger.debug('sliceMapCreator frameNumber %d' % frameNumber)
|
|
|
|
|
|
if len(frame) is 0:
|
|
|
- raise ValueError('could not find any frame')
|
|
|
+ raise ValueError('could not find any frame')
|
|
|
|
|
|
firstSlice = frame[0]
|
|
|
|
|
@@ -245,6 +233,7 @@ def processFrame(frame, frameNumber, imageFrom, imageTo, zDownScalingFactor):
|
|
|
|
|
|
return sprite
|
|
|
|
|
|
+
|
|
|
def generateInfo(sprites, frameFrom, frameTo, imageFrom, imageTo):
|
|
|
logger.debug('sliceMapCreator generating and saving .js info file')
|
|
|
|
|
@@ -263,14 +252,13 @@ def generateInfo(sprites, frameFrom, frameTo, imageFrom, imageTo):
|
|
|
infoObject['frameFrom'] = frameFrom
|
|
|
infoObject['frameTo'] = frameTo
|
|
|
|
|
|
-
|
|
|
return infoObject
|
|
|
|
|
|
+
|
|
|
def __resizeSprites(sprites, destSize, imgFormat):
|
|
|
returnSprites = {}
|
|
|
exponentMin = 9
|
|
|
exponent = exponentMin
|
|
|
- destWidth = 0
|
|
|
|
|
|
firstSprite = sprites[0]
|
|
|
# hardcoded value, 2^12
|
|
@@ -293,7 +281,6 @@ def __resizeSprites(sprites, destSize, imgFormat):
|
|
|
if pot > widthUpperBound:
|
|
|
exponentFound = True
|
|
|
exponent = exponent
|
|
|
- destWidth = int(math.pow(2, exponent))
|
|
|
else:
|
|
|
exponent += 1
|
|
|
|
|
@@ -321,7 +308,7 @@ def __resizeSprites(sprites, destSize, imgFormat):
|
|
|
pilImage = pilImage.convert('RGBA')
|
|
|
try:
|
|
|
pilImage.save(buff, format='WEBP', quality=quality)
|
|
|
- except OSError as e:
|
|
|
+ except OSError:
|
|
|
# due to a bug in the library of webp, the webp encoder fails for high file sizes
|
|
|
# --> if this is a big 'destSize', we have to try it again with lower quality levels
|
|
|
logger.debug('sliceMapCreator failed to store webp with size of %dpx and quality value of %d' % (destSize, quality))
|
|
@@ -338,13 +325,12 @@ def __resizeSprites(sprites, destSize, imgFormat):
|
|
|
pilImage.save(buff, format='JPEG', quality=quality, progressive=True)
|
|
|
contenttype += 'jpeg'
|
|
|
|
|
|
-
|
|
|
logger.debug('sliceMapCreator image with res %d has size of: %d' % (destSize, buff.tell()))
|
|
|
|
|
|
- buff.seek(0,0)
|
|
|
+ buff.seek(0, 0)
|
|
|
buffers.append(buff)
|
|
|
|
|
|
- ### end for each array
|
|
|
+ # end for each array
|
|
|
|
|
|
spriteToAdd = copy.deepcopy(generatorSprite)
|
|
|
spriteToAdd.buffers = buffers
|
|
@@ -361,6 +347,7 @@ def __resizeSprites(sprites, destSize, imgFormat):
|
|
|
|
|
|
return returnSprites
|
|
|
|
|
|
+
|
|
|
def calculateXYDimensions(frame):
|
|
|
if len(frame.shape) != 3:
|
|
|
raise ValueError('frame must have the dimension 3')
|
|
@@ -373,17 +360,18 @@ def calculateXYDimensions(frame):
|
|
|
pixel_offset = [0, 0]
|
|
|
|
|
|
if new_width < max_size:
|
|
|
- pixel_offset[0] = math.ceil((max_size - new_width) / 2)
|
|
|
- new_width = max_size
|
|
|
+ pixel_offset[0] = math.ceil((max_size - new_width) / 2)
|
|
|
+ new_width = max_size
|
|
|
|
|
|
if new_height < max_size:
|
|
|
- pixel_offset[1] = math.ceil((max_size - new_height) / 2)
|
|
|
- new_height = max_size
|
|
|
+ pixel_offset[1] = math.ceil((max_size - new_height) / 2)
|
|
|
+ new_height = max_size
|
|
|
|
|
|
logger.debug('sliceMapCreator pixeloffsets %d, %d, newWidth %d' % (pixel_offset[0], pixel_offset[1], new_width))
|
|
|
|
|
|
return pixel_offset, (new_width, new_height)
|
|
|
|
|
|
+
|
|
|
def crop_frames(frames, pixel_offset, new_size):
|
|
|
if len(frames.shape) != 4:
|
|
|
raise ValueError('frame must have the dimension 3')
|
|
@@ -397,22 +385,24 @@ def crop_frames(frames, pixel_offset, new_size):
|
|
|
|
|
|
logger.debug('squared_frames.shape: %d, %d, %d, %d' % squared_frames.shape)
|
|
|
logger.debug(' frames.shape: %d, %d, %d, %d' % frames.shape)
|
|
|
- logger.debug('offsety: %d, offsetx: %d ' %(offset_y, offset_x))
|
|
|
+ logger.debug('offsety: %d, offsetx: %d ' % (offset_y, offset_x))
|
|
|
'''for frameNumber in range(0, frames.shape[0]):
|
|
|
squared_frames[frameNumber] = sliceMapCreator.cropFrame(frames[frameNumber], pixelOffset, newSize)'''
|
|
|
|
|
|
- squared_frames[:, :, offset_y : y + offset_y, offset_x : x + offset_x ] = frames
|
|
|
+ squared_frames[:, :, offset_y: y + offset_y, offset_x: x + offset_x] = frames
|
|
|
|
|
|
'''newFrame = np.zeros((z, y + 2 * offset_y, x + 2 * offset_x), dtype=frame.dtype)
|
|
|
newFrame[:, offset_y : offset_y + y, offset_x : offset_x + x]'''
|
|
|
|
|
|
return squared_frames
|
|
|
|
|
|
+
|
|
|
class ResultFrame():
|
|
|
def __init__(self, frame, fromImage):
|
|
|
self.frame = frame
|
|
|
self.fromImage = fromImage
|
|
|
|
|
|
+
|
|
|
def framePreprocessor(inFrame, queue, fromImage, toImage, destShape):
|
|
|
numberOfImages = toImage - fromImage + 1
|
|
|
outFrame = np.empty((numberOfImages, destShape[0], destShape[1]), dtype=inFrame.dtype)
|