Replace "box" in image with text - image-processing

I have an image like:
image
Where the yellow box (CODICEFISCALE) is a field that must to be replaced by stored text in the database and the green box (firma) must to be replaced with another image of the user's signature.
How can I do that?
I am open to use any possible solution.
Update:
My question is only about replace the boxes (always the same) in the images (the images can be different).

Related

how to select and manipulate a portion of text inside a text layer using python-fu

I wrote a gimp python plugin to create an image, add a text layer, and write some text into it. Similar to this one.
I want to apply a different formatting to a portion of this text but I can't find the function to do it when I browse Filters > Script-Fu > Console > Browser.
This gif illustrates what I want. It was done manually:
Very complicated. You can get the info in the "parasite" attached to the text layer:
data=layer.parasite_find('gimp-text-layer').data
So for instance, for an image like this:
Data looks like this:
(markup "<markup>SO<span font=\"URW Bookman L Bold\">ME</span> <span size=\"15728\">TE</span>XT</markup>")
(font "Bungee")
(font-size 80)
(font-size-unit pixels)
(antialias yes)
(language "en")
(base-direction ltr)
(color (color-rgb 0 0 0))
(justify fill)
(box-mode dynamic)
(box-unit pixels)
(hinting yes)
Markup is possibly related to Cairo (which is the library used by Gimp to draw text and curves). However,
The parasite is only available once the image has been saved to disk
I have never checked what happens if you update the parasite.
Edited:
It seems creating or updating the parasite doesn't work and Gimp seems to ignored the parasite. It only considers it when loading the file, and when the file is saved, it generates a new parasite from the actual layer contents.
It may be easier for you to format several individual text layers side-by-side. To align them to the same baseline: if you use pdb.gimp_text_get_extents_fontname() with a character that has a flat bottom (I typically use "X") the "ascent" is the distance of the baseline from the top of the layer (round character such as "O" can extent slightly under the baseline).

Convert text to image breaking the text down into multiple columns when it exceedes the defined dimensions

I have seen this thread, but I wish to convert, say poetry, into a wallpaper. (something more advanced I suppose)
Poetry is usually long, and in order to fit it in one wallpaper (fullHD), there should be some way to break it down into columns, creating a new column, once the height of the image was exceeded.
Is there a way to do this via ImageMagick?
Or should I edit the text manually, namely:
figure out the proper height of the text and how much fits in,
break down the text file into multiple text files where appropriate,
create an image using each of those text files taking into account the proper gap between columns.

How do I change text in a python-fu gimp script without changing the font?

I have a master image which I edit in gimp to get the look and feel wanted. I then want to use a python script to produce a bunch of new images with the text (on several different layers) changed. I would like to leave the font, size, italicized or not, etc. alone.
I've successfully changed the text in my batch script with this function:
pdb.gimp_text_layer_set_text(layer, text)
The problem is this also overwrites the font and other text parameters that I had picked out in the master file. Is there a way to change just the text and leave the font alone?
Alternately, a more clunky way would be to try and save everything important about the font before the change and try to reapply it. This is what I've tried:
# Find the text layer
text1_layer = filter(lambda x: x.name == 'text1', im.layers)[0]
# Save the font
font = pdb.gimp_text_layer_get_font(text1_layer)
font_size, font_unit = pdb.gimp_text_layer_get_font_size(text1_layer)
# Set the text
pdb.gimp_text_layer_set_text(text1_layer, tex1_text)
# Restore the font
pdb.gimp_text_layer_set_font(text1_layer, font)
pdb.gimp_text_layer_set_font_size(text1_layer, font_size, font_unit)
Unfortunately, that doesn't seem to work consistently. It looks like the get_font and get_font_size commands retrieve the right font for one of the layers, but not for the others. It doesn't seem to preserve italics etc., and I wouldn't expect it to preserve whether or not text is underlined.
A third option would be to hard code in the font. I would need to go through all the text fields, figure out what the font parameters are and hard code them in for each one. Then, if I redesign the master file (which I will do a lot), I have to repeat the process. This shouldn't be necessary.
I've done a bit more research and found a solution that works for me in gimp, however, it sounds like you should probably be using something other than gimp, such as imagemagick, if you're doing much text editing from a script.
The solution is, when you change text in the gimp editor you need to change the font in the tool dialog box, so it's set as a property of the layer, instead of just selecting the text and changing the font in the floating font box that shows up - that changes the font of the text you selected, but leaves the base font of the text layer unchanged.
In turns out that gimp text functionality is pretty poor, and there are lots of complaints surrounding issues like this online. There is some hope for the future. Apparently gimp supports a markup language for text. You can get the markup for your text this way:
pdb.gimp_text_layer_get_markup(layer)
Unfortunately, there is no set_markup function - even though it's been commented on and requested for 3-5 years now, so don't hold your breath. If such a function existed, then the get_markup and set_markup functions would give a script complete control over text. It doesn't exist, however, so if you want to change pieces of text (eg. to add italics to a word), you have to create separate layers. If all your text is to be formatted the same, you can edit it via script or editor, and as long as you set it in the text layer properties (done via the tool box in the editor, and the only way you can change font in a script), then the set_text function in a script will maintain font, etc.
https://bugzilla.gnome.org/show_bug.cgi?id=724101
http://gimpchat.com/viewtopic.php?f=9&t=10101&p=132782&hilit=change+text+markup#p132782
Not really a satisfactory answer, but too big to post as a comment.
Yes, the text layer API wasn't updated to follow the new capabilities. Actually all the info is in a gimp-text-layer "parasite":
parasites=layer.parasite_list()
if parasites and 'gimp-text-layer' in parasites:
data=layer.parasite_find('gimp-text-layer').data
pdb.gimp_message('Text layer "%s": %s' % (layer.name,data))
This parasite doesn't seem to exist until the image has been saved at least once.
However, even though you can replace the parasite data, it doesn't change the text layer, and to make it worse, it seems that Gimp detects the change, assumes that the layer text data is corrupt, and makes the layer a plain bitmap when saving the image.
Now, poring over the source code, there are mentions of a GDynText plugin that you can find on SourceForge and that advertizes itself as:
GIMP Dynamic Text is a GIMP plug-in that works like the text tool but allows you writing multi-line text and made you able of modifying it later as you want (text/font/font size/color/...).
So you could be lucky, or not...

Word Openxml: how to get a text box the right size?

I'm using PHP to generate docx documents from a database. The generated document contains column charts which have labels attached (i.e. user shapes containing textboxes). In an attempt to get the textboxes to accommodate and display all of the text (i.e. it shouldn't be necessary for the user to resize a textbox to see all the text) my code calculates how many characters will fit into 3cm, adds linefeeds to the string as required and tells me how many lines of text are needed. I have:
<a:xfrm xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:off x="1638276" y="1676399"/>
<a:ext cx="1257325" cy="'.(252000 * $labelLeftLines).'"/>
</a:xfrm>
which I believe should give me a text box around 3.5cm wide (extra .5 for the internal padding) and a height of .7cm multiplied by whatever is the value of $labelLeftLines. However, the text box always turns up as 3.cm wide by .86cm high, which only ever displays one line of text.
If I add in 'autofit':
<a:bodyPr xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" vertOverflow="clip" wrap="square" rtlCol="0">
<a:spAutoFit/>
</a:bodyPr>
the generated file looks just the same, though, when I right click on the textbox to inspect the properties, 'autofit' is indeed applied. I have to uncheck it and recheck it to make it affect the textbox.
Any openXML gurus out there?
Hmm, some random floundering around revealed that the values I need to manipulate are here:
<cdr:relSizeAnchor xmlns:cdr="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing">
<cdr:from>
<cdr:x>0.47</cdr:x>
<cdr:y>0.75</cdr:y>
</cdr:from>
<cdr:to>
<cdr:x>0.67</cdr:x>
<cdr:y>1</cdr:y>
</cdr:to>
Changing those values does actually change the size of the texbox, though I haven't a clue what units are being used. From 0.75 to 1 produces a height of 1.43cm.
One day I'll maybe be able to find my way around the doucmentation.

Is there an easy way to cut a slice from an image using Gimp?

Wondering if there is an easy way to remove a rectangular slice across the entire width of an image using Gimp, and have the resulting hole closed up automatically. I hope that makes sense. If I select a slice across an image and do "cut", it leaves a blank "hole" there. I want the new top and bottom of the image to join and fill that hole, reducing the image height by the amount sliced out.
Any easy way to do this?
Here is a method that is quick and often does what you want:
Cut out the middle, leaving a transparent "hole".
Click anywhere to remove the selection (so the hole is not selected).
Click Image > Zealous crop .
This is going to remove the middle part. However, if you also have transparency in other parts of the image (like around the edges) it's going to remove that transparency too.
I believe you're asking to do something like cut out the middle of a page, leaving the header and footer and have the blank space removed with the cut action, effectively joining the header and footer together.
To my knowledge, I don't believe so. Even if you cut, or delete, that space is still part of the image even without content.
But, you would be able to highlight the top or bottom (or left or right) of the remaining space and drag it to align with the other side. It's not ideal for repetitive tasks, but should get you through if you only have to do it a few times.
Install Python and the Python Imaging Library. Back in GIMP, select and cut the full-width areas you don't want to transparent, and export the image to test.png. Then use this Python code (works only if complete lines are transparent; will not work properly if there are 100%-transparent pixels anywhere other than on a full-width row)—
from PIL import Image
i = Image.open("test.png")
b = i.tobytes()
b2 = ''.join(b[n:n+4] for n in xrange(0,len(b),4) if ord(b[n+3]))
newHeight = len(b2)/i.width/4
i2 = Image.frombytes('RGBA',(i.width,newHeight),b2)
i2.save("test.png")
Then re-load test.png and verify that the areas you cut have gone.
In gimp 2.8.1 you can easily create a new image from a selection. So if you select a rectangular than do a copy (Ctrl-C) and a past in a new image
Edit -> Paste as -> new image (or Ctrl-Shift-V).

Resources