Page 1 sur 1
[..] Modifier une image bmp
Posté : mer. 07 mai 2008 15:04
par tolf
Bonjour,
J'ai un fichier .bmp et je voudrais faire un script qui puisse transformer tous les pixels de couleur 0xff0000 en 0x00ff00 et d'enregistrer le nouveau fichier.
Ma question est :
- Existe-t-il une fonction qui puisse lire le fichier et retourner les pixels sous une certaine forme (array, string, ou autre) ?
- Existe-t-il une fonction qui permet d'enregistrer à nouveau le fichier à partir de ce qui a été retourné par la première fonction (et que j'aurai modifié au préalable) ?
Merci de votre aide
Re: [..] Modifier une image bmp
Posté : mer. 07 mai 2008 21:34
par Tlem
J'ai jeté un oeil dans la doc, et rien vu de tel.
Par contre peut être certaines nouvelles fonction GDI peuvent aider, mais je vois pas trop comment.
Re: [..] Modifier une image bmp
Posté : jeu. 08 mai 2008 16:34
par tolf
J'ai trouvé les fonctions suivantes :
_GDIPlus_ImageLoadFromFile, qui retourne un "handle to an image object".
_GDIPlus_ImageSaveToFile, qui enregistre une image à partir d'un "handle to an image object".
Par contre, pour modifier l'image, je n'ai pas trouvé de fonction, notamment pour ce qui est de lire la couleur des pixels.
Re: [..] Modifier une image bmp
Posté : jeu. 08 mai 2008 17:25
par Tlem
Je dirais qu'il faut sans doute faire une boucle sur la surface totale du bitmap, et utiliser la fonction :
pour récupérer la couleur de chaque pixel et utiliser la fonction
pour faire le remplacement de couleur.
Je n'ai rien essayé, mais ça me parait logique.

Re: [..] Modifier une image bmp
Posté : ven. 09 mai 2008 15:10
par tolf
oui mais
retourne la couleur des pixels de l'écran

, et pas de ceux du "handle to an image object".
Re: [..] Modifier une image bmp
Posté : ven. 09 mai 2008 15:21
par tolf
J'ai essayé ce bout de code :
Code : Tout sélectionner
:#include <GUIConstants.au3>
#include <GDIPlus.au3>
$fichier = FileOpenDialog("Choisissez une image bitmap", @ScriptDir, "Fichiers bitmap (*.bmp)")
_GDIPlus_Startup()
$himage = _GDIPlus_ImageLoadFromFile($fichier)
GUICtrlSetGraphic($himage, $GUI_GR_COLOR, 0xff0000)
For $x = 1 To _GDIPlus_ImageGetWidth($himage)
For $y = 1 To _GDIPlus_ImageGetHeight($himage)
GUICtrlSetGraphic($himage, $GUI_GR_PIXEL, $x, $y)
Next
TrayTip($fichier, "col : " & $x, 5) ; permet de tester la colonne en cours
Next
_GDIPlus_ImageSaveToFile($himage ,StringTrimRight($fichier, 4) & "2.bmp")
_GDIPlus_ShutDown()
mais l'image de sortie est identique à celle de départ donc la fonction :
ne me semble pas être adapté dans ce cas (l'image n'est de toute façon pas un contrôle GUI ^^)
Re: [..] Modifier une image bmp
Posté : mer. 14 mai 2008 14:21
par tolf
Sinon, n'y-a-t-il pas moyen de lire les pixels directement dans le fichier avec des FileRead pour les modifier ?
Re: [..] Modifier une image bmp
Posté : mar. 19 août 2008 16:23
par tolf
Bon j'ai cherché sur le forum anglais et j'ai finalement trouvé :
http://www.autoitscript.com/forum/index ... l=lockbits
► Afficher le texteexemple de code
Code : Tout sélectionner
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
Global Const $GUI_Width = 200
Global Const $GUI_Height = 100
$strFileName = "spectrum.bmp"
$hwnd = GUICreate("GUI", $GUI_Width, $GUI_Height)
GUISetState()
_GDIPlus_Startup()
;Get handle to an Image object
$pBitmap = _GDIPlus_ImageLoadFromFile($strFileName)
;Get handle to a Graphics object
$graphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)
;Draw the picture into the GUI - it will stretch to fit the GUI
_GDIPlus_GraphicsDrawImageRect($graphics, $pBitmap, 0, 0, 100, $GUI_Height)
;Get $tagGDIPBITMAPDATA structure
$BitmapData = _GDIPlus_BitmapLockBits($pBitmap, 20, 20, 60, 60, $GDIP_ILMWRITE, $GDIP_PXF32RGB)
If @ERROR Then MsgBox(0,"","Error locking region for writing")
;Stride - Offset, in bytes, between consecutive scan lines of the bitmap. If the stride is positive, the bitmap is top-down. If the stride is negative, the bitmap is bottom-up.
$Stride = DllStructGetData($BitmapData, "Stride")
ConsoleWrite("Bitmap Stride: " & $Stride & @CRLF)
;Image width - Number of pixels in one scan line of the bitmap.
$Width = DllStructGetData($BitmapData, "Width")
ConsoleWrite("Bitmap Width: " & $Width & @CRLF)
;Image height - Number of scan lines in the bitmap.
$Height = DllStructGetData($BitmapData, "Height")
ConsoleWrite("Bitmap Height: " & $Height & @CRLF)
;Pixel format - Integer that specifies the pixel format of the bitmap
$PixelFormat = DllStructGetData($BitmapData, "PixelFormat")
ConsoleWrite("Bitmap PixelFormat: " & $PixelFormat & @CRLF)
;Scan0 - Pointer to the first (index 0) scan line of the bitmap.
$Scan0 = DllStructGetData($BitmapData, "Scan0")
ConsoleWrite("Bitmap Scan0: " & $Scan0 & @CRLF)
For $row = 0 To $Height - 1
For $col = 0 To $Width - 1
$pixel = DllStructCreate("dword", $Scan0 + $row * $Stride + $col*4)
DllStructSetData($pixel,1,0x0000FF00)
;ConsoleWrite(Hex(DllStructGetData($pixel, 1)) & @CRLF)
Next
;ConsoleWrite("-------" & @CRLF)
Next
;Unlock region previously locked for writing
_GDIPlus_BitmapUnlockBits($pBitmap, $BitmapData)
_GDIPlus_GraphicsDrawImageRect($graphics, $pBitmap, 100, 0, 100, 100)
While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
Func OnAutoItExit()
If $pBitmap Then _GDIPlus_ImageDispose($pBitmap)
_GDIPlus_Shutdown()
EndFunc ;==>OnAutoItExit
Le code ajoute un carré vert à l'image chargée.