def image_file_load(Prg, Path, ThumbnailSize=None, PixelReturn=False): if not os.path.isfile(Path): Msg = util.ui_msg(Prg, "file_operation.file_missing", PrintInTerminal=True) Prg["Warning"].append(Msg) return False Img = Image.open(Path) if ThumbnailSize: Img.thumbnail(ThumbnailSize) if PixelReturn: return Img.load() return Img
def test_ui_msg(self): Prg = { "Os": "Linux", "UiLanguage": "hun", "UiMessages": { "Menu": { "File": { "SaveAs": { "eng": "Save as", "hun": "Mentés másként" }, "Load": { "eng": "Load" }, "Export": { "hun": "Exportálás" } } }, "Text_formatted": { "eng": "File: {:s}", "hun": "Fájl: {:s}" } } } # happy path: self.assertEqual("Mentés másként", util.ui_msg(Prg, "Menu.File.SaveAs", TestCase=True)) # handle multiple value in one request: TxtSaveAs, Export = util.ui_msg(Prg, ["Menu.File.SaveAs", "Menu.File.Load"], TestCase=True) self.assertEqual("Mentés másként", TxtSaveAs) self.assertEqual("Load", Export) # key is unknown: self.assertEqual( "Ui message key is unknown in container: hun - Menu.File.UnknownKey", util.ui_msg(Prg, "Menu.File.UnknownKey", TestCase=True)) # key exists, language==hun, but only default eng element exists in translations self.assertEqual("Load", util.ui_msg(Prg, "Menu.File.Load", TestCase=True)) # key hasn't got default eng value: self.assertEqual( "Ui message, default eng translation is missing: Menu.File.Export", util.ui_msg(Prg, "Menu.File.Export", TestCase=True)) # text with formatting: self.assertEqual( "Fájl: /tmp/file.txt", util.ui_msg(Prg, "Text_formatted", TestCase=True).format("/tmp/file.txt"))
def mark_pixels_select_from_img(Prg, Img, ColorBlockBackgroundRgb, ColorBlockBackgroundRgbDelta, ColorBlockBackgroundGray, ColorBlockBackgroundGrayDelta): if not util.img_is_rgb(Img) and not util.img_is_grayscale(Img): print(util.ui_msg(Prg, "ocr.pixel_data_size_unknown")) PixelDataSize = Img.get("PixelDataSize", -1) sys.exit(PixelDataSize) InkPixels_and_MarkId = dict() DeltaR, DeltaG, DeltaB = ColorBlockBackgroundRgbDelta BackgroundR, BackgroundG, BackgroundB = ColorBlockBackgroundRgb BgRedMax = BackgroundR + DeltaR BgRedMin = BackgroundR - DeltaR BgGreenMax = BackgroundG + DeltaG BgGreenMin = BackgroundG - DeltaG BgBlueMax = BackgroundB + DeltaB BgBlueMin = BackgroundB - DeltaB BgGrayMin = ColorBlockBackgroundGray - ColorBlockBackgroundGrayDelta BgGrayMax = ColorBlockBackgroundGray + ColorBlockBackgroundGrayDelta if util.img_is_rgb(Img): def is_mark(Img, X, Y): if is_mark_rgb(Img, X, Y, BgRedMin, BgRedMax, BgGreenMin, BgGreenMax, BgBlueMin, BgBlueMax): return True return False if util.img_is_grayscale(Img): def is_mark(Img, X, Y): if is_mark_grayscale(Img, X, Y, BgGrayMin, BgGrayMax): return True return False for X in range(0, Img["Width"]): for Y in range(0, Img["Height"]): if is_mark(Img, X, Y): InkPixels_and_MarkId[(X, Y)] = None return InkPixels_and_MarkId
def window_main(Prg): # store passed Prg as a global variable, too, because Tkinter buttons need a state # I collect the msg NOT in the if because if one of them is missing, it causes Error global PrgGlobal PrgGlobal = Prg Prg["Tkinter"] = dict() MainWidth = 1200 MainHeight = 800 SourceWidth = 300 OnePageWidth = 600 Window = window_new(Prg, "window.main.title") Window.geometry('{}x{}'.format(MainWidth, MainHeight)) Prg["Tkinter"]["Window"] = Window def frame_thumbnail_bind(Event, Canvas): print("Event:", Event) print("canvas bbox all", Canvas.bbox("all")) ScrollRegion = Canvas.bbox("all") Canvas.configure(scrollregion=ScrollRegion) ############# SCROLLBAR ################### ContainerLeft = Tkinter.Frame(Window, bg="blue", width=SourceWidth) ContainerLeft.pack(side="left") # # https://stackoverflow.com/questions/16188420/tkinter-scrollbar-for-frame CanvasForScrollBar = Tkinter.Canvas(ContainerLeft, bg="red", width=SourceWidth, height=9999) # auto fitting in Y direction, until reach this value CanvasForScrollBar.pack(side="left", fill="y", expand=True) # IMPORTANT: the canvas FILL, expand settins Modifiy the scrollbar's look!!!!! Prg["Tkinter"]["CanvasForScrollBar"] = CanvasForScrollBar # Tkinter.Label(CanvasForScrollBar, text="canvas").pack() FrameThumbnails = Tkinter.Frame(CanvasForScrollBar, bg="purple") Prg["Tkinter"]["FrameThumbnails"] = FrameThumbnails Scrollbar = Tkinter.Scrollbar(ContainerLeft, orient="vertical", command=CanvasForScrollBar.yview) CanvasForScrollBar.configure(yscrollcommand=Scrollbar.set) Scrollbar.pack(side="right", fill="y") CanvasForScrollBar.create_window((0, 0), window=FrameThumbnails, anchor='nw') FrameThumbnails.bind("<Configure>", lambda Event, Canvas=CanvasForScrollBar: frame_thumbnail_bind(Event, Canvas)) ############# SCROLLBAR ################### ##### Text Select Preview ################# OnePageFrame = frame_new(Prg, Window, OnePageWidth, MainHeight) OnePageFrame.pack() Tkinter.Button(OnePageFrame, text=util.ui_msg(Prg, "ocr.page_current_analyse"), command=marks_collect_from_page).pack() TextSelectPreviewImg = Image.new("RGB", Prg["UiTextSelectPreviewSize"], color="grey") Prg["Tkinter"]["OnePageTextSelectPreviewImgLoaded"] = None Prg["Tkinter"]["OnePageTextSelectPreviewImgRendered"] = TextSelectPreviewImg ImageTkPhotoImage = ImageTk.PhotoImage(TextSelectPreviewImg) Label = Tkinter.Label(OnePageFrame, image=ImageTkPhotoImage) Prg["Tkinter"]["OnePageTextSelectPreviewLabel"] = Label Label.pack() ##### Text Select Preview ################# Tkinter.Button(FrameThumbnails, text=util.ui_msg(Prg, "file_operation.file_load_into_thumbnail_list"), command=files_thumbnails_load_button_cmd).pack() Window.mainloop()
def window_new(Prg, TitleKey=""): Window = Tkinter.Tk() if TitleKey: Window.title(util.ui_msg(Prg, TitleKey)) return Window