Find areas to output in pdf file - ruby

Problem: I have a scanned version of some tax report (e.g. https://docs.google.com/file/d/0B3TmJsb1vYhqclFFendvTlRoV28/edit?usp=sharing ), and I must fill it programmly. So, it`s needed to find empty areas and put to it some values in program.
Now I see only one solution - manually find coordinates of these empty areas, code it to program, and output values to these coordinates.
Is it possible to programmly find coordinates (may be by some pattern recognition or something else) of empty areas? May be use of some Ruby tools (e.g. Prawn) ?

So, there isn't complete automatic solution. I've solved this problem by searching areas to input, store its coordinates and then output to these areas

Related

Clear the current path in Cairo

In some Ruby code which uses Cairo, I need to draw a number of fills over a single path. This is part of a graphics application, where multiple fills may be stacked on top of each other using different blend modes.
Each fill is drawn using fill_preserve to preserve the path so that the next fill can occur over the same path without retracing it. This is done somewhat like so:
rectangle_data.each do |rectangle_datum|
context.rectangle(*rectangle_datum.rectangle)
fill_data.each do |fill_datum|
context.set_source_rgba(*fill_datum.color)
context.fill_preserve
end
end
The problem is that this leaves the path even after all fills have taken place, which means that individual shapes are just drawn as one huge filled shape.
To solve this, I'd simply need to clear the current path manually, but I can't figure out how to do this. I've had a look through the documentation for Cairo::Context through Google Translate but I can't find it. (The docs are in Japanese!)
How can I manually clear a Cairo context's current path?
I had a look over Cairo::Context#methods and found #new_path, which does just what I'm after. So you can do:
rectangle_data.each do |rectangle_datum|
context.rectangle(*rectangle_datum.rectangle)
fill_data.each do |fill_datum|
context.set_source_rgba(*fill_datum.color)
context.fill_preserve
end
context.new_path
end

Cropping SVG to range in Inkscape?

Say I have a range – something like a 400x400 rectangle at 60, 60 – which is dynamically generated by a separate program. I'm wondering how it's possible to crop my document to that range in the command line?
Everything I've read has suggested I'd need to add a rectangle to the document, resize the document to that rectangle (resize to selection), and then remove the rectangle.
But I'm having trouble with adding and removing that rectangle. I found the ToolRect verb, but I can't seem to find anything related to actually drawing that rectangle (or removing it).
So, am I doing this wrong or is there just no way to add (and select) the rectangle using only the command line? Using another program is also fine, but I haven't had much luck with that (I couldn't get the python modules installed for the only possibly helpful thing I found..).
In this email discussion from 2012, someone said:
There is no way to pass parameters to verbs (with the current
implementation, they don't take parameters by design).
In case they add this capability later, the required verbs to crop the page would be:
EditSelectAll
SelectionGroup
ToolRect (requires parameters, i.e. where to crop)
EditSelectAll
ObjectSetClipPath
FitCanvasToDrawing
FileVacuum
FileSaveAs (requires a parameter, so that we don't have to overwrite the original)
Since Inkscape can edit any valid SVG, I'd rather look into other available SVG libraries, like this one for Python.
If you are OK with rasterising your image, take a look at this question. Inkscape unfortunately ignores the --export-area option when exporting to svg or pdf.
My – admittedly, unsatisfying – solution was to create a separate program to add a viewbox to the SVG text.
The program I made was implemented into a separate part of my project, so I don't have a good command line version, but if you plan on making one yourself, whatever XML editing library you have for your language of choice should be all you need. I used xmldom for Node.js with relative ease.

Plotting several jpeg images in a single display

I need to plot and display several jpeg images in a single combined display (or canvas?). For example, suppose I have images {a,b,c,d}.jpg, each of different size, and I would like to plot them on one page in a 2x2 grid. It would be also nice to be able to set a title for each subplot.
I've been thoroughly looking for a solution, but couldn't find out how to do it, so any ideas would really help. I would preferably use a solution that is based on the EBImage package.
There are two ways how to arrange several plots with base graph functions, namely par(mfrow=c(rows,columns)) (substitute rows and columns with integers) and layout(mat) where mat is a matrix like matrix(c(1,2,3,4)).
For further info see ?par, ?layout, and especially Quick-R: Combining Plots.
However, as your question is about images I don't know if it helps you at all. If not, I am sorry for misinterpreting your question.
To add to Henriks solution, a rather convenient way of using the par() function is:
jpeg(filename="somefile.jpg")
op <- par(mfrow=c(2,2)
#plot the plots you want
par(op)
dev.off()
This way, you put the parameters back to the old state after you ran the code. Be aware of the fact this is NOT true if one of the plots gave an error.
Be aware of the fact that R always put the plots in the same order. Using mfrow fills the grid row by row. If you use mfcol instead of mfrow in the code, you fill up column by column.
Layout is a whole different story. Here you can define in which order the plots have to be placed. So layout(matrix(1:4,nrow=2) does the same as par(mfcol=c(2,2)). But layout(matrix(c(1,4,3,2),ncol=2)) places the first plot lefttop, the next one rightbottom, the third one righttop, and the last one leftbottom.
Every plot is completely independent, so the titles you specify using the option main are printed as well. If you want to have more flexibility, you should take a look at lattice plots.
If you do not want the images in a regular grid (the different sizes could imply this), then you might consider using the subplot function from the TeachingDemos package. The last example in the help page shows using an image as a plotting character, just modify to use your different images and sizes/locations.
The ms.image function (same package) used with my.symbols is another possibility.

Latex - Is it possible to have text on top of images?

I want to create something like a leaflet/magazine using Latex. Is it possible to place text on top of an image and style the text freely?
Any links to examples of something like this?
I usually do something like
\usepackage{tikz}
...
\begin{tikzpicture}
\draw (0, 0) node[inner sep=0] {\includegraphics[width=4cm]{imagefile.png}};
\draw (1, 1) node {Hello world};
\end{tikzpicture}
A very good toolset for manipulating images is pgf/TikZ pdf doc.
See \pgfimage for examples. It allows to mix text and image freely. There are many ways to do it. One of them is to use layers (p. 220 of pgfmanual). The pgf manual contains many simple examples, and is very precise.
Another solution is to use the lpic package: the homepage contains some examples.
You can find other examples for pfg and TikZ here and here for many impressive examples.
You can also define the text after the image and then offset it using negative vertical space. Because it follows after the image in the LaTeX source, it will be drawn on top of the image instead of underneath it.
\includegraphics[...]{...}
\vspace*{-20ex} % Tune this to the image height.
\begin{center}
Text
\end{center}
\vspace*{20ex} % The spacing above but without the minus.
Another solution is the textpos package which allows you to specify boxes at absolute positions on the page. The boxes can overlap, so you can put the figure in one box, and text in another box on top of it.
I realise that the question is old and answer is accepted, but for completeness would like to propose an alternative approach for making leaflets in LaTeX.
Specifically, the leaflet document style served me really well for this purpose.
A good blog post wrt background image can be found here. There are a couple of packages that are required to use in order to make the proposed approach work, but were not mentioned in the blog post:
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{transparent}
As already suggested, you can annotate the different parts of the figure using TikZ. However, sometimes it might even better to use numbers to reference the different parts and explain them in the figure caption.
To easily get the precise relative positions (which is often tedious) and to generate LaTeX code automatically, you could use the new web-based LaTeX Overlay Generator, which I built for such cases. This is just a small interactive tool, which helps you to find the right locations.
Another way to do this, admittedly not using LaTeX, would simply be to edit the image with photoshop or gimp or something like that. I guess your option in terms of typesetting mathematics might be fairly limited doing it this way.
Otherwise I'd endorse using tikz.
Are you sure you want to do it in LaTeX? Desktop publishing software might be more suited to your needs... Something like scribus might be easier than tikz in terms of learning curve, depending on what you want to do.
I'd like to add on to #midtiby's answer...
You can also specify the text position using a relative position, like so:
\usepackage{tikz}
...
\begin{tikzpicture}
\node[inner sep=0] (image) at (0,0) {\includegraphics[width=4cm]{imagefile.png}};
\node[above=0 of image] {Hello world};
\end{tikzpicture}

Automatic selection of control points in Matlab

Is there a way to select the control points automatically in Matlab instead of manually selecting them by cpselect? Thank you very much.
I just recently worked on a project where I had to do the same thing -- eventually I found that you can select control points automatically, but only if you use automatic selection to find the control points for both the unregistered and the orthophoto. (The control points used to define image transforms are stored in matrices, so if you can get your automated system to output a set of point coordinates in matrix form you can pass them to straight to cp2tform and bypass cpselect entirely.) On the other hand, cpselect stores corresponding pairs of image points using some kind of special data structure, so that I was never able to just pass it a set of control points for one image while leaving the other image blank.
I don't have the Matlab Image Processing Toolbox, but I see from the documentation that cpeselect can be called with an argument specifying the initial set of control points. Can you reduce your task to automating the creation of that initial set?

Resources