Jump to content
GhostLine

Looking for AddPicToPic function

Recommended Posts

GhostLine

Hello guys,

 

I'm looking for a simply usable function like _AddTextToPic() (written here : http://www.autoitscript.com/forum/topic/138197-place-text-in-image-and-save/ ) in order to add a picture into a picture.

I explain : we use sticky printed paper with the logo and adress of each of our clients to identify parcels. Until now, I've used Paint.net and do them myself, but since I've been promoted, I've to let usable tools to my collegue before I go.

That's why I want to make a little interface where the user has only to put the path to the client logo, some texts, and then to click Ok in order to generate the picture.

Can anyone help me on this matter ?

Thanks !

 

Edit : for some reasons, I can't post answers to my posts, so I edit the first :)

I've successfuly adapted some scripts I've found in order to do this ! I can, without installing anything, "paste" texts and images in an existing picture. It's quick and REALLY dirty, but it work :

#include <GDIPlus.au3>
#include <WinAPI.au3>
Example()
_AddTextToPic (@MyDocumentsDir & "\GDIPlus_Image.jpg", @MyDocumentsDir & "\GDIPlus_Imag.jpg", "CODE MAG :", 133, 49)
_AddTextToPic (@MyDocumentsDir & "\GDIPlus_Imag.jpg", @MyDocumentsDir & "\GDIPlus_Image.jpg", "N° CAISSE :", 133, 125)
_AddTextToPic (@MyDocumentsDir & "\GDIPlus_Image.jpg", @MyDocumentsDir & "\GDIPlus_Imag.jpg", "NOM MAGASIN :", 20, 200)
_AddTextToPic (@MyDocumentsDir & "\GDIPlus_Imag.jpg", @MyDocumentsDir & "\GDIPlus_Image.jpg", "CODE MAG :", 556, 49)
_AddTextToPic (@MyDocumentsDir & "\GDIPlus_Image.jpg", @MyDocumentsDir & "\GDIPlus_Imag.jpg", "N° CAISSE :", 556, 125)
_AddTextToPic (@MyDocumentsDir & "\GDIPlus_Imag.jpg", @MyDocumentsDir & "\GDIPlus_Image.jpg", "NOM MAGASIN :", 443, 200)
_AddTextToPic (@MyDocumentsDir & "\GDIPlus_Image.jpg", @MyDocumentsDir & "\GDIPlus_Imag.jpg", "CODE MAG :", 133, 349)
_AddTextToPic (@MyDocumentsDir & "\GDIPlus_Imag.jpg", @MyDocumentsDir & "\GDIPlus_Image.jpg", "N° CAISSE :", 133, 425)
_AddTextToPic (@MyDocumentsDir & "\GDIPlus_Image.jpg", @MyDocumentsDir & "\GDIPlus_Imag.jpg", "NOM MAGASIN :", 20, 500)
_AddTextToPic (@MyDocumentsDir & "\GDIPlus_Imag.jpg", @MyDocumentsDir & "\GDIPlus_Image.jpg", "CODE MAG :", 556, 349)
_AddTextToPic (@MyDocumentsDir & "\GDIPlus_Image.jpg", @MyDocumentsDir & "\GDIPlus_Imag.jpg", "N° CAISSE :", 556, 425)
_AddTextToPic (@MyDocumentsDir & "\GDIPlus_Imag.jpg", @MyDocumentsDir & "\GDIPlus_Image.jpg", "NOM MAGASIN :", 443, 500)
ShellExecute(@MyDocumentsDir & "\GDIPlus_Image.jpg")
Func Example()
    Local $hImage1, $hImage2, $hGraphic
    ; Initialize GDI+ library
    _GDIPlus_Startup()
    $hImage1 = _GDIPlus_ImageLoadFromFile(@ScriptDir&"\image.jpg")
    $hImage2 = _GDIPlus_ImageLoadFromFile("c:\temp\interface.bmp")
    $iScale = 0.55
    $hBitmap_Scaled = _GDIPlus_ImageScale($hImage2, $iScale, $iScale, $GDIP_INTERPOLATIONMODE_NEARESTNEIGHBOR)
    ; Draw one image in another
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap_Scaled, 10, 75)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap_Scaled, 10, 375)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap_Scaled, 435, 75)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap_Scaled, 435, 375)
    ; Save resultant image
    _GDIPlus_ImageSaveToFile($hImage1, @MyDocumentsDir & "\GDIPlus_Image.jpg")
    ; Clean up resources
    _GDIPlus_BitmapDispose($hImage2)
    _GDIPlus_ImageDispose($hImage1)
    ; Shut down GDI+ library
    _GDIPlus_Shutdown()
EndFunc   ;==>Example
Func _AddTextToPic ( $_ImagePath, $_Outputpath, $sString, $_X=0, $_Y=0 )
    Local $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, $hFormat, $aInfo, $hBrush1, $hBrush2, $iWidth, $iHeight, $hPen
    ; Initialize GDI+ library
    _GDIPlus_StartUp ( )
    ; Load image and emboss text
    $hImage   = _GDIPlus_ImageLoadFromFile ( $_ImagePath )
    $hGraphic = _GDIPlus_ImageGetGraphicsContext ( $hImage )
    $hFamily  = _GDIPlus_FontFamilyCreate ( "Calibri" )
    $hFont    = _GDIPlus_FontCreate ( $hFamily, 14, 1 )
    $hFormat  = _GDIPlus_StringFormatCreate ( 0x4000 )
    $hBrush2  = _GDIPlus_BrushCreateSolid ( 0xff000000 )
    $hPen     = _GDIPlus_PenCreate ( 0xC4000000, 1 )
    ; Set Text Pos
    $tLayout  = _GDIPlus_RectFCreate ( $_X, $_Y )
    $aInfo    = _GDIPlus_GraphicsMeasureString ( $hGraphic, $sString, $hFont, $tLayout, $hFormat )
    _GDIPlus_GraphicsDrawStringEx ( $hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush2 )
    ; Save image
    _GDIPlus_ImageSaveToFile ( $hImage, $_Outputpath )
    ; Free resources
    _GDIPlus_PenDispose ( $hPen )
    _GDIPlus_BrushDispose ( $hBrush1 )
    _GDIPlus_BrushDispose ( $hBrush2 )
    _GDIPlus_StringFormatDispose ( $hFormat )
    _GDIPlus_FontDispose ( $hFont )
    _GDIPlus_FontFamilyDispose ( $hFamily )
    _GDIPlus_GraphicsDispose ( $hGraphic )
    _GDIPlus_ImageDispose ( $hImage )
    _GDIPlus_ShutDown ( )
EndFunc ;==> _AddTextToPic ( )

This way, I can include it in a larger program I'm writing right now (as you can guess, printing stickers is only a small part of the whole thing :)).

Edited by GhostLine

Share this post


Link to post
Share on other sites
ViciousXUSMC

I have scripted/automated IrfanView it could do this for you :)

It has a CMD driven interface and batch processing making it easy to just write the proper cmd and wrap it in autoit where you can use inputboxes or a gui to have the user entered data populated to your cmd.

That is a lot of work on your part to look at the program, learn its features, and then write a script but I just wanted to point it out.

 

I wrote a right click context where you right click a folder of images and it will automatically create a folder called "processed images" batch process all images in that folder to shrink them down to a certian size and then add white space to the bottom of the image for text captions.

Since there is a watermark feature you could use that to insert your image into the other image, and then simply make use of some other tools or features for text. 

Share this post


Link to post
Share on other sites
ViciousXUSMC

Since you took the time to look I'll post something I made yesterday in spirit of this thread.

This is another right click, this one is made to right click on the image instead of a folder and what it does is add a caption to the image at the bottom center based on what is entered in the input box.

This is the same script you could use to do image in image the only thing you would change is your irfanview .ini file that contains the "changes" you want to implement to your image.  In my case my .ini only contains "add text" featuers not any watermarks.

My installer that adds the right click option for "text overlay" (Shift RightClick since I only want it to appear when intentionally called)

#RequireAdmin

DirCreate("C:\IT Automation\TextOverlay")

FileInstall("i_view32.ini", "C:\IT Automation\TextOverlay\i_view32.ini", 1)
FileInstall("TextOverlay.exe", "C:\IT Automation\TextOverlay\TextOverlay.exe", 1)
FileInstall("TextOverlay.bat", "C:\IT Automation\TextOverlay\TextOverlay.bat", 1)

RegWrite("HKEY_CLASSES_ROOT\*\shell\Text Overlay")
RegWrite("HKEY_CLASSES_ROOT\*\shell\Text Overlay", "", "REG_SZ", "Text Overlay")
RegWrite("HKEY_CLASSES_ROOT\*\shell\Text Overlay", "Extended", "REG_SZ", "")
RegWrite("HKEY_CLASSES_ROOT\*\shell\Text Overlay", "Icon", "REG_SZ", '"C:\IT Automation\TextOverlay\TextOverlay.exe"')
RegWrite("HKEY_CLASSES_ROOT\*\shell\Text Overlay\command")
RegWrite("HKEY_CLASSES_ROOT\*\shell\Text Overlay\command", "", "REG_SZ", 'C:\Windows\explorer.exe "C:\IT Automation\TextOverlay\TextOverlay.exe"')

MsgBox(0, "Black Magic Automation", "Install is Completed, Shift Right Click Images to use.")

Here is the code for the script itself:

#include <file.au3>

$saveClip = ClipGet()
Send("^c")
Sleep(150)
$filesfolders = ClipGet()
ClipPut($saveClip)

$sText = InputBox("Black Magic Automation", "What Caption Do You Want To Add To This Image?", "Your Caption")

FileCopy("C:\IT Automation\TextOverlay\TextOverlay.bat", @TempDir & "\textoverlay.bat", 1)
FileCopy("C:\IT Automation\TextOverlay\i_view32.ini", @TempDir & "\i_view32.ini", 1)
_ReplaceStringInFile(@TempDir & "\textoverlay.bat", "replace", $filesfolders)
_ReplaceStringInFile(@TempDir & "\i_view32.ini", "replace", $sText)
ShellExecute(@TempDir & "\textoverlay.bat")

 

I am no master of AutoIT so I am sure there are smarter and better ways.  For example most people probably would use Run(@ComSpec) Instead of a .bat file but I personally like to still use an external bat file in some instances, a good reason for this one is I can change the .bat file to change my options without recompiling my script.

So what is happening here is your selected image path is copied to the variable $filesfolders
Then your caption is saved to the variable $sText

We move our base .bat file from the C: to the TempDirectory as well as our .ini file.

The .bat file contains the path to the file to be processed, so we add that path in by replacing the string in the .bat file with what we copied to $filesfolders (via the right click context menu) the .ini contains all the settings to be applied to the image.  In this case we were only applying a caption so I wrote in the .ini a caption "replace" so that we write the contents of the variable $sText to the image.

Here is what my .ini looks like.

[Batch]
AdvCrop=0
AdvCropX=0
AdvCropY=0
AdvCropW=0
AdvCropH=0
AdvCropC=0
AdvResize=0
AdvResizeOpt=1
AdvResizeW=0.00
AdvResizeH=0.00
AdvResizeL=1280.00
AdvResizeS=0.00
AdvResizeMP=0.00
AdvResample=1
AdvResizePerc=0
AdvResizePercW=0.00
AdvResizePercH=0.00
AdvDPI=0
AdvResizeUnit=0
AdvResizeRatio=1
AdvNoEnlarge=0
AdvNoShrink=0
AdvResizeOnDpi=0
AdvCanvas=0
AdvAddText=1
AdvWatermark=0
AdvReplaceColor=0
AdvUseBPP=0
AdvBPP=0
AdvUseFSDither=1
AdvDecrQuality=0
AdvAutoRGB=0
AdvHFlip=0
AdvVFlip=0
AdvRLeft=0
AdvRRight=0
AdvGray=0
AdvInvert=0
AdvSharpen=0
AdvGamma=0
AdvContrast=0
AdvBrightness=0
AdvSaturation=0
AdvColR=0
AdvColG=0
AdvColB=0
AdvSharpenVal=1
AdvGammaVal=0.00
AdvContrastVal=0
AdvBrightnessVal=0
AdvSaturationVal=0
AdvColRVal=0
AdvColGVal=0
AdvColBVal=0
AdvDelOrg=0
AdvOverwrite=1
AdvSubdirs=0
AdvSaveOldDate=0
AdvAllPages=1
UseAdvOptionsOrder=0
AdvFineR=0
AdvFineRVal=0.00
AdvBlur=0
AdvBlurVal=1
AdvMedian=0
AdvMedianVal=3
AdvRbg=0
AdvBgr=0
AdvBrg=0
AdvGrb=0
AdvGbr=0
AdvAutoCrop=0
AdvOptionsOrder=
[BatchText]
AddText=replace
TextCoord=0;15;1200;120;
Corner=2
Orientation=1
TranspText=1
SemiTranspText=0
FitColorW=1
OutlineFill=0
Outline=0
Emboss=0
Shadow=0
FontColor=0
TxtBgkr=16777215
FontParam=-27|0|0|0|400|0|0|0|0|3|2|0|18|
Font=Times New Roman
[Effects]
CanvL=0
CanvR=0
CanvT=0
CanvB=55
CanvInside=0
CanvColor=16777215
CanvMethod=0
CanvW=1920
CanvH=1080
CanvCorner=1
ReplaceColorOld=0
ReplaceColorNew=0
ReplaceColorTol=0

 

I think for the most part this is ready made for what you need, you would just need to configure your watermark options in the .ini and since I wrote this to replace the image you may want to change your .bat so that it creates a copy of the image rather than replace it (similar to the first script where I batch convert an entire folder of images and move a copy to the processed folder)

 

Edit:

And the .bat File

"i_view32.exe" "replace" /ini="%TEMP% /advancedbatch /convert="replace"

 

And one more edit, just tried it I have it working with @ComSpec instead of a .bat file.  In this case we still have a copy of the .ini file due to the nature of editing it each time.

#include <file.au3>

$saveClip = ClipGet()
Send("^c")
Sleep(150)
$filesfolders = ClipGet()
ClipPut($saveClip)

$sText = InputBox("Black Magic Automation", "What Caption Do You Want To Add To This Image?", "Your Caption")

FileCopy("C:\IT Automation\TextOverlay\i_view32.ini", @TempDir & "\i_view32.ini", 1)
_ReplaceStringInFile(@TempDir & "\i_view32.ini", "replace", $sText)
Run(@ComSpec & " /c " & "i_view32.exe " & '"' & $filesfolders & '"' & " /ini=" & @TempDir & " /advancedbatch /convert=" & '"' & $filesfolders & '"', "", @SW_HIDE)

 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites
GhostLine

Woot ! I can write again :) 

Bert : it seem to be a corporate security issue, since it work on "open internet".

 

ViciousXUSMC : thanks a lot for your post !

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×