Forum > Graphics
Text to images Generator (Console app generating bitmaps with text)
barracuda:
Hello,
I am using mint 20 and my question is about creating series of text for test purposes. I have given the question to chatGPT but he simply has not senseful answer. The problem is like that: I need to create text like "A word". But the image must be the dimentions of the word image plus some 2 or 4px around it. I originally tried to write to program in AHK for Windows XP (old version) and found no support for this old app. Maybe I could try Freepascal. I think even the chatGPT could do that but it has no idea how to solve the problem with measuring of the text of the graphic context which does not exist. And it can not to create the graphic context because he does not know the width and height of the image...
So how to solve this "loop" issue?
As I said, I have tried to write the code in AHK so I will try to explain it on the piece of code of it.
I have fonts and texts I want to create.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---fonts := ["Verdana", "Roboto", "Arial", "Helvetica", "sans-serif"]texts := ["Psalm 91:1", "Slovo", "AHK pro", "Vygenerování zkušebních obrázků.", "Vygenerování zkušebních obrázků.", "Včera ráno, dnes odpoledne a v neděli večer"]fontSize := 22.4 I create dir..
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---If !FileExist("TestImages_22.4px") FileCreateDir, TestImages_22.4px
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Then there is loop for every font.for font in fonts {font := Gdip_FontCreate(font, fontSize) and neste loop for every text
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---for text in texts {Now ChatGPT suggested some shitty code to create image of size 1x1 px - seems nonsense for me
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---image := Gdip_CreateBitmap(1, 1) ; Vytvoření prázdného obrázku pro MeasureStringG := Gdip_GraphicsFromImage(image) G is 0
Or this buggy shi..
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---M := Gdip_MeasureString(0, text, font, 0, &RectF) textWidth := M.width Where it used pointer 0 for Graphic Context.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---and then the map should be created
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---image := Gdip_CreateBitmap(M.Width, M.Height)And the rest of code inside the loop is
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- fileName := font . " " . StrSplit(text, " ")[1] . ".png" pBrush := Gdip_BrushCreateSolid(0x55FFFFFF) ; Černé pozadí Gdip_FillRectangle(G, pBrush, 0, 0, textWidth, y + fontSize) ; Vykreslení pozadípBrush := Gdip_BrushCreateSolid(0xFF000000) ; Černé pozadíGdip_DrawString(G, text, font, textBrush, x, y)Gdip_SaveBitmapToFile(image, fileName)Gdip_DisposeImage(image)Gdip_DeleteBrush(pBrush)Gdip_DeleteStringFormat(hFormat)Gdip_DeleteFont(hFont)Gdip_DeleteFontFamily(hFamily)
I think this should not be hard to do in any programming language and I can create it on linux and copy the files to Windows then.
But what is your idea of solving the problem with measuring of non existing image text?
wp:
Don't waste your time with ChatGPT...
Measuring text size is one of the standard methods of the LCL canvas: Canvas.TextSize(text), the result is a TSize record with the elements CX (width) and CY (height).
The following code snippet creates a bitmap, sets the font, measures the text, adjusts the bitmap size to the text size + some margin, draws the text and saves the bitmap to file.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Types, LCLType, LCLIntf; { TForm1 } procedure TForm1.Button1Click(Sender: TObject);const MARGIN = 3; TXT = 'A word';var bmp: TBitmap; ext: TSize; R: TRect;begin bmp := TBitmap.Create; try // Set the font and measure the text size bmp.Canvas.Font.Name := 'Arial'; bmp.Canvas.Font.Size := 24; bmp.Canvas.Font.Color := clRed; bmp.Canvas.Font.Style := [fsItalic]; R.TopLeft := Point(0, 0); R.BottomRight := TPoint(bmp.Canvas.TextExtent(TXT)); // Set the size of the bitmap (= text size + margin) bmp.SetSize(R.Right + 2*MARGIN, R.Bottom + 2*MARGIN); // Paint the bitmap's background color bmp.Canvas.Brush.Color := clWhite; bmp.Canvas.FillRect(0, 0, bmp.Width, bmp.Height); // Draw the text bmp.Canvas.TextOut(MARGIN, MARGIN, TXT); // Save bitmap to file bmp.SaveToFile('A_Word.bmp'); finally bmp.Free; end;end;
barracuda:
Thank you but I don't remember how to create new program. Should I create new Unit?
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit3; {$mode ObjFPC}{$H+} interface uses ...
dseligo:
--- Quote from: barracuda on October 28, 2023, 11:27:45 am ---Thank you but I don't remember how to create new program. Should I create new Unit?
--- End quote ---
?
https://wiki.freepascal.org/Lazarus_Tutorial#Your_first_Lazarus_Program.21
wp:
--- Quote from: barracuda on October 28, 2023, 11:27:45 am ---Thank you but I don't remember how to create new program. Should I create new Unit?
--- End quote ---
In Lazarus:
* "File" > "New" > "Project" > "Application" > "OK"
* In the component palette click on the icon of TButton (the third one on the "Standard" palette). Then click on the form at the point where you want the button to be.
* Double-click on the button on the form. The code editor appears with an empty ButtonClick procedure. From my previous post, copy the code between ("procedure TForm1.ButtonClick(...)") and "end;" and paste it over the existing empty procedure in your code editor. Add the units Types, LCLType, LCLIntf to the uses clause of the form.
* Press F9 to compile and run
Navigation
[0] Message Index
[#] Next page