I'm actually trying to display a .tif image within a canva. However, when i launch the script, the canvas is empty. Here is the function in which i'm trying to display the .tif image in a canvas :
def display_sem_data():
global sem_data_path
global sem_photo
global current_sem_image_index
global sem_images
global sem_data_path
global sem_canvas
# Affectation of new sem_data_path with the selected sample name
sem_data_path = sem_data_path + selected_sample
#Checking if there is any SEM folder for this sample
if not os.path.isdir(sem_data_path):
# If not, display "No SEM data found" in the canvas
NoSEMDataCanvas = tk.Canvas(SEMFrame, height=40, width=900)
NoSEMDataCanvas.grid(row=0, column=0, sticky='nsew')
NoSEMDataCanvas.create_text(
450, 20, text="No data folder found for this sample, please make sure to create your sample folder at this directory :\n"+sem_data_path)
return
# Get list of all SEM images in the folder
sem_images = [image for image in os.listdir(
sem_data_path) if image.endswith(".tif")]
#Checking if there is any SEM data (.tif files) for this sample
if len(sem_images) == 0:
NoSEMDataCanvas = tk.Canvas(SEMFrame, height=40, width=900)
NoSEMDataCanvas.grid(row=0, column=0, sticky='nsew')
NoSEMDataCanvas.create_text(
450, 20, text="No SEM data found, please make sure your .tif file is in your sample folder at this directory :\n"+sem_data_path)
return
# Create a canvas to display the SEM images
sem_canvas = tk.Canvas(SEMFrame, width=800, height=600)
sem_canvas.grid(row=0, column=0)
# Create a variable to keep track of the current image index
current_sem_image_index = tk.IntVar()
current_sem_image_index.set(0)
# Display the first image on the canvas
print(sem_data_path + "\\" + sem_images[current_sem_image_index.get()])
current_sem_image = Image.open(sem_data_path + "\\" +
sem_images[current_sem_image_index.get()])
current_sem_image = current_sem_image.resize((640, 512), Image.ANTIALIAS)
sem_photo = ImageTk.PhotoImage(current_sem_image)
sem_canvas.create_image(400, 300, image=sem_photo)
# Create a scale widget to change the current image index
sem_image_index_scale = tk.Scale(SEMFrame, from_=0, to=len(
sem_images)-1, orient='horizontal', variable=current_sem_image_index, command=update_sem_image())
sem_image_index_scale.grid(row=1, column=0, sticky='ew')
I tried to modify the canvas size, the point of the image creation within the canvas, but nothing works.
I verified my directories (which are correct), as well as the files name after their acquisitions (which are also correct).
The .tif image i'm trying to load first is named "221124-c_2-1.tif" and its size is exactly 1280x1024 and weight 1.25 Mo
I would like the image to show up within the canvas.
Then, with a cursor, i'll be able to navigate between all .tif images within the same folder.
Related
I want to load a tif file into SceneView, I tried the method from this, the code is as follows:
Esri.ArcGISRuntime.Geometry.Envelope pacificSouthwestEnvelope = ...;
// Create an ImageFrame with a local image file and the extent envelope
ImageFrame imageFrame = new ImageFrame(new System.Uri(TifFilePath), pacificSouthwestEnvelope);
//ImageFrame imageFrame = new ImageFrame(image, pacificSouthwestEnvelope);
// Add the ImageFrame to an ImageOverlay and set it to be 50% transparent
ImageOverlay imageOverlay = new ImageOverlay(imageFrame);
imageOverlay.Opacity = 1;
// Add the ImageOverlay to the scene view's ImageOverlay collection
MySceneView.ImageOverlays.Add(imageOverlay);
imageFrame.LoadAsync().Wait();
await MySceneView.SetViewpointAsync(new Viewpoint(imageFrame.Extent));
But it did not succeed.
Envelope's range is obtained in arcmap.
The LoadAsync() method is called to ensure that the layer has been loaded.
Finally, I set the display range of SceneView to the range of imageFrame.
But I did not see my picture on SceneView.
Then I tried to load .png and .jpg files, but they were also unsuccessful.
I don't know what's wrong with my code?
Finally, I used RasterLayer to complete the loading of the tif file.
Raster raster = new Raster(filePath);
RasterLayer rasterLayer = new RasterLayer(raster);
MySceneView.Scene.Basemap.BaseLayers.Add(rasterLayer);
I need help with saving processed images to different folder with imwrite.
Currently, I can save all processed images to a single folder.
Img_filename=ls('C:\Users\User\Desktop\Guanlin_CNN1D\CNN1D\GF_BSIF\*.jpg');
imageSize = size(Img_filename);
Img_filenum = size(Img_filename,1);
for img=1:Img_filenum
img_temp=double((rgb2gray(imread(Img_filename(img,:)))));
-----------processing--------
count = count+1;
FileName = fullfile('C:\Users\User\Desktop\Guanlin_CNN1D\CNN1D\GF_BSIF\folder_1',sprintf('%03d_circle_cropped.jpg',count));
imwrite(MM, FileName)
end
However, I have 1000 different images in 1 folder and after processing, it will generate 500 images and I want to save the first 500 processed images into folder_1. And the second 500 processed images to folder_2 and the third 500 images to folder_3 and so on...
How to re-write the imwrite function?
Thank you!
The root folder I used here is named Image_Folder and resides on the desktop. The output folders are named Folder_1, Folder_2 and Folder_3 and also reside on the desktop. I used two nested loops to control the saving of the images. The outer loop controls which folder to write to and the inner loop controls the writing images 1 through 500. The variable Image_File_Names can be used to access the input image file names.
%Folder holding the 1000 images%
Image_Path = "/Users/michael/Desktop/Image_Folder";
%Prefix path for output folders%
Export_Path = "/Users/michael/Desktop/Folder_";
%Adding path with input images%
addpath(Image_Path);
%Listing all images in folder directory%
Image_File_Names = ls(Image_Path);
Image_File_Names = split(Image_File_Names);
Number_Of_Images = length(Image_File_Names) - 1;
for Folder_Index = 1: 3
Export_Folder_Path = Export_Path + num2str(Folder_Index) + "/";
mkdir(Export_Folder_Path);
for Image_Index = 1: 500
%Grab the images as needed%
Input_Image_Index = 1; %Change this index to another variable to grab images within input folder%
Image = imread(string(Image_File_Names(Input_Image_Index)));
%***************************************%
%PROCESS IMAGE%
%***************************************%
%***************************************%
Output_File_Path = Export_Folder_Path + "Image_" + num2str(Image_Index) +".jpg";
imwrite(Image,Export_Folder_Path+num2str(Image_Index)+".jpg");
end
end
Using MATLAB version: R2019b
My end goal is to change the theme of a presentation. To do this, I have created a source template and new template (with the correct theme). I iterate over each slide in the source template then add a new slide to the new template with the contents of the source using the code below - source. If there is a better way to do this I'd love to hear it.
This works great for text and text boxes, however the test image cannot be displayed in the new powerpoint (show in the image below):
Code
def copy_slide_from_external_prs(self, src, idx, newPrs):
# specify the slide you want to copy the contents from
src_slide = src.slides[idx]
# Define the layout you want to use from your generated pptx
slide_layout = newPrs.slide_layouts[2]
# create now slide, to copy contents to
curr_slide = newPrs.slides.add_slide(slide_layout)
# remove placeholders
for p in [s.element for s in curr_slide.shapes if 'Text Placeholder' in s.name or 'Title' in s.name]:
p.getparent().remove(p)
# now copy contents from external slide, but do not copy slide properties
# e.g. slide layouts, etc., because these would produce errors, as diplicate
# entries might be generated
for shp in src_slide.shapes:
el = shp.element
newel = copy.deepcopy(el)
curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
return newPrs
I was trying many different solutions and tried creating a new Picture using the image.blob property in the source image. However, then the image does not have an element. Do I need to convert the blob to a PNG, save it, then create a new image using that saved PNG?
There must be a better way to do this. Again, I just want to change the theme.
Thanks in advance!
Here is the workaround I developed. I first check if the shape is an image, and if it is, I write the image to a local directory. Then I add a picture to the slide using that saved image. Finally, I delete the locally saved image.
Now this copy_slide function works for images:
def copy_slide_from_external_prs(src, idx, newPrs):
# specify the slide you want to copy the contents from
src_slide = src.slides[idx]
# Define the layout you want to use from your generated pptx
SLD_LAYOUT = 5
slide_layout = prs.slide_layouts[SLD_LAYOUT]
# create now slide, to copy contents to
curr_slide = newPrs.slides.add_slide(slide_layout)
# create images dict
imgDict = {}
# now copy contents from external slide, but do not copy slide properties
# e.g. slide layouts, etc., because these would produce errors, as diplicate
# entries might be generated
for shp in src_slide.shapes:
if 'Picture' in shp.name:
# save image
with open(shp.name+'.jpg', 'wb') as f:
f.write(shp.image.blob)
# add image to dict
imgDict[shp.name+'.jpg'] = [shp.left, shp.top, shp.width, shp.height]
else:
# create copy of elem
el = shp.element
newel = copy.deepcopy(el)
# add elem to shape tree
curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
# add pictures
for k, v in imgDict.items():
curr_slide.shapes.add_picture(k, v[0], v[1], v[2], v[3])
os.remove(k)
Using code from #Michael-Berk I made very elegant method for copying slides (including images). Check this answer: https://stackoverflow.com/a/73954830/20159015
Hello!
i'm trying to have my images appearing on a label by using this code
this is to save a random image from a particular URL
def photo_1():
urls = "https://www.flickr.com/photos/flickr/galleries/72157644537473411/" # You may change this into other websites!
regex = '<img src="([^"]+)".*>'
pattern = re.compile(regex)
photofile=urllib.urlopen(urls)
raw_data=photofile.read()
download=re.findall(pattern,raw_data)
randomdownload=random.choice(download)
urllib.urlretrieve(randomdownload, "1.gif")
global done_button2
done_button2 = Button(photo_window, text = 'Click here to display your chosen image on the black screen!', width = 53, command = generate_1)
done_button2.grid(row = 5, sticky = N)
done_button.config(state='disabled')
and this is to have the saved image appearing on a label but apparently not working so well ..
def generate_1():
img = ImageTk.PhotoImage(Image.open("1.gif"))
image_area = Label(photo_window, image = img, width = 55, height = 5).grid(row=2)
global done_button3
done_button3 = Button(photo_window, text = 'Click here to save the second random image locally! ', width = 53, command = photo_2)
done_button3.grid(row = 6, sticky = N)
done_button2.config(state='disabled')
this was a part of my code and when i run this application i made,
the only thing i can see is a white rectangular shape and i would like it to be as big as a black label underneath (size of (55,5) )with the actual image appearing...
can anyone help me with this problem?
You might have thought that my English is not that great haha
but please have mercy!
I would like to write the equivalent of
convert left.jpg right.jpg +append ouput.jpg
I found something like it in another post:
files = glob('*.jpg')
with Image() as orig: # create empty Image object
for f in files:
page = Image(filename=f)
orig.sequence.append(page)
orig.save(filename='result.pdf')
and changed it to
with Image() as orig: # create empty Image object
page = Image(filename='left.jpg'); orig.sequence.append(page)
page = Image(filename='right.jpg'); orig.sequence.append(page)
orig.save(filename='output.jpg')
but the output file just shows the first file, rather than a file with the images side-by-side.
My first attempt was completely wrong, it probably makes an animated image. Provided the two images are the same size, this will do it:
with Image() as blankimage:
with Image(filename = 'imageA.tif') as imageA:
w = imageA.width; h = imageA.height
with Image(filename = 'imageB.tif') as imageB:
blankimage.blank(w*2, h)
blankimage.composite(imageA, 0, 0)
blankimage.composite(imageB, w, 0)
blankimage.save(filename = 'output.tif')