Ejemplo n.º 1
0
def testcases():

    # Test cases
    # read / save photon file
    # read / save cbddlp file (should be same as photon file)

    # read / save properties for header/layers
    #

    print("Save All...")
    photon_filepath_org = '/home/nard/PhotonFile/test/bunny.photon'
    photonfile = PhotonFile()
    photonfile.load(photon_filepath_org)
    photon_dirpath_new = '/home/nard/PhotonFile/test/bunny.img'
    photonfile.layers.saveAll(photon_dirpath_new)
    print("...success.")

    return

    print("Test signatures...")
    photonfile = PhotonFile()
    photon_filepath_org = '/home/nard/PhotonFile/test/bunny.photon'
    photonfile.load(photon_filepath_org)
    assert (photonfile.signature() == 'ChituBox 1.4.0')
    print("...success.")

    print("Test read/write .cbddlp file...")
    filepath = '/home/nard/PhotonFile/test/bunny.cbddlp'
    filepath_new = '/home/nard/PhotonFile/test/bunny2.cbddlp'
    org_filesize = os.path.getsize(filepath)
    photonfile = PhotonFile(filepath)
    photonfile.load()
    photonfile.save(filepath_new)
    new_filesize = os.path.getsize(filepath_new)
    assert new_filesize == org_filesize
    print("...success.")

    print("Test read/write .photon file...")
    filepath = '/home/nard/PhotonFile/test/bunny.photon'
    filepath_new = '/home/nard/PhotonFile/test/bunny2.photon'
    org_filesize = os.path.getsize(filepath)
    photonfile = PhotonFile()
    #photonfile.load()
    photonfile.load(filepath)
    photonfile.load(filepath)  # do this 2 time to check clear old vars
    photonfile.save(filepath_new)
    new_filesize = os.path.getsize(filepath_new)
    assert new_filesize == org_filesize
    print("...success.")

    print("Test layers.count / layers.last / layers.height / volume...")
    filepath = '/home/nard/PhotonFile/test/bunny.photon'
    photonfile.load(filepath)
    assert photonfile.layers.count() == 1716
    assert photonfile.layers.last() == 1716 - 1
    assert photonfile.layers.height(0) == 0.05
    assert photonfile.volume(retUnit='ml') == 9.0
    print("...success.")

    print("Test Property get/set...")
    dlpWidth_write = 2560
    prevWidth_write = 320
    layerHeight_write = 0.04
    photonfile.setProperty("Resolution X", dlpWidth_write)
    photonfile.previews.setProperty(0, "Resolution X", prevWidth_write)
    photonfile.layers.setProperty(0, "Layer height (mm)", layerHeight_write)
    dlpWidth_read = photonfile.getProperty("Resolution X")
    prevWidth_read = photonfile.previews.getProperty(0, "Resolution X")
    layerHeight_read = photonfile.layers.getProperty(0, "Layer height (mm)")
    assert dlpWidth_read == dlpWidth_write
    assert prevWidth_read == prevWidth_write
    assert layerHeight_read == layerHeight_write
    print("...success.")

    print("Test layerimage save to file...")
    filepath0 = '/home/nard/PhotonFile/test/images/test0.png'
    filepath4 = '/home/nard/PhotonFile/test/images/test4.png'
    photonfile.layers.save(0, filepath0)
    photonfile.layers.save(4, filepath4)
    print("...success.")

    print("Test image insert to layer...")
    filepath0 = '/home/nard/PhotonFile/test/images/test0.png'
    filepath4 = '/home/nard/PhotonFile/test/images/test4.png'
    photonfile.layers.insert(filepath4, 4)
    photonfile.layers.insert(filepath0, 0)
    assert photonfile.layers.count() == 1716 + 2
    print("...success.")

    print("Test layer delete...")
    photonfile.layers.delete(0)
    photonfile.layers.delete(4)
    assert photonfile.layers.count() == 1716
    photonfile.save(filepath_new)
    new_filesize = os.path.getsize(filepath_new)
    assert new_filesize == org_filesize
    print("...success.")

    print("Test image encoding...")
    filepath = '/home/nard/PhotonFile/test/images/test.png'
    filepath_new = '/home/nard/PhotonFile/test/images/test2.png'
    photonfile.layers.insert(filepath, 4)
    photonfile.layers.save(4, filepath_new)
    org_imgfilesize = os.path.getsize(filepath)
    org_imgobjsize = PIL.Image.open(filepath).size
    new_imgfilesize = os.path.getsize(filepath_new)
    new_imgobjsize = PIL.Image.open(filepath_new).size
    assert (new_imgobjsize == org_imgobjsize)
    assert (new_imgfilesize == org_imgfilesize)
    print("...success.")

    print("Test layer replace layer with new image...")
    photon_filepath_org = '/home/nard/PhotonFile/test/bunny.photon'
    photonfile.load(photon_filepath_org)
    img_filepath4 = '/home/nard/PhotonFile/test/images/test4.png'
    photonfile.layers.replace(4, img_filepath4)
    assert photonfile.layers.count() == 1716
    photon_filepath_new = '/home/nard/PhotonFile/test/bunny2.photon'
    photonfile.save(photon_filepath_new)
    org_filesize = os.path.getsize(photon_filepath_org)
    new_filesize = os.path.getsize(photon_filepath_new)
    assert new_filesize == org_filesize
    print("...success.")

    print("Test append photonfile with new image...")
    photon_filepath_org = '/home/nard/PhotonFile/test/bunny.photon'
    photon_filepath_new = '/home/nard/PhotonFile/test/bunny2.photon'
    photonfile.load(photon_filepath_org)
    img_filepath4 = '/home/nard/PhotonFile/test/images/test4.png'
    photonfile.layers.append(img_filepath4)
    photonfile.layers.delete(photonfile.layers.last())
    photonfile.save(photon_filepath_new)
    assert new_filesize == org_filesize
    print("...success.")

    print("Save All...")
    photon_filepath_org = '/home/nard/PhotonFile/test/bunny.photon'
    photonfile.load(photon_filepath_org)
    photon_dirpath_new = '/home/nard/PhotonFile/test/bunny.img'
    photonfile.layers.saveAll(photon_dirpath_new)
    print("...success.")

    print("get All...")
    print("...success.")

    print("Save Replace...")
    print("...success.")

    print("Test copy image (via clipboard)...")
    print("...success.")

    print("Test cut/paste image (via clipboard)...")
    print("...success.")

    print("Test undo (via clipboard)...")
    print("...success.")

    print("Save Preview...")
    print("...success.")

    print("Replace Preview...")
    print("...success.")

    return
Ejemplo n.º 2
0
def readPhotonFile(filename):
    global files
    global layerHeight

    photonfile = PhotonFile()
    photonfile.load(filename)

    # Check if file not already present
    for file in files:
        if file[1] == filename:
            root.option_add('*Dialog.msg.font', 'Helvetica 10')
            messagebox.showerror('File already loaded',
                                 'You already loaded this file.')
            return

    # If first file we copy properties
    if len(files) == 0:
        layerHeight = photonfile.getProperty("Layer height (mm)")

        entry_BottomLayers.delete(0, tkinter.END)
        entry_NormalExp.delete(0, tkinter.END)
        entry_BottomExp.delete(0, tkinter.END)
        entry_Offtime.delete(0, tkinter.END)
        entry_BottomLayers.insert(0, photonfile.getProperty("# Bottom Layers"))
        entry_NormalExp.insert(0, photonfile.getProperty("Exp. time (s)"))
        entry_BottomExp.insert(0, photonfile.getProperty("Exp. bottom (s)"))
        entry_Offtime.insert(0, photonfile.getProperty("Off time (s)"))

    # Check if layerheight is same as other layerheights
    if photonfile.getProperty("Layer height (mm)") != layerHeight:
        root.option_add('*Dialog.msg.font', 'Helvetica 10')
        messagebox.showerror('Different Layer Height',
                             'All photonfiles should have samen layerheight.')
        return

    # set layerheight
    label_layerHeight['text'] = str(layerHeight)

    # Show messagebox to let use know we are analyzing
    app = showProgress(root, "Analyzing...", 0, 1, True, False, False)

    xmin, ymin, xmax, ymax = 9999, 9999, 0, 0
    for layerNr in range(photonfile.layers.count()):
        im = photonfile.layers.get(
            layerNr, 'n')  # 2560 rows, 1440 cols; element is called im[y,x]
        res = cv2.findContours(im.copy(), cv2.RETR_EXTERNAL,
                               cv2.CHAIN_APPROX_SIMPLE)
        # In the current OpenCV's master branch the return statements have changed, see http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours.
        if len(res) == 3: _, contours, hierarchy = res
        if len(res) == 2: contours, hierarchy = res
        #contours, hierarchy = cv2.findContours(im.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
        idx = 0
        for cnt in contours:
            idx += 1
            x, y, w, h = cv2.boundingRect(cnt)
            xmin = min(x, xmin)
            ymin = min(y, ymin)
            xmax = max(x + w, xmax)
            ymax = max(y + h, ymax)

        app.setProgressPerc(int(100 * layerNr / photonfile.layers.count()))

    app.hide()

    #put some margin around regions
    margin = 10

    def clamp(n, smallest, largest):
        return max(smallest, min(n, largest))

    xmin = clamp(xmin - margin, 0, 1439)
    ymin = clamp(ymin - margin, 0, 2559)
    xmax = clamp(xmax + margin, 0, 1439)
    ymax = clamp(ymax + margin, 0, 2559)

    #files entries are tuples of:filename,photonfile classobject,position,size
    files.append([
        len(files), filename, photonfile, (xmin, ymin),
        (xmax - xmin, ymax - ymin)
    ])

    print("Read:", filename, photonfile.layers.count(), (xmin, ymin),
          (xmax - xmin, ymax - ymin))
    print("Read:", files[len(files) - 1])

    basename = os.path.basename(filename)
    barename = os.path.splitext(basename)[0]
    item = str(len(files)) + " " + barename
    listbox.insert(tkinter.END, item)

    return photonfile