Skip to content

Commit 3024f8b

Browse files
add open file option
1 parent 408ff33 commit 3024f8b

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

Diff for: black hole simulation - menu.py

+50-23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from tkinter import Button, Tk, Frame, Entry, Label, Checkbutton, BooleanVar, StringVar
1010
import sys
1111
from numba import jit
12+
from tkinter.filedialog import askopenfilename
1213

1314
# =============================================================================
1415
#Variables & constants initialisation (don't change)
@@ -425,10 +426,6 @@ def gif(nbr_offset):
425426
offset_X_temp=int(axe_X/nbr_offset) #at the end to have offset=0 for the first iteration
426427
offset_X_tot+=offset_X_temp
427428
# =============================================================================
428-
img_original = Image.open(nom_image, mode='r') #use equirectangular images
429-
img_debut=resize_img(img_original,final_size_img)
430-
print("FOV ",FOV_img,"*",FOV_img_Y,\n",sep='')
431-
#------------------------------------------------------------------------------
432429
def black_hole():
433430
global left_side,right_side,up_side,down_side,interpolation,img_debut,D,Rs,deviated_angle_splin,seen_angle_splin,img_res,seen_angle,deviated_angle,offset_X_tot,FOV,FOV_img,last_angle
434431
vrai_debut=time.process_time()
@@ -476,6 +473,14 @@ def black_hole():
476473
vrai_fin=time.process_time()
477474
print("\nglobal computing time:",round(vrai_fin-vrai_debut,1))
478475
# =============================================================================
476+
def open_image(nom_image):
477+
img_original = Image.open(nom_image, mode='r') #use equirectangular images
478+
img_debut=resize_img(img_original,final_size_img)
479+
print("FOV ",FOV_img,"*",FOV_img_Y,\n",sep='')
480+
return [img_original, img_debut]
481+
482+
[img_original, img_debut] = open_image(nom_image)
483+
479484
black_hole()
480485
# =============================================================================
481486
zoom=0
@@ -608,6 +613,25 @@ def save_gif(): # function that listens to click event
608613
message3["text"] = "Save: "+nom_court+" D="+str(D)+" Rs="+str(Rs)+" size="+str(final_size_img)+" offset_X=*"+extension
609614
except:
610615
message3["text"] = "need integer"
616+
617+
def open_file_name():
618+
# https://gist.github.com/Yagisanatode/0d1baad4e3a871587ab1
619+
global nom_image, nom_court, extension, img_original, img_debut, img
620+
nom_image = askopenfilename(initialdir="C:/Users/Batman/Documents/Programming/tkinter/",
621+
filetypes =[("Image File", ".png .jpg")],
622+
title = "Choose a image."
623+
)
624+
if nom_image:
625+
[img_original, img_debut] = open_image(nom_image)
626+
[img_original, img_debut] = open_image(nom_image)
627+
img2=Image.new( 'RGBA', (axe_X,axe_Y)) #creat a transparent image (outside of the function to recover info if must stop loop)
628+
img2=img_pixels(img_debut,img2)
629+
img=ax.imshow(img2)
630+
fig.canvas.draw()
631+
message2["text"] = "Done !"
632+
else:
633+
print("cancel")
634+
611635
# =============================================================================
612636
root = Tk()
613637
frame = Frame(root)
@@ -616,60 +640,63 @@ def save_gif(): # function that listens to click event
616640
#bottomframe = Frame(root) #si bas diff du haut (inbrique plusieurs widgets)
617641
#bottomframe.pack( side = "bottom" )
618642

643+
open_file_button = Button(frame, text="Open image", width=14, command=open_file_name)
644+
open_file_button.grid(row=0, column=0)
645+
619646
L1 = Label(frame, text="Radius")
620-
L1.grid(row=0, column=0)
647+
L1.grid(row=1, column=0)
621648
radius = Entry(frame,textvariable=StringVar(frame, value=Rs), bd =2, width=7)
622-
radius.grid(row=0, column=1)
649+
radius.grid(row=1, column=1)
623650

624651
L2 = Label(frame, text="Distance")
625-
L2.grid(row=1, column=0)
652+
L2.grid(row=2, column=0)
626653
distance = Entry(frame,textvariable=StringVar(frame, value=D), bd =2, width=7)
627-
distance.grid(row=1, column=1)
654+
distance.grid(row=2, column=1)
628655

629656
compute_button = Button(frame, text="Compute", width=14, command=compute)
630-
compute_button.grid(row=0, column=2)
657+
compute_button.grid(row=1, column=2)
631658

632659
message = Label(frame, text="", width=20) #allow to display message when activate [text]
633-
message.grid(row=0, column=3)
660+
message.grid(row=1, column=3)
634661
message5 = Label(frame, text="", width=20) #allow to display message when activate [text]
635-
message5.grid(row=1, column=3)
662+
message5.grid(row=2, column=3)
636663

637664
L3 = Label(frame, text="Image size")
638-
L3.grid(row=2, column=0)
665+
L3.grid(row=3, column=0)
639666
size = Entry(frame,textvariable=StringVar(frame, value=final_size_img), bd =2, width=7)
640-
size.grid(row=2, column=1)
667+
size.grid(row=3, column=1)
641668

642669
size_button = Button(frame, text="Resolution", width=14, command=increase_resolution)
643-
size_button.grid(row=2, column=2)
670+
size_button.grid(row=3, column=2)
644671

645672
message2 = Label(frame, text="", width=20) #allow to display message when activate [text]
646-
message2.grid(row=2, column=3)
673+
message2.grid(row=3, column=3)
647674

648675
save_button = Button(frame, text="Save image", width=14, command=save_file)
649-
save_button.grid(row=3, column=2)
676+
save_button.grid(row=4, column=2)
650677

651678
message4 = Label(frame, text="") #allow to display message when activate [text]
652-
message4.grid(row=3, column=3)
679+
message4.grid(row=4, column=3)
653680

654681
message6 = Label(frame, text="Fix background") #allow to display message when activate [text]
655-
message6.grid(row=4, column=0)
682+
message6.grid(row=5, column=0)
656683

657684
fixed_background = BooleanVar()
658685
C1 = Checkbutton(frame, text = "", variable = fixed_background, \
659686
onvalue = True, offvalue = False)
660-
C1.grid(row=4, column=1)
687+
C1.grid(row=5, column=1)
661688

662689
L4 = Label(frame, text="images")
663-
L4.grid(row=5, column=0)
690+
L4.grid(row=6, column=0)
664691

665692
number = Entry(frame,textvariable=StringVar(frame, value=10), bd =2, width=7)
666-
number.grid(row=5, column=1)
693+
number.grid(row=6, column=1)
667694

668695
save_gif_button = Button(frame, text="Save animation", width=14, command=save_gif)
669-
save_gif_button.grid(row=5, column=2)
696+
save_gif_button.grid(row=6, column=2)
670697

671698
message3 = Label(frame, text="") #allow to display message when activate [text]
672-
message3.grid(row=5, column=3)
699+
message3.grid(row=6, column=3)
673700

674701
root.mainloop()
675702
#raise SystemExit #not needed and too brutal

0 commit comments

Comments
 (0)