Jump to content

Create white images instead of black depending of a value.


 Share

Recommended Posts

Hello from Barcelona.

I'm trying do stickers to identify some metal pieces at my work. We use the metal pieces because they have a hole in the middle, so if our products are bigger than that hole they will not pass thought it.

(I don't know how to call it in English).

Posted Image

Our idea is to create some images, print them as stickers ans then identify the metal pieces like that:

Posted Image

(the number of the sticker is expressed in milimeters).

I have created an excel book with all the metal pieces that we have, their inner diameter, their outer diameter and their tall:

Posted Image

So in colums E and G I got the needed size for every sticker (in centimeters).

I have also created (with the help of the examples posted in the forum) a little script that reads the Excel book and creates the stickers with the correct size as png images... but it always creates black images.

I would like to create the stickers in a colour-code, so when one of the metal pieces has an inner diameter that it's second decimal number is zero it has a white sticker and when the second decimal is different to zero then I can create a black sticker.

Something like this:

Posted Image and Posted Image

My little script is this one:

#include <Excel.au3>
#include <Array.au3>
#include <File.au3>
#include <GDIPlus.au3>


_GDIPlus_Startup()

$oExcel = _ExcelBookOpen(@ScriptDir & "\Casquillos.xlsx", 0)
Sleep(200)
$aArray = $oExcel.Activesheet.Range("B3:B185").Value
Sleep(200)
Global $namenumber = 3 ; number of the estarting row to read in the excel file

If IsArray($aArray) Then
ProgressOn("Creando Imágenes", "Creando ...")
For $i = 1 To 185 ;I can't use $aArray[0], it marks an error.

Sleep(20)

$name0 = _ExcelReadCell($oExcel, $namenumber, 2) ;I read the row with the inner diameter to get the name of the image file

$name = StringFormat("%.2f",$name0)
Sleep(20)
$width = _ExcelReadCell($oExcel, $namenumber, 5);I read the row with the needed sticker height to get the widht of the image file
Sleep(20)
$height = _ExcelReadCell($oExcel, $namenumber, 7);I read the row with the needed sticker height to get the widht of the image file

;create blank image of height and width

$newimage = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $width * 37, "int", $height * 37, "int", 0, "int", 0x00022009, "ptr", 0, "int*", 0) ;I do $width * 37 to pass from centimeters to pixels using my current images resolution
Sleep(20)
$newimage = $newimage[6]
Sleep(20)
$CLSID = _GDIPlus_EncodersGetCLSID("PNG")
Sleep(20)
_GDIPlus_ImageSaveToFileEx($newimage, @ScriptDir & "\" & $name & ".png", $CLSID)
Sleep(20)
_GDIPlus_BitmapDispose($newimage)
Sleep(20)
$namenumber = $namenumber + 1
ProgressSet(Int($i * 100 / 185), $name)
Next
ProgressOff()
_GDIPlus_Shutdown()
_ExcelBookClose($oExcel, 0)
Else
MsgBox(262160, "Error", "No he podido leer el fichero Casquillos.xlsx", 5)
_ExcelBookClose($oExcel, 0)
Exit
EndIf

But I don't know how to create directly white images intead of black ones (I know that there are examples to invert colors of images after being created).

I would also like to answer if is possible to directly write the numers in the stickers (like a watermark maybe?)

Greets from Barcelona

Casquillos.xlsx

Edited by adolfito121
Link to comment
Share on other sites

I have partially solved the issue of creating the black and white images depending on if the last number of the digit is a zero or not and thanks to FreeImage.au3 (I invert the color of the image after creating it).

This is the code for anyone interested:

#include <Excel.au3>
#include <Array.au3>
#include <File.au3>
#include <GDIPlus.au3>
#include <FreeImage.au3>
#include <Write Text on Bitmap.au3>

_GDIPlus_Startup()

$oExcel = _ExcelBookOpen(@ScriptDir & "\Casquillos.xlsx", 0)
Sleep(200)
$aArray = $oExcel.Activesheet.Range("B3:B185").Value
Sleep(200)
Global $namenumber = 3 ; number of the estarting row to read in the excel file

If IsArray($aArray) Then
ProgressOn("Creando Imágenes", "Creando ...")
For $i = 1 To 185 ;I can't use $aArray[0], it marks an error.

Sleep(20)

$name0 = _ExcelReadCell($oExcel, $namenumber, 2) ;I read the row with the inner diameter to get the name of the image file

$name = StringFormat("%.2f", $name0)
Sleep(20)
$width = _ExcelReadCell($oExcel, $namenumber, 5);I read the row with the needed sticker height to get the widht of the image file
Sleep(20)
$height = _ExcelReadCell($oExcel, $namenumber, 7);I read the row with the needed sticker height to get the widht of the image file

;create blank image of height and width

$String = StringRight($name, 1)
If $String = 0 Then
$newimage = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $width * 37, "int", $height * 37, "int", 0, "int", 0x00022009, "ptr", 0, "int*", 0) ;I do $width * 37 to pass from centimeters to pixels using my current images resolution
Sleep(20)
;$newimage = $newimage[6]
$newimage = $newimage[6]
Sleep(20)
$CLSID = _GDIPlus_EncodersGetCLSID("BMP")
Sleep(20)
_GDIPlus_ImageSaveToFileEx($newimage, @ScriptDir & "\" & $name & ".bmp", $CLSID)
Sleep(20)
_GDIPlus_BitmapDispose($newimage)
Sleep(20)

_FreeImage_LoadDLL(@ScriptDir & "\FreeImage.dll")
_FreeImage_Initialise()

$sFile = @ScriptDir & "\" & $name & ".bmp"
$FIF = _FreeImage_GetFileTypeU($sFile)
If $FIF = $FIF_UNKNOWN Then
$FIF = _FreeImage_GetFIFFromFilenameU($sFile)
EndIf
$ImageHandle = _FreeImage_LoadU($FIF, $sFile)
_FreeImage_Invert($ImageHandle)
_FreeImage_SaveU($FIF, $ImageHandle, $sFile)
_FreeImage_Unload($ImageHandle)
_FreeImage_DeInitialise()
$tamanyfont = ($width * 37);I divide the width of the image for the number of charachters to write to get an idea of a font size that can fit as much as possible
$tamanyfont2 = StringLen($name)
$tamanyfont3 = $tamanyfont / $tamanyfont2
$tamanyfont4 = Round($tamanyfont3,0)
$Prueba = WTOB($sFile,$name,"Arial",$tamanyfont4,-1,-1,0,0, 0x00000000,1,1)
Else
$newimage = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $width * 37, "int", $height * 37, "int", 0, "int", 0x00022009, "ptr", 0, "int*", 0) ;I do $width * 37 to pass from centimeters to pixels using my current images resolution
Sleep(20)
;$newimage = $newimage[6]
$newimage = $newimage[6]
Sleep(20)
$CLSID = _GDIPlus_EncodersGetCLSID("BMP")
Sleep(20)
_GDIPlus_ImageSaveToFileEx($newimage, @ScriptDir & "\" & $name & ".bmp", $CLSID)
Sleep(20)
_GDIPlus_BitmapDispose($newimage)
Sleep(20)
$sFile = @ScriptDir & "\" & $name & ".bmp"
$tamanyfont = $width * 37
$tamanyfont2 = StringLen($name)
$tamanyfont3 = $tamanyfont / $tamanyfont2
$tamanyfont4 = Round($tamanyfont3,0)
$Prueba = WTOB($sFile,$name,"Arial",$tamanyfont4,-1,-1,0,0, 0xFFFFFFFF,1,1)
EndIf
$namenumber = $namenumber + 1
ProgressSet(Int($i * 100 / 185), $name)

Next
ProgressOff()
_GDIPlus_Shutdown()
_ExcelBookClose($oExcel, 0)
Else
MsgBox(262160, "Error", "No he podido leer el fichero Casquillos.xlsx", 5)
_ExcelBookClose($oExcel, 0)
Exit
EndIf

I'm trying to directly write in the bmp's files using an UEZ udf (Write Text on Bitmap.au3).

But at the moment the program only creates the images without any text inside them. I don't know if is a good idea to try to find the best font size just doing width/characters to write, but even giving a fix font size it doesn't works :(

Greets from Barcelona

Edited by adolfito121
Link to comment
Share on other sites

Try this:

#include <Excel.au3>
#include <Array.au3>
#include <File.au3>
#include <GDIPlus.au3>


_GDIPlus_Startup()

$oExcel = _ExcelBookOpen(@ScriptDir & "\Casquillos.xlsx", 0)

$iRange = 10
$aArray = $oExcel.Activesheet.Range("B3:B185").Value
Global $namenumber = 3 ; number of the estarting row to read in the excel file
Global $hBrush, $hFormat, $hFamily, $hFont, $hGfxContext

If IsArray($aArray) Then
    ProgressOn("Creando Imágenes", "Creando ...")
    $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    For $i = 1 To $iRange ;I can't use $aArray[0], it marks an error.
        $name0 = _ExcelReadCell($oExcel, $namenumber, 2) ;I read the row with the inner diameter to get the name of the image file
        $name = StringFormat("%.2f", $name0)
        $width = _ExcelReadCell($oExcel, $namenumber, 5);I read the row with the needed sticker height to get the widht of the image file
        $height = _ExcelReadCell($oExcel, $namenumber, 7);I read the row with the needed sticker height to get the widht of the image file
        $iW = $width * 37
        $iH = $height * 37
        ;create blank image of height and width

        $newimage = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iW, "int", $iH, "int", 0, "int", $GDIP_PXF32ARGB, "ptr", 0, "int*", 0) ;I do $width * 37 to pass from centimeters to pixels using my current images resolution
        $newimage = $newimage[6]
        $hGfxContext = _GDIPlus_ImageGetGraphicsContext($newimage)
        _GDIPlus_GraphicsClear($hGfxContext, 0xFFFFFFFF)
        _GDIPlus_GraphicsSetSmoothingMode($hGfxContext, 2)
        DllCall($ghGDIPDll, "uint", "GdipSetTextRenderingHint", "handle", $hGfxContext, "int", 4)
        $hFont = _GDIPlus_FontCreate($hFamily, $iW / (1 + StringLen($name)))
        $tLayout = _GDIPlus_RectFCreate(0, 0, $iW, $iH)
        _GDIPlus_GraphicsDrawStringEx($hGfxContext, $name, $hFont, $tLayout, $hFormat, $hBrush)
        _GDIPlus_ImageSaveToFile($newimage, @ScriptDir & "\" & $name & ".png")
        _GDIPlus_FontDispose($hFont)
        _GDIPlus_GraphicsDispose($hGfxContext)
        _GDIPlus_BitmapDispose($newimage)
        $tLayout = 0
        $namenumber = $namenumber + 1
        ProgressSet(Int($i * 100 / 185), $name)
    Next
    ProgressOff()

    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_Shutdown()
    _ExcelBookClose($oExcel, 0)
Else
    MsgBox(262160, "Error", "No he podido leer el fichero Casquillos.xlsx", 5)
    _ExcelBookClose($oExcel, 0)
EndIf
Exit

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...