ArcGIS Runtime : SceneView load tif file not work - arcgis-runtime

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);

Related

Display a .TIF file into a tk.canvas using the PIL library

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.

add an image to given coordinates on a PDF page

How can I add an image to given coordinates (x,y) on a PDF page, using IronPdf?
Using the samples, I managed easily to add a "stamp", but that is placed in the center of the page.
I would need to be able to pass (x,y) as parameters when adding the image to each page, and I find this info nowhere in the class documentation.
var stamper = new IronPdf.Editing.ImageStamper(Image);
pdf.ApplyStamp(stamper, pageNumber);
See code above that adds the image in the page center.
You can set HorizontalOffset and VerticalOffset by IronPdf.Editing.Length .
I wrote sample code as below, hope it helps:
var pdf = new PdfDocument("./test.pdf");
ImageStamper logoImageStamper = new ImageStamper("[YOUR IMAGE PATH]/cat.jpg");
logoImageStamper.HorizontalOffset = new Length(300, MeasurementUnit.Pixel);
logoImageStamper.VerticalOffset = new Length(-500, MeasurementUnit.Pixel);
pdf.ApplyStamp(logoImageStamper);
pdf.SaveAs("test_new.pdf");
Code running effect:

How to use Itext to add an image file into an existing PDF, and declare a unique name for it?

In order to find an added image file and replace it with another image file when I read a PDF next time, I want to use Itext to add an image file into an existing PDF, and declare a unique name for it.
My code:
final PdfName key = new PdfName("MY_SIGN_KEY");
final PdfName val = new PdfName("MY_SIGN_VAL");
Image signImage=Image.getInstance(signPngFile.getAbsolutePath());
signImage.setAlignment(1);
signImage.scaleAbsolute(newWidth, newHeight);
signImage.setAbsolutePosition(200,200);
PdfContentByte over = stamper.getOverContent(1);
PdfImage stream = new PdfImage(signImage, "", null);
stream.put(key,val);// a unique name for it.(设置唯一标识符)
//PdfIndirectObject ref=over.getPdfWriter().addToBody(stream);
//signImage.setDirectReference(ref.getIndirectReference());
over.addImage(signImage);
I have tried your code and it works for me. See the AddImageWithID example:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Image image = Image.getInstance(IMG);
PdfImage stream = new PdfImage(image, "", null);
stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789"));
PdfIndirectObject ref = stamper.getWriter().addToBody(stream);
image.setDirectReference(ref.getIndirectReference());
image.setAbsolutePosition(36, 400);
PdfContentByte over = stamper.getOverContent(1);
over.addImage(image);
stamper.close();
reader.close();
}
In this example, I take a file named hello.pdf and I add an image named bruno.jpg with the file hello_with_image_id.pdf as result.
The image doesn't look black:
The ID is added:
Can you try the code I shared and see if the problem persists.
I can think of one reason why you'd get a black image: in our code, we assume that a single image is added. In the case of JPEG, this is always the case. In the case of PNG or GIF though, adding one source image could result in two images being added. Strictly speaking, PDF doesn't support transparent images (depending on how you interpret the concept of transparent images). Whenever you add a single source image with transparent parts, two images will be added to the PDF: one opaque image and one image mask. The combination of the opaque image and the image mask results in something that is perceived as a transparent image. Maybe this is what happens in your case.

load a thumbnail on QListWidget with reduced resolution

I need to reduce the resolution of the images that I add and show in a QListWidgtet.
Now I use the next code but it does not show all the images because they are being loaded with full resolution
void ImagesWizard::on_pbAddImages_clicked()
{
QFileDialog dialog(this);
dialog.setDirectory(mInitPath);
dialog.setFileMode(QFileDialog::ExistingFiles);
dialog.setNameFilter(trUtf8("Images (*.jpg *.png *.tif *.tiff *.bmp);; JPG (*.jpg);; PNG (*.png);; TIF (*.tif *.tiff);; BMP (*.bmp);;"));
QStringList filesToLoad;
if (dialog.exec())
filesToLoad = dialog.selectedFiles();
if (filesToLoad.count()!=0) {
QListWidget *localPathList= new QListWidget();
localPathList->setViewMode(QListWidget::IconMode );
localPathList->setIconSize(QSize(100,100));
localPathList->setResizeMode(QListWidget::Adjust);
localPathList->setSelectionMode(QAbstractItemView::MultiSelection);
for (int var = 0; var < filesToLoad.count(); ++var) {
if (!mImagesList->contains(filesToLoad[var])) {
QFileInfo fileInfo(filesToLoad[var]);
QString filename(fileInfo.fileName());
QListWidgetItem *listItem = new QListWidgetItem(QIcon(filesToLoad[var]),filename);
localPathList->addItem(listItem);
mImagesList->append(filesToLoad[var]);
}
}
pbNext->setFocus();
}
}
Is there any way to resize the resolution in of the QIcon to optimice the time consumed to load the images and to show all the images added?
Now if I load a lot of images the last QListWidgetItems only add the name.
Thanks for your help
Use QImage first to scale the image and construct the icon from the resulting pixmap.
QSize desiredSize;
Qimage orig(filesToLoad[var]);
Qimage scaled = orig.scaled(
desiredSize,
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
QListWidgetItem *listItem = new QListWidgetItem(QIcon(Qpixmap::fromImage(scaled)),filename);
It is very common to store the presized image too on the disk, to avoid the two step conversion process.

kivy: possible to use buffer as image source?

I've got code along the lines of the following which generates a new image out of some existing images.
from PIL import Image as pyImage
def create_compound_image(back_image_path, fore_image_path, fore_x_position):
back_image_size = get_image_size(back_image_path)
fore_image_size = get_image_size(fore_image_path)
new_image_width = (fore_image_size[0] / 2) + back_image_size[0]
new_image_height = fore_image_size[1] + back_image_size[1]
new_image = create_new_image_canvas(new_image_width, new_image_height)
back_image = pyImage.open(back_image_path)
fore_image = pyImage.open(fore_image_path)
new_image.paste(back_image, (0, 0), mask = None)
new_image.paste(fore_image, (fore_x_position, back_image_size[1]), mask = None)
return new_image
Later in the code, I've got something like this:
from kivy.uix.image import Image
img = Image(source = create_compound_image(...))
If I do the above, I get the message that Image.source only accepts string/unicode.
If I create a StringIO.StringIO() object from the new image, and try to use that as the source, the error message is the same as above. If I use the output of the StringIO object's getvalue() method as the source, the message is that the source must be encoded string without NULL bytes, not str.
What is the proper way to use the output of the create_compound_image() function as the source when creating a kivy Image object?
It seems you want to just combine two images into one, you can actually just create a texture using Texture.create and blit the data to a particular pos using Texture.blit_buffer .
from kivy.core.image import Image
from kivy.graphics import Texture
bkimg = Image(bk_img_path)
frimg = Image(fr_img_path)
new_size = ((frimg.texture.size[0]/2) + bkimg.texture.size[0],
frimg.texture.size[1] + bkimg.texture.size[1])
tex = Texture.create(size=new_size)
tex.blit_buffer(pbuffer=bkimg.texture.pixels, pos=(0, 0), size=bkimg.texture.size)
tex.blit_buffer(pbuffer=frimg.texture.pixels, pos=(fore_x_position, bkimg.texture.size[1]), size=frimg.texture.size)
Now you can use this texture anywhere directly like::
from kivy.uix.image import Image
image = Image()
image.texture = tex
source is a StringProperty and is expecting a path to file. That's why you got errors when you tried to pass PIL.Image object, StringIO object or string representation of image. It's not what framework wants. As for getting image from StringIO, it was discussed before here:
https://groups.google.com/forum/#!topic/kivy-users/l-3FJ2mA3qI
https://github.com/kivy/kivy/issues/684
You can also try much simpler, quick and dirty method - just save your image as a tmp file and read it normal way.

Resources