dreamend
dreamend

Reputation: 79

Image Proccessing And Analyse

I need to develop a little program that will do what i can do with photoshop. That is

1-) Pick an image 2-) Pick the area you want 3-) Desaturate 4-) Apply threshold (123) to choosen area

And after all, I want to calculate the percentage of the white areas.

I didn't do any image processing projects, so any programming language, framework, library that you prefer is OK. I want to do it as quick as possible. What is your suggestion?

Thanks for Help!

Upvotes: 0

Views: 208

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207540

I think there are 2 possibilities

ImageMagick

ImageMagick has Perl abindings, which makes it VERY easy as you don't need a compiler and a load of other junk. See some examples here.

ImageMagick also has PHP bindings, so have a look here.

But before you do any Perl/PHP, I would play around at the command line with ImageMagick. Start with your image called "1.jpg" and try these steps at the commandline and see the intervening stages in "2.jpg", "3.jpg" etc. I suspect that these 4 lines do more or less everything you want:

convert 1.jpg -crop 200x400+400+600 2.jpg
convert 2.jpg -colorspace gray -depth 8 3.jpg
convert 3.jpg -threshold 50% 4.jpg
convert 4.jpg -format "%c" histogram:info:
40000:(0,0,0) #000000 (0)
40000:(255,255,255) #FFFFFF (255)

NetPBM is another library you could play around with.

By the way, if you already have, and and are familiar with, Photoshop, you can program it in Javascript yourself. You need to use the Adobe ExtendScript Toolkit. There are a few examples in the "Presets/Scripts" sub-folder of your Photoshop installation - they end in ".jsx". You are not obliged to use Javascript, there are a couple of other languages it can handle - VBScript, AppleScript.

Overview is here.

Documentation is here.

Here is an example I wrote a while back:

// ProcessForWeb - Adobe Photoshop Script
// Description: Copies current image, then flattens it and resizes it to a square shape on a white
// background with a drop shadow. Then saves the copy as an optimised JPEG. Leaves original
// untouched.
// Requirements: Adobe Photoshop CS2, or higher and also you must have a Style saved with the
// name "MyDropShadow"
// Version: 0.1
// Author: Mark Setchell ([email protected])
// Web: http://www.thesetchells.com
// ============================================================================
// Installation:
// 1. Place script in 'C:\Program Files\Adobe\Adobe Photoshop CS#\Presets\Scripts\'
// 2. Restart Photoshop
// 3. Choose File > Scripts > ProcessForWeb
// ============================================================================

// enable double-clicking from Mac Finder or Windows Explorer
// this command only works in Photoshop CS2 and higher
#target photoshop

// bring application forward for double-click events
app.bringToFront();

/*
 TODO:
 3. Improve name of saved file
 */

///////////////////////////////////////////////////////////////////////////////
// ProcessForWeb
///////////////////////////////////////////////////////////////////////////////
function ProcessForWeb() {

    // Pick up active document
    var originalDocument = activeDocument;

     // Create output folder and work out name of JPEG output file
     var ImageName = activeDocument.name;
    ImageName = ImageName.substr(0,ImageName.length-4);
    var OutputFolder = new Folder("~/Desktop/JPEG");
    OutputFolder.create();
    var JPEGname = new File("~/Desktop/JPEG/"+ImageName+".jpg");

    // duplicate original document
    var duplicate = originalDocument.duplicate();

    // flatten the duplicate document and make 8-bit (in case it was 16)
    duplicate.flatten();
    duplicate.bitsPerChannel = BitsPerChannelType.EIGHT;

    // resize the duplicate so that the longest side is IMAGEDIMENSION pixels long
    const IMAGEDIMENSION = 800;
    var w = duplicate.width.value;
    var h = duplicate.height.value;

    if(h>w){
       duplicate.resizeImage(null,IMAGEDIMENSION,72,ResampleMethod.BICUBICSMOOTHER);
    } else {
       duplicate.resizeImage(IMAGEDIMENSION,null,72,ResampleMethod.BICUBICSMOOTHER);
    }

    // Now copy the resized, flattened image ready to paste into a new white document
    duplicate.selection.selectAll();
    duplicate.selection.copy()
    duplicate.close(SaveOptions.DONOTSAVECHANGES);

    // Create a new, blank, white document to paste into. It will be CANVASDIMENSION pixels square
    const CANVASDIMENSION = 1000;
    var newDocumentRef = app.documents.add (CANVASDIMENSION, CANVASDIMENSION, 72, '/tmp/tmp.psd',NewDocumentMode.RGB, DocumentFill.WHITE,1.0,BitsPerChannelType.EIGHT,ColorProfile.WORKING);

    // Create a new empty layer to paste into and to which we will apply drop shadow
    var imageLayer=newDocumentRef.artLayers.add(); 

    // Paste the resized, flattened image into the new layer
    newDocumentRef.paste();

    // Apply my Drop Shadow style
    imageLayer.applyStyle("Setchell - Drop Shadow");

    // Set up our web export options    
    var options = new ExportOptionsSaveForWeb();
    options.quality = 100;
    options.format = SaveDocumentType.JPEG;
    options.includeprofile=true;

    newDocumentRef.exportDocument(JPEGname,ExportType.SAVEFORWEB,options);
    newDocumentRef.close(SaveOptions.DONOTSAVECHANGES);
}

///////////////////////////////////////////////////////////////////////////////
// isCorrectVersion - check for Adobe Photoshop CS2 (v9) or higher
///////////////////////////////////////////////////////////////////////////////
function isCorrectVersion() {
    if (parseInt(version, 10) >= 9) {
        return true;
    }
    else {
        alert('This script requires Adobe Photoshop CS2 or higher.', 'Wrong Version', false);
        return false;
    }
}

///////////////////////////////////////////////////////////////////////////////
// isOpenDocs - ensure at least one document is open
///////////////////////////////////////////////////////////////////////////////
function isOpenDocs() {
    if (documents.length) {
        return true;
    }
    else {
        alert('There are no documents open.', 'No Documents Open', false);
        return false;
    }
}

///////////////////////////////////////////////////////////////////////////////
// showError - display error message if something goes wrong
///////////////////////////////////////////////////////////////////////////////
function showError(err) {
    if (confirm('An unknown error has occurred.\n' +
        'Would you like to see more information?', true, 'Unknown Error')) {
            alert(err + ': on line ' + err.line, 'Script Error', true);
    }
}


// test initial conditions prior to running main function
if (isCorrectVersion() && isOpenDocs()) {

    // Save current RulerUnits to restore when we have finished
    var savedRulerUnits = app.preferences.rulerUnits;

    // Set RulerUnits to PIXELS
    app.preferences.rulerUnits = Units.PIXELS;

    try {
        ProcessForWeb();
    }
    catch(e) {
        // don't report error on user cancel
        if (e.number != 8007) {
            showError(e);
        }
    }

    // Restore RulerUnits to whatever they were when we started
    app.preferences.rulerUnits = savedRulerUnits;
}

Upvotes: 1

Thamizh
Thamizh

Reputation: 574

Just you want to do the above four mentioned task means, you can choose any programming language because those tasks are so easy.
But actually you want to do photoshop like application means, my strong recommendation is(because we have created a such tool just a month before)

Platform Details:-
Operating System : Linux (Linux mint is best)
Programming Language : Python or Cpp
Gui Designing Tool : Glade
Working Area(Canvas) : Goocanvas

Upvotes: 1

Related Questions