def SaveCoverFromFs(tiles, newwidth, newheight, cols, rows):

    tilewidth = int(newwidth / cols)
    tileheight = int(newheight / rows)

    newwidth = int(newwidth / tilewidth) * tilewidth
    newheight = int(newheight / tileheight) * tileheight

    hiresoutip = ColorProcessor(newwidth, newheight)
    hiresout = ImagePlus("hi res output", hiresoutip)
    hiresout.show()

    x = 0
    y = -1

    plane = []

    # scale the images
    for i in sorted(tiles.iterkeys()):
        if y < rows - 1:
            y += 1
        else:
            y = 0
            x += 1
        imp = IJ.openImage(str(tiles[i]))
        scale = Scale(imp.getProcessor())
        ipscaled = ScaleImageToSize(scale, tilewidth, tileheight)
        hiresoutip.copyBits(ipscaled, x * tilewidth, y * tileheight, 0)
        hiresout.draw()
def SaveCoverFromZip(tileIndex, newwidth, newheight, cols, rows,
                     originalspath):
    baseDir = re.sub(r'\/originals.zip', "", originalspath)

    #print baseDir

    zf = zipfile.ZipFile(originalspath, mode='r')

    tilewidth = int(newwidth / cols)
    tileheight = int(newheight / rows)

    newwidth = int(newwidth / tilewidth) * tilewidth
    newheight = int(newheight / tileheight) * tileheight

    hiresoutip = ColorProcessor(newwidth, newheight)
    hiresout = ImagePlus("hi res output", hiresoutip)
    hiresout.show()

    x = 0
    y = -1

    plane = []

    # scale the images
    for i in sorted(tileIndex.iterkeys()):
        if y < rows - 1:
            y += 1
        else:
            y = 0
            x += 1
        #bi = bir.openImage(tileIndex[i]);
        #ip = ColorProcessor(bi)
        image = zf.read(str(tileIndex[i]) + ".jpeg")
        #IJ.log("Placing image :" + str(tileIndex[i]) + ".jpeg")
        my_file = open(baseDir + 'temporary.jpeg', 'w')
        my_file.write(image)
        my_file.close()
        imp = IJ.openImage(baseDir + "/temporary.jpeg")
        ip = imp.getProcessor()
        scale = Scale(ip)
        ipscaled = ScaleImageToSize(scale, tilewidth, tileheight)
        hiresoutip.copyBits(ipscaled, x * tilewidth, y * tileheight, 0)
        hiresout.draw()
def PrepareDatabase(minw, maxw, baseDir, aspectRatio, majorWidth, majorHeight):
    outputpath = baseDir + "/" + str(majorWidth) + "_" + str(
        majorHeight) + "_orig.tif"
    #initialize stacks and labels
    stackScaled = []
    stackOrig = ImageStack(majorWidth, majorHeight)
    imageNames = []
    for i in range(minw, maxw + 1):
        stackScaled.append(ImageStack(i, int(round(i / aspectRatio, 0))))
        imageNames.append('')

    counter = 0

    # initialize zip file for originals
    zf = zipfile.ZipFile(baseDir + "/originals.zip",
                         mode='w',
                         compression=zipfile.ZIP_DEFLATED,
                         allowZip64=1)
    zf.writestr('from_string.txt', 'hello')
    zf.close()
    zf = zipfile.ZipFile(baseDir + "/originals.zip",
                         mode='a',
                         compression=zipfile.ZIP_DEFLATED,
                         allowZip64=1)

    for root, dirs, files in os.walk(str(baseDir)):
        for f1 in files:
            if f1.endswith(".jpg") or f1.endswith(".jpe") or f1.endswith(
                    ".jpeg"):
                id = root + "/" + f1
                IJ.redirectErrorMessages()
                IJ.redirectErrorMessages(1)
                imp = IJ.openImage(id)
                if imp is None:
                    print "Couldn\'t open image from file:", id
                    continue
                # skip non RGBimages
                if imp.getProcessor().getNChannels() != 3:
                    print "Converting non RGB image:", id
                    if imp.getStackSize() > 1:
                        StackConverter(imp).convertToRGB()
                    else:
                        ImageConverter(imp).convertToRGB()
                #skip images with different aspect ratio
                width = imp.getWidth()
                height = imp.getHeight()
                ratio = round(
                    float(width) / float(height), 2
                )  # this makes the ratio filering approximate, minor variations in image dimensions will be ignored
                if ratio != aspectRatio:
                    IJ.log("Skipping image of size: " + str(width) + "," +
                           str(height))
                    continue
                # now scale the image within a given range
                scale = Scale(imp.getProcessor())
                IJ.log("Scaling image " + str(counter) + " " + str(id))
                for i in range(minw, maxw + 1):
                    stackScaled[i - minw].addSlice(
                        None,
                        ScaleImageToSize(scale, i,
                                         int(round(i / aspectRatio, 0))))
                    imageNames[i - minw] += str(id) + ";"
                # save the originals to a temp directory
                scaledOrig = ImagePlus(
                    None, ScaleImageToSize(scale, majorWidth, majorHeight))
                SaveToZip(zf, scaledOrig, baseDir, counter)
                counter += 1
    zf.close()
    # save the stacks
    for i in range(minw, maxw + 1):
        impScaled = ImagePlus(
            str(minw) + "_" + str(int(round(i / aspectRatio, 0))),
            stackScaled[i - minw])
        impScaled.show()
        #print imageNames
        impScaled.setProperty('Info', imageNames[i - minw][:-1])
        fs = FileSaver(impScaled)
        filepath = baseDir + "/" + str(i) + "_" + str(
            int(round(i / aspectRatio, 0))) + ".tif"
        IJ.log("Saving output stack" + str(filepath))
        fs.saveAsTiffStack(filepath)
        #IJ.save(impScaled, filepath);
        IJ.log("Done")