How to crop an image in Julia? - julia

I current have some images which contain a border that I need to remove so that I can use those images for a computer vision ML project. How can I use Julia to crop the images and remove the border?

In Julia, you can do the following:
using Images
img_source = testimage("lighthouse")
# where testimage is function from the `TestImages` package.
# You can then do the following:
new_img = img_source[100:200, 100:200]
where img_source[y1:y2, x1:x2] represents the coordinates of the subsection of the original image you want to use. You can read more about this in the Julia Images docs.

Related

How to resize an image in Julia?

I am trying to use some pre-trained computer vision models which require images of a certain dimension. I need to re-size my custom dataset to be a certain dimension. How can I do this with Julia?
In Julia, you can use the imresize function as follows:
using Images
img_square = imresize(img_source, (224, 224))
where img_source is the original image which you want to resize and 224x224 is the new image size. You can find out more about the resizing operations in the Julia Images docs.

R: importing and saving SVG graphics

I have a complex task: to merge existing SVG image with barplots in R. I want to save the output file as a vector graphics as well. So I create the layout, in one of the subplots I create the barplot and now:
How can I load an existing SVG from the hard drive and then put it into the plot? I have tried grimport and grimport2 libraries but they fail to read my SVG. How should I prepare it? It is a simple sketch made in Inkscape, should I save it in any special way?
I'd prefer to use a library that is supported by Anaconda Cloud.
EDIT:
I menaged to read the .ps file with grimport and convert it to a picture object - it was crashing previously because I had a text box with non-standard font in the SVG and the library could not ready that properly (some encoding problems).
Now I am just looking for a way to put the Picture object on the layout, just as if I would use plot(runif(10), runif(10)) to have a scatterplot
grid.picture(picture_object[-1],x=x_coord,y=y_coord)
With the variables x_coord and y_coord I can manipulate the position of the image.

How to enlarge map/plot in R?

I plotted a map in R but when I export it the size is very small. How can I enlarge the map and still save it as a picture? (I know that I can save it as PDF and then it's a vector graphic - but I need to copy it to Power Point and also need a transparent background - I don't think that is possible with a PDF isn't it?)
As you can see here the map is way to small to use it in a Power Point slide:
If someone knows a good way to save it as vector graph that I can easily use in Power Point that would be perfect as well.
The png() function lets you specify width and height size in the default resolution of "px" and the defaults are 480 and 480. You can also supply a res argument in units of "ppi". If you have text you probably ought to specify point size >= 20 for legibility. I generally save my graphs as PDF and convert to PNG with an external program. However, the latest versions of PowerPoint will accept pdf formats. It is also possible to save as an .eps format.

How do I export a higher resolution image of a Mathematica Graph object?

How do I export a re-sized version of the output I get from a call to GraphPlot
(or TreePlot if they produce different output) to a jpg file?
Currently, I'm simply calling Export[file_name, G]
where G is the result from a call to something like GraphPlot.
I'm using Microsoft office picture manager to view the jpgs,
but re-scaling them there yields unsatisfactory results due to poor resolution
(the graph I'm trying to plot has strings as labels which can't be made out after rescaling this way). I would like to be able to choose the size/resolution of the rendered jpg.
As Simon already pointed out, don't use a raster-format for a vector-graphics. Instead, export you plot to e.g. a scalable vector graphics:
graph = GraphPlot[ExampleData[{"Matrix", "HB/can_292"}, "Matrix"]];
Export["graph.svg", graph]
The advantage is, that in such an image, you can still adjust and change lines, polygons and colors. And finally, you can export it to an image of arbitrary quality easily.
And remember, for Plots which contain lines, polygons, ... everything with sharp edges you should never use jpg. General speaking, this is a format for photographs only since its compression is made for reducing filesize in natural images. In those images you don't recognize the compression, in images with text, lines and polygons you certainly will notice the artefacts. Use png if possible. Take your browser and zoom into the above image.
You can set both image size and compression level of the exported file by doing something like
Export[file_name, G, ImageSize -> 1200, "CompressionLevel" -> 0]
The best way I find is to use ImageResolution property. It Increases the resolution of exported image but does not change the scale. Use it like this:
Export[ "image-file-name.png", image, ImageResolution -> 500 ]
Set the size of your graph before exporting it:
Graph[theGraph, ImageSize->2000]

Qt and exporting vector images

I am using QPainter to draw some graphs on the screen and save these graphs as a raster images.
Is it possible to use the same logic I used for rasterizing these graphs to have them exported to vector images that I can use in a PDF?
You can use a QSvgGenerator as a "backend" to your QPainter, and you'll get an SVG document out of it. (The documentation has an example for how to do that.)
I don't know if you can use that directly in PDFs, but there are converters out there if you need a different vector graphics format.

Resources