How to get the properties of an image in Julia - julia

I am trying to figure out the dimensions of an image along with its type in a script so I can pre-set the length and width of my plot window. How can I do that using Julia?

In Julia images are simply arrays. Just use size(img) full code below:
using Images
#requires using Pkg;pkg"add ImageMagikck"
img = load("photo.jpg")
Now get the size:
julia> typeof(img)
Array{RGB{Normed{UInt8,8}},2}
julia> size(img)
(2222, 2396)
If you want to display the image:
using ImageView
imshow(img)

Related

How do I convert a Color Image to Black and White using ImageMagick?

Background: I performed an Indexed conversion in Gimp of a color image and the result was a nice B&W version of the source Image.
I have tried numerous options in ImageMagick to no avail. I can get close but never quite as clear and crisp as what gimp seems to do effortlessly.
Here is my source:
var bmp = new MagickImage(sourceImage);
bmp.Threshold(new ImageMagick.Percentage(60));
bmp.Resample(200, 200);
bmp.ColorType = ColorType.Bilevel;
bmp.BitDepth(1);
bmp.Settings.Compression = CompressionMethod.Group4;
bmp.Strip();
bmp.Format = MagickFormat.Tiff;
I have been adjusting the Threshold call and have tried various suggestions I have seen online with varying amounts of success.
magickimage has a feature called -monochrome but I have not found how that is achieved in the .net library.
I am sure this is possible but what is the best way to achieve a nice B&W conversion.
With ImageMagick 7, you can do Otsu thresholding.
Input:
magick check.png -alpha off -auto-threshold otsu x.png
Result:
There is no built-in equivalent in ImageMagick 6. However I have a script, otsuthresh that will do that At my web site
So in ImageMagick 6, you just have to do simple thresholding.
convert check.png -alpha off -threshold 50% y.png
Result:
I note that your input image has an opaque alpha channel which needs to be removed to get proper results.
This is possibly the simplest way you can use Greyscale in Magick.NET.
MagickImage image = new MagickImage(imagePath);
image.Grayscale();
string fileName = image.FileName + "_grey.png";
image.Write(fileName);
Instead of image.FileName you can also directly use the image path if you have it.
Optionally you can add a PixelIntensityMethod in Greyscale for eventually better results.
Don't forget to call image.RePage() when you want to crop the image.
Otsu's method will perform better when dealing with texts over a background with slightly gradient colors:
How do I convert a Color Image to Black and White using ImageMagick?
According to #fmw42 's comparison on multiple threshold methods:
the Local Adaptive(its equivalent in imagemagick is -lat) algorithm might work best on texts over background:
Also try to combine with connected components processing that was introduced in another answer made by #fmw42 to remove unnecessary edges/dots which might get confused by any further OCR process.

How to load raw binary array data into Julia and display it?

I would like to load a raw image data (such as the .raw ones from http://eeweb.poly.edu/~yao/EL5123/SampleData.html) into Julia and display them.
Basically, I am looking for a way to load file into Julia as Array{xxx} type.
Any ideas?
Here is the code and along with the resulting plot:
using Plots, Images, HTTP
r = HTTP.request("GET", "http://eeweb.poly.edu/%7Eyao/EL5123/image/lena_gray.raw")
img = reshape(r.body, 512, 512)
v = rotr90(colorview(Gray, img./256));
Plots.plot(v)
savefig("lena.png")

bokeh: How to export a grid to png with given size?

I prepared some bokeh plots to display as html.
To this end I prepared a gridplot containing the subplots, the legends and some headings. This all displays extremely nice in HTML and with sizing_mode='stretch_width' it's even kind of responsive.
webpage = gridplot([[header_col],[panel_grid_plot]], toolbar_location=None, sizing_mode='stretch_width')
show(webpage)
Now I also want to export this "webpage" to a PNG. To this end, I use
export_png(webpage, filename= png_filename, width=1800)
Unfortunately, the width parameter is ignored as the webpage is an object of type gridbox and not of type Plot. (This is checked in the bokeh/io/export.py in the method def get_layout_html())
The output is a png of a width of 800px which is kind of useless as the actual information is crushed (while the legends are nicely scaled):
Any ideas how to set the width of my PNG export to useful values?
Is there a way to convert a gridboxto a Plot?
Thanx!
You should've received a warning saying that the width argument will be ignored since you're passing into export_png something that's not a plot.
A way of achieving what you want:
webpage = gridplot(..., sizing_mode='stretch_width')
webpage.width = 1800
export_png(webpage)

set default device (png) with default height and width

I'm setting the default device to PNG options(device="png").
For one plot, I can make a PNG in R with particular dimensions using png(...):
v <- 1:10
png("squared.png", width = 960, height = 480)
plot(v, v**2)
dev.off()
But I want to set the default height/width (just like I'm setting the default device) so that all plots come out with my desired height and width.
Ah, this is what I'm looking for:
options(device = function() png(width = 960))
The device argument should be
a character string giving the name of a function, or the function object itself, which when called creates a new graphics device of the default type for that session...
Using a function instead of the string "png" gives me the flexibility I need.
Why don't you re-define png? If you type png into the console, R will display the function code. You can copy and paste it into an R script, changing its defaults. Then, the autocomplete of the function arguments will still work.

convert textGrob to imageGrob/rasterGrob?

Apologies if this is very straightforward. Actually I hope it will be!
I am trying to dynamically create images from text which I can then resize and plot (either stretched or squashed) to produce a motif-type graph.
I started out using images (which I'd generated using png() and ggplot()) and plotting them as annotation_custom()
require(ggplot2)
require(grid)
require(gridExtra)
qplot(c(0,10),c(0,10)) +
annotation_custom(rasterGrob(image=readPNG("1999.png"),x=0,y=0,height=1,width=1,just=c("left","bottom")),
xmin=0,xmax=5,ymin=0,ymax=7.5)
to produce:
This is fine, but it's awkward to create the images dynamically if they are not the same size, using png(), plus it's clunky to persist them to file, so I tried to see if I could use a textGrob:
myText<-"1000"
myTextGrob<-textGrob(myText,just=c("left","bottom"),gp=gpar(fontsize="100",col="red",fontfamily="Showcard Gothic"))
qplot(c(0,10),c(0,10))+annotation_custom(myTextGrob,0,0,0,0)
and got this, which is fine, except....
...it doesn't seem possible to stretch & skew it in the same way as a rasterGrob so my question is - is it possible to create the textGrob and coerce it to a rasterGrob? Or is there another solution which will let me skew/stretch the textGrob?
Thanks in advance!
It doesn't seem easy without creating temporary files. Instead of raster files, you might use vector paths with the grImport package. There are two options to import text,
as a path; it works (example below), but there's no obvious way to bypass the ps to xml conversion step with intermediate files
as a text string; the xml is much shorter in this case, and could be created directly from R, but unfortunately I couldn't find a way to transform the two axes independently.
library(grImport)
scale_text <- function(text="hello world", scale=4, tmp=tempfile()){
tmp.ps <- paste0(tmp, ".ps")
tmp.xml <- paste0(tmp, ".xml")
string.ps <- paste0('%!PS
/Courier % name the desired font
20 selectfont % choose the size in points and establish
% the font as the current one
1 ', scale, ' scale % scale axis
72 500 moveto % position the current point at
% coordinates 72, 500 (the origin is at the
% lower-left corner of the page)
(', text, ') show % stroke the text in parentheses
showpage % print all on the page
')
cat(string.ps, file=tmp.ps)
PostScriptTrace(tmp.ps, tmp.xml)
readPicture(tmp.xml)
}
hello <- scale_text()
grid.newpage()
grid.picture(hello)

Resources