拜求能将彩色图片转换成灰度图的方法(去色) [已解决]
本帖最后由 z761003 于 2011-11-22 09:52 编辑请问能如何用AU3将彩色图片去色,变成灰度图片呀?{:face (332):}{:face (207):} 问题还是没解决,能不能给我看看,大虾们 利用WEB 代码 然后AU3调用就OK了. 参考福大这个帖子2楼http://www.autoitx.com/forum.php?mod=viewthread&tid=20741&from=favorites 本帖最后由 happytc 于 2011-11-22 09:01 编辑
回复 1# z761003
最近正在用au3写个类似Windows里的‘画图(mspaint)’简化功能版的,如灰度/尺寸/添加箭头等常用修改图片功能。
下面的代码是修改灰度(估计楼主是要这个功能,而不是黑白)/黑白/亮度/对比度等等
GDIP.au3若没有,见附件:
#include <Constants.au3>
#include <GDIP.au3>
#include <GUIConstantsEx.au3>
#include <ScrollBarConstants.au3>
#include <GUIToolbar.au3>
#include <WindowsConstants.au3>
Opt('MustDeclareVars', 1)
If Not IsDeclared("SB_ENDSCROLL") Then Global Const $SB_ENDSCROLL = 8
Global $hToolbar
Global Enum $idOpen = 0x0400, $idSave
Global $aLables = ["Contrast", "Brightness", "Saturation", "Hue", "Gamma"]
Global $aSliders = _
[, _
, _
, _
, _
]
Global $hGraphics, $hImage, $hBackImage, $hImageContext, $hIA, $tPreHue, $tPostHue, $aImageSize, $sImage, $iPicWidth, $iPicHeight, $fChange
Global $sImageFormats = "All Picture Files (*.bmp;*.gif;*.jpg;*.jpeg;*.jpe;*.jfif;*.tif;*.tiff;*.png;*.exif;*.wmf;*.emf)"
_GDIPlus_Startup()
_Main()
_GDIPlus_Shutdown()
Exit
Func _Main()
Local $hGUI, $BtnReset, $BtnBlackWhite, $BtnNegative, $BtnGrey, $iGUIWidth, $iGUIHeight, $iLeft, $iTop, $iI, $aSize
$iGUIWidth = @DesktopWidth * 0.75
$iGUIHeight = @DesktopHeight * 0.75
If $iGUIWidth < 900 Then $iGUIWidth = 900
If $iGUIHeight < 700 Then $iGUIHeight = 700
$hGUI = GUICreate("Image tool", $iGUIWidth, $iGUIHeight)
$hToolbar = _GUICtrlToolbar_Create($hGUI)
$aSize = _GUICtrlToolbar_GetMaxSize($hToolbar)
$iPicWidth = $iGUIWidth - 250
$iPicHeight = $iGUIHeight - $aSize - 30
GUICtrlCreateLabel("", 10, $aSize + 20, $iPicWidth, $iPicHeight, -1, $WS_EX_CLIENTEDGE)
$hGraphics = _GDIPlus_GraphicsCreateFromHWND(GUICtrlGetHandle(-1))
$hIA = _GDIPlus_ImageAttributesCreate()
_GDIPlus_GraphicsSetInterpolationMode($hGraphics, 7)
$iLeft = $iGUIWidth - 230
$iTop = $aSize + 20
For $iI = 0 To UBound($aSliders) - 1
GUICtrlCreateLabel($aLables[$iI], $iLeft, $iI * 60 + $iTop)
$aSliders[$iI] = GUICtrlCreateSlider($iLeft, $iI * 60 + $iTop + 20, 150, 20)
$aSliders[$iI] = GUICtrlGetHandle(-1)
$aSliders[$iI] = GUICtrlCreateLabel("", $iLeft + 160, $iI * 60 + $iTop + 20, 40, 25)
GUICtrlSetLimit($aSliders[$iI], $aSliders[$iI], $aSliders[$iI])
Next
_Reset()
$BtnReset = GUICtrlCreateButton("Reset", $iLeft + 150, $iI * 60 + $iTop, 70, 25)
$BtnBlackWhite = GUICtrlCreateButton("Black&&White", $iLeft + 62, $iI * 60 + $iTop, 75, 25)
$BtnNegative = GUICtrlCreateButton("Negative", $iLeft - 10, $iI * 60 + $iTop, 60, 25)
$BtnGrey = GUICtrlCreateButton("Greyscale", $iLeft - 10, $iI * 70 + $iTop, 60, 25)
_GUICtrlToolbar_AddBitmap($hToolbar, 1, -1, $IDB_STD_LARGE_COLOR)
_GUICtrlToolbar_AddButton($hToolbar, $idOpen, $STD_FILEOPEN)
_GUICtrlToolbar_AddButton($hToolbar, $idSave, $STD_FILESAVE)
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
GUIRegisterMsg($WM_HSCROLL, "_WM_HSCROLL")
GUISetState()
While 1
Switch GUIGetMsg()
Case $BtnReset
_Reset()
Case $BtnBlackWhite
_BlackAndWhite()
Case $BtnNegative
_Negative()
Case $BtnGrey
_Greyscale()
Case $GUI_EVENT_RESTORE
If $hImage Then _Update()
Case $GUI_EVENT_CLOSE
If $fChange Then
If _Save() Then
If $hImageContext Then _GDIPlus_GraphicsDispose($hImageContext)
If $hBackImage Then _GDIPlus_ImageDispose($hBackImage)
If $hImage Then _GDIPlus_ImageDispose($hImage)
_GDIPlus_ImageAttributesDispose($hIA)
_GDIPlus_GraphicsDispose($hGraphics)
ExitLoop
EndIf
Else
ExitLoop
EndIf
EndSwitch
WEnd
EndFunc ;==>_Main
Func _Reset()
Local $iI
For $iI = 0 To UBound($aSliders) - 1
GUICtrlSetData($aSliders[$iI], _GetStringFormat($iI, $aSliders[$iI]))
GUICtrlSetData($aSliders[$iI], $aSliders[$iI])
$aSliders[$iI] = -1
Next
_GDIPlus_ImageAttributesSetThreshold($hIA, 0, False)
If $hImage Then _Update()
EndFunc ;==>_Reset
Func _Save()
Local $iRet
If $fChange Then
$iRet = MsgBox(0x23, "Save changes", 'Would you like to save changes to "' & StringTrimLeft($sImage, StringInStr($sImage, "\", 0, -1)) & '"?')
If $iRet = 2 Then
Return False
ElseIf $iRet = 6 Then
_GDIPlus_ImageSaveToFile($hBackImage, $sImage)
EndIf
EndIf
Return True
EndFunc ;==>_Save
Func _Initialize()
$tPreHue = _GDIPlus_ColorMatrixCreate()
$tPostHue = _GDIPlus_ColorMatrixCreate()
_GDIPlus_ColorMatrixInitHue($tPreHue, $tPostHue)
$aImageSize = _GDIPlus_ImageGetDimension($hImage)
_GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, 0, 0, $iPicWidth, $iPicHeight)
_Reset()
$fChange = False
EndFunc ;==>_Initialize
Func _Update()
Local $tColorMatrix, $pColorMatrix, $nContrast, $nBrightness, $nSaturation, $nHue, $nGamma
$tColorMatrix = _GDIPlus_ColorMatrixCreate()
$pColorMatrix = DllStructGetPtr($tColorMatrix)
$nContrast = _GetValue(0)
$nBrightness = _GetValue(1)
$nSaturation = _GetValue(2)
$nHue = _GetValue(3)
$nGamma = _GetValue(4)
_GDIPlus_ColorMatrixScale($tColorMatrix, $nContrast, $nContrast, $nContrast, 1, 1)
_GDIPlus_ColorMatrixTranslate($tColorMatrix, $nBrightness, $nBrightness, $nBrightness, 0, 1)
_GDIPlus_ColorMatrixSetSaturation($tColorMatrix, $nSaturation, 1)
_GDIPlus_ColorMatrixRotateHue($tColorMatrix, $tPreHue, $tPostHue, $nHue)
_GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $pColorMatrix)
_GDIPlus_ImageAttributesSetGamma($hIA, 0, True, $nGamma)
_GDIPlus_GraphicsDrawImageRectRectIA($hImageContext, $hImage, 0, 0, $aImageSize, $aImageSize, 0, 0, $aImageSize, $aImageSize, $hIA)
_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBackImage, 0, 0, $aImageSize, $aImageSize, 0, 0, $iPicWidth, $iPicHeight)
EndFunc ;==>_Update
Func _GetValue($iIndex)
Return GUICtrlRead($aSliders[$iIndex]) * $aSliders[$iIndex]
EndFunc ;==>_GetValue
Func _GetStringFormat($iIndex, $iVal)
If $aSliders[$iIndex] Then
Return StringFormat("%." & $aSliders[$iIndex] & "f", $iVal * $aSliders[$iIndex])
Else
Return StringFormat("%d", $iVal * $aSliders[$iIndex])
EndIf
EndFunc ;==>_GetStringFormat
Func _BlackAndWhite()
Local $tBWMatrix, $pBWMatrix
If $hImage Then
_GDIPlus_ImageAttributesSetThreshold($hIA, 0, True, 0.5)
$tBWMatrix = _GDIPlus_ColorMatrixCreateGrayScale()
$pBWMatrix = DllStructGetPtr($tBWMatrix)
_GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $pBWMatrix)
_GDIPlus_GraphicsDrawImageRectRectIA($hImageContext, $hImage, 0, 0, $aImageSize, $aImageSize, 0, 0, $aImageSize, $aImageSize, $hIA)
_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBackImage, 0, 0, $aImageSize, $aImageSize, 0, 0, $iPicWidth, $iPicHeight)
$fChange = True
EndIf
EndFunc ;==>_BlackAndWhite
Func _Greyscale()
Local $tNegMatrix, $pNegMatrix
If $hImage Then
$tNegMatrix = _GDIPlus_ColorMatrixCreateGrayScale()
$pNegMatrix = DllStructGetPtr($tNegMatrix)
_GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $pNegMatrix)
_GDIPlus_GraphicsDrawImageRectRectIA($hImageContext, $hImage, 0, 0, $aImageSize, $aImageSize, 0, 0, $aImageSize, $aImageSize, $hIA)
_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBackImage, 0, 0, $aImageSize, $aImageSize, 0, 0, $iPicWidth, $iPicHeight)
$fChange = True
EndIf
EndFunc ;==>_Greyscale
Func _Negative()
Local $tNegMatrix, $pNegMatrix
If $hImage Then
$tNegMatrix = _GDIPlus_ColorMatrixCreateNegative()
$pNegMatrix = DllStructGetPtr($tNegMatrix)
_GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $pNegMatrix)
_GDIPlus_GraphicsDrawImageRectRectIA($hImageContext, $hImage, 0, 0, $aImageSize, $aImageSize, 0, 0, $aImageSize, $aImageSize, $hIA)
_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBackImage, 0, 0, $aImageSize, $aImageSize, 0, 0, $iPicWidth, $iPicHeight)
$fChange = True
EndIf
EndFunc ;==>_Negative
Func _WM_HSCROLL($hWnd, $iMsg, $iwParam, $ilParam)
Local $iI, $iPos, $iCode
$iCode = BitAND($iwParam, 0xFFFF)
If $iCode = $SB_ENDSCROLL Or $iCode = $SB_THUMBPOSITION Then Return 0
For $iI = 0 To UBound($aSliders) - 1
If $aSliders[$iI] = $ilParam Then
$iPos = GUICtrlRead($aSliders[$iI])
If $iPos <> $aSliders[$iI] Then
GUICtrlSetData($aSliders[$iI], _GetStringFormat($iI, $iPos))
$aSliders[$iI] = $iPos
If $hImage Then
$fChange = True
_Update()
EndIf
EndIf
ExitLoop
EndIf
Next
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_HSCROLL
Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $tNMHDR, $hFrom, $iCode, $iCommand, $sFileName, $sFile, $sExt, $sTmp, $hBitmap, $hContext
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hFrom = DllStructGetData($tNMHDR, "hWndFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
If $hFrom = $hToolbar And $iCode = $NM_LDOWN Then
Local $iCommand = _GUICtrlToolbar_IndexToCommand($hToolbar, _GUICtrlToolbar_GetHotItem($hToolbar))
Switch $iCommand
Case $idOpen
$sFileName = FileOpenDialog("Open File", @WorkingDir, $sImageFormats, 3)
If $sFileName Then
If _Save() Then
$sImage = $sFileName
If $hImage Then
_GDIPlus_GraphicsDispose($hImageContext)
_GDIPlus_ImageDispose($hBackImage)
_GDIPlus_ImageDispose($hImage)
$hImage = 0
$hBackImage = 0
$hImageContext = 0
$sImage = ""
EndIf
$hBitmap = _GDIPlus_ImageLoadFromFile($sFileName)
If @error Then
MsgBox(0x10, "Error", "Could not load image")
_Reset()
$fChange = False
Else
$hImage = _GDIPlus_BitmapCreateFromScan0(_GDIPlus_ImageGetWidth($hBitmap), _GDIPlus_ImageGetHeight($hBitmap))
$hContext = _GDIPlus_ImageGetGraphicsContext($hImage)
_GDIPlus_GraphicsDrawImage($hContext, $hBitmap, 0, 0)
_GDIPlus_GraphicsDispose($hContext)
_GDIPlus_ImageDispose($hBitmap)
$hBackImage = _GDIPlus_ImageClone($hImage)
$hImageContext = _GDIPlus_ImageGetGraphicsContext($hBackImage)
_Initialize()
EndIf
EndIf
EndIf
Case $idSave
If $hImage Then
$sFile = StringTrimLeft($sImage, StringInStr($sImage, "\", 0, -1))
$sExt = StringTrimLeft($sFile, StringInStr($sFile, ".", 0, -1))
$sFileName = FileSaveDialog("Save As", @WorkingDir, $sImageFormats, 18, $sFile)
If $sFileName Then
If StringRight($sFileName, StringLen($sExt) + 1) <> "." & $sExt Then $sFileName &= "." & $sExt
_GDIPlus_ImageSaveToFile($hBackImage, $sFileName)
$fChange = False
EndIf
EndIf
EndSwitch
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_NOTIFY
楼上的要是把思路写出来,新手可以膜拜下 四楼大哥,你的那个<GDIP.au3>我没啊 验证码的教程中有一些不错的图片处理的方法,很不错.. 问题是没能力单独把它独立出来使用啊{:face (303):} 四楼大哥,你的那个我没啊
z761003 发表于 2011-11-20 15:34 http://www.autoitx.com/images/common/back.gif
把<GDIP.au3>改为<GDIPlusEx.au3>试试~ 也不行哦,,, 本帖最后由 afan 于 2011-11-21 18:48 编辑
#include <GDIPlusEx.au3>
$sFile = '1.jpg' ;需要处理的图片
$sFile_Gray = '1_去色.jpg' ;保存输出的图片
_GDIPlus_Startup()
_GDIPlus_ImageToGray($sFile, $sFile_Gray)
_GDIPlus_Shutdown()
Func _GDIPlus_ImageToGray($sFile, $sFile_Gray = '')
;=====将彩色转为灰度(去色),输出文件或灰度图像对象句柄
Local $hImage, $iW, $iH, $hGraphics, $tCOLORMATRIX, $pCOLORMATRIX, $FS
$hImage = _GDIPlus_ImageLoadFromFile($sFile)
$iW = _GDIPlus_ImageGetWidth($hImage)
$iH = _GDIPlus_ImageGetHeight($hImage)
$hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
$hIA = _GDIPlus_ImageAttributesCreate()
$tCOLORMATRIX = _GDIPlus_ColorMatrixCreateGrayScale()
$pCOLORMATRIX = DllStructGetPtr($tCOLORMATRIX)
_GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, 1, $pCOLORMATRIX)
_GDIPlus_GraphicsDrawImageRectRectIA($hGraphics, $hImage, 0, 0, $iW, $iH, 0, 0, $iW, $iH, $hIA)
If $sFile_Gray <> '' Then
$FS = _GDIPlus_ImageSaveToFile($hImage, $sFile_Gray)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_ImageDispose($hImage)
Return $FS
Else
Return $hImage
EndIf
EndFunc ;==>_GDIPlus_ImageToGray 回复 6# z761003
呵,看来没有服务完全呀,我已经在4楼帖子上传了 哇哈哈,,,解决了,,,11楼的代码完全可用哦,喜欢死我了,在些谢谢 afan超级版主 还有happytc两位好大哥,嘻,happytc我看也肯定是行的,只是我好几个UDF都找不到,用不起来。谢谢你们哦 可是,把这个贴弄成“已解决”怎么弄啊,找半天了找不到,我真是够菜的{:face (229):} 留名~~~~~~~
页:
[1]
2