Jump to content

Validate Black Images


AnRios
 Share

Recommended Posts

Greetings friends!

I have been searching the help file and Google, with no success, to find a way to validate images from a folder and mark them somehow in a spreadsheet.

My context is: I made a code with the help of the community that captures images from SAP and saves them in a folder.  Now I'd like to identify which ones are black, if it's even possible. I read about PixelSearch, but did not get it to work. If someone could point me in the right direction, I'd appreciate it.

The code I'm using:

#include <File.au3>
#include <ScreenCapture.au3>

$x = InputBox("Title", "Amount of Images To Capture", "", "", 320, 150)
If @error Then Exit
$x = Number($x)

$y = InputBox("Title", "Batch Name", "", "", 320, 150)
If @error Then Exit
$y = String($y)

HotKeySet("{HOME}", "printscreen")

Func printscreen()
$FilePath =("C:\Fiscalizacao\Fotos"&"/")
$FileName = $y & " - "
$FileList = _FileListToArray($FilePath, $FileName & '*.jpg', 1)

If Not IsArray($FileList) Then
    $FileName&= '1.jpg'
Else
        $FileName &= $FileList[0] + 1 & '.jpg'
EndIf
_ScreenCapture_Capture($FilePath & "\" & $FileName, 354, 196, 673, 436)
EndFunc

HotKeySet("{BS}", "Terminate")
Func Terminate()
    Exit 0
EndFunc

For $i = 1 to $x

Opt("WinTitleMatchMode",2)
If NOT WinExists("Relatorio") Then
   MsgBox(0, "Atenção!", "Relatório do MOM deve estar aberto!")
   Call("Terminate")
EndIf
Opt("WinTitleMatchMode",2)
WinActivate("Relatorio")
Sleep (250)
Opt("WinTitleMatchMode",2)
SendKeepActive("Relatorio")
Send("{ENTER}")
Sleep (1000)
Send("{HOME}")
Sleep (200)
Opt("WinTitleMatchMode",2)
WinActivate("Relatorio")
Send("{DOWN}")

Next

 

Link to comment
Share on other sites

If it is pure black I believe you could use PixelCheckSum when the image is displayed.  But you could also use GDI+ to load the image in memory and check if pixels are black.  I trust that the second option would be faster, especially if you have a large number of images.

Link to comment
Share on other sites

Actually the images aren't always pure black. Sometimes lighter tones do appear. Right now what I'm doing is loading them up in a VBA form, to display them, then manually validate, the black ones.

 

Say, how could I use GDI+ with my code?

Link to comment
Share on other sites

You can try with _GDIPlus_BitmapLockBits. There is an example in the help file  To me it is not clear if you only need to validate black images or also ones that are not pure black. If it is the second case then it would be a good idea to define "black color"

Link to comment
Share on other sites

Here a basic example of how to check a specific pixel color of an image.jpg.   Now you will have to manage the different shades of black.  It will depend on how those shades appear in your images.  You could separate the pixels into RGB values and identify if it is close to black.  Anyway it will start you up...

#include <GDIPlus.au3>

Const $sFile = "Test.jpg"

_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile($sFile)
If @error Then
  _GDIPlus_Shutdown()
  Exit MsgBox($MB_SYSTEMMODAL, "", "An error has occured - unable to load image!")
EndIf

Local $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage)     ;get width and height of the image
Local $tBitmapData = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF32ARGB)     ;locks the bitmap for reading
Local $iScan0 = DllStructGetData($tBitmapData, "Scan0")     ;get scan0 (pixel data) from locked bitmap
Local $iSearchPixel = 0xFFFFFFFF     ; pure black
Local $tPixel = DllStructCreate("int[" & $iW * $iH & "];", $iScan0)
Local $iPixel, $iRowOffset

For $iY = 0 To $iH - 1
  $iRowOffset = $iY * $iW + 1
  For $iX = 0 To $iW - 1       ;get each pixel in each line and row
    $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX)         ;get pixel color
    If $iPixel = $iSearchPixel Then ConsoleWrite("color found" & @CRLF)
  Next
Next
_GDIPlus_BitmapUnlockBits($hImage, $tBitmapData)     ;unlocks the bitmap that was locked by _GDIPlus_BitmapLockBits

;cleanup resources
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()

 

Link to comment
Share on other sites

@ahmet the images are pictures taken on a field operation. Whey they are taken outside the parameters, they turn out "black", but not always pure black. Sometimes the result is a lighter shade of black. Nevertheless they have no content, which is what I'm trying to automate.

 

@Nine I have a large amount of files to analyze. I didn't quite get how to use your code to achieve that (I'm somewhat new to scripting in AutoIt). As far as I understood, the example covers one file at a time?

Link to comment
Share on other sites

1 minute ago, AnRios said:

As far as I understood, the example covers one file at a time?

Yes you will need to adapt the code for multiple files.  It is kind of easy to do, but for now make it work for one file.  Try different images, and when it is all good, then change it for multiple files.  Good luck. 

Link to comment
Share on other sites

Sorry, I tried to run it, but nothing happens. I used the location from which the images are stored. 

#include <GDIPlus.au3>

Const $sFile = "C:\Fiscalizacao\Fotos\Lote 01 - 1.jpg" ; here don't know if it's correct

_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile($sFile)
If @error Then
  _GDIPlus_Shutdown()
  Exit MsgBox($MB_SYSTEMMODAL, "", "An error has occured - unable to load image!")
EndIf

 

Link to comment
Share on other sites

You copied just a few lines of code of my example !  And you say nothing happens ? What did you expect, some miracle ?  Copy my whole script, study it, open help file, read the explanations of each function, run all the examples of the help file related to those functions.  If it is still not clear, redo it until you have enough knowledge to modify the script in accordance of your needs.

Edited by Nine
Link to comment
Share on other sites

wouldnt there be noticeable difference in file size?  thought being: who cares what the data actually is, just throw out the ones that dont have enough of it? 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Maybe this is something worth to investigate:

Edit:

It's really fast :). Here's a sample code which calculates the average color for every picture in a given directory.

16777216 = 0x000000 = black
33554431 = 0xFFFFFF = white

Set a threshold (try and error), maybe everything below 16777216 + 1000000 = 17777216 is black enough for you.

_ASM_BitmapGetAverageColorValue_Ex.zip

Edited by KaFu
Link to comment
Share on other sites

@Nine calm down pal.  Obviously I used the entire script. What I sent was just the part where I wasn't sure about. I read everything and I am studying, but sometimes you get to a point where you don't understand things, that's why I'm here asking.

Link to comment
Share on other sites

8 hours ago, AnRios said:

Obviously I used the entire script.

No it was not obvious.  Beside your statement saying "Nothing happens" proves that you don't understand the basics of the script.

Link to comment
Share on other sites

18 hours ago, Nine said:

No it was not obvious.  Beside your statement saying "Nothing happens" proves that you don't understand the basics of the script.

Your statement proves you have no education. I ran your code changing exclusively the file and nothing happened, I didn't know you were so senstitive to facts.

Edited by AnRios
Link to comment
Share on other sites

  • Developers

Guys, please slow down and stop making this personal. 

Just stick to the facts or don't reply.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

On 11/6/2019 at 3:18 PM, AnRios said:

Sorry, I tried to run it, but nothing happens. I used the location from which the images are stored. 

#include <GDIPlus.au3>

Const $sFile = "C:\Fiscalizacao\Fotos\Lote 01 - 1.jpg" ; here don't know if it's correct

_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile($sFile)
If @error Then
  _GDIPlus_Shutdown()
  Exit MsgBox($MB_SYSTEMMODAL, "", "An error has occured - unable to load image!")
EndIf

 

please provide a sample image so i can test with your code. thanks. we can help you adapt @Nine's code. is this the total script that isn't working?

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

11 minutes ago, Earthshine said:

please provide a sample image so i can test with your code. thanks. we can help you adapt @Nine's code.

Sure mate, thanks!

I know they all look pitch black, but some present slightly different shades.

No, I just posted the beggining because I didn't know If I put the source file in the right place. I tried the full code posted before.

#include <GDIPlus.au3>

Const $sFile = "Test.jpg" ; I just changed here from what I understood.

_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile($sFile)
If @error Then
  _GDIPlus_Shutdown()
  Exit MsgBox($MB_SYSTEMMODAL, "", "An error has occured - unable to load image!")
EndIf

Local $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage)     ;get width and height of the image
Local $tBitmapData = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF32ARGB)     ;locks the bitmap for reading
Local $iScan0 = DllStructGetData($tBitmapData, "Scan0")     ;get scan0 (pixel data) from locked bitmap
Local $iSearchPixel = 0xFFFFFFFF     ; pure black
Local $tPixel = DllStructCreate("int[" & $iW * $iH & "];", $iScan0)
Local $iPixel, $iRowOffset

For $iY = 0 To $iH - 1
  $iRowOffset = $iY * $iW + 1
  For $iX = 0 To $iW - 1       ;get each pixel in each line and row
    $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX)         ;get pixel color
    If $iPixel = $iSearchPixel Then ConsoleWrite("color found" & @CRLF)
  Next
Next
_GDIPlus_BitmapUnlockBits($hImage, $tBitmapData)     ;unlocks the bitmap that was locked by _GDIPlus_BitmapLockBits

;cleanup resources
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()

Lote 01 - 26.jpg

Lote 01 - 293.jpg

Lote 01 - 415.jpg

 

 

 

Edited by AnRios
Link to comment
Share on other sites

can you zip up a directory that has a couple of successful captures alongside some of these failures? 

I am still of the auspice that there is no need to read pixel values, nor even open the files.

 

**Or if sensitive can i see a screenshot of the directory in details view? my continued curiosity is about size discrepancies.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

13 minutes ago, iamtheky said:

can you zip up a directory that has a couple of successful captures alongside some of these failures? 

I am still of the auspice that there is no need to read pixel values, nor even open the files.

Ok friend, let's hope you are correct!


Here it goes the zipped directory and a screenshot of the situation. Several "good" images and a black one (failed).

Example.jpg

Pictures.zip

Link to comment
Share on other sites

2 failed

2failed.thumb.PNG.379f7edbbc4d4b5c2e8006588ffc8239.PNG

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

  • Jos locked this topic
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...