Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.2K Views
|
9 Replies
|
1 Total Like
View groups...
Share
Share this post:

Convert a batch of RGB images to Grayscale?

Posted 7 years ago

I have a few RGB images in a folder which i would like to convert to grayscale. I have been able to convert them individually but i am unable to convert all the images in the folder at one go. I have tried to implement ColorConvert and ColorSpace but was unable to convert the complete set of images in one go. I need to convert approximately 2000 images so it is not possible to convert them individually. I request some help to achieve this task.

Thanks

POSTED BY: Pushkar D
9 Replies
POSTED BY: Mikayel Egibyan

You can make a function and use it:

pathToFiles = "/yourPath/..";
SetDirectory[pathToFiles];
newPath = FileNameJoin[{pathToFiles, "newPath"}];
If[!DirectoryQ[newPath],
    CreateDirectory[newPath];
];
files = FileNames["*.jpg", pathToFiles];
Export[FileNameJoin[{newPath, FileNameTake[#, -1]}], customFunc@Import[#]] & /@ files

Now, your customFunc

customFunc[img_Image] :=
    Block[{dims, dirs, magnitudes, orientations},
       dims = ImageDimensions[img];
       dirs = ImageData[GradientOrientationFilter[img, 5]];
       magnitudes = ImageData[GradientFilter[img, 5]];
       orientations = MapThread[#1 {-Sin[#2], Cos[#2]} &, {magnitudes, dirs}, 2];

        ListVectorPlot[
           MapIndexed[{{#2[[2]], dims[[2]] - #2[[1]]}, #1} &, orientations, {2}], 
           VectorColorFunction -> (Yellow &)
        ]
    ]
POSTED BY: Mikayel Egibyan
Posted 7 years ago

I have tried what you suggested but the code is just creating an empty folder and is exiting the kernel.

I forgot to mention to you that i do not want the image in the background, i just want the descriptors as jpg without the actual image in the background.

POSTED BY: Pushkar D

I have updated the reply not to include the original image.

Are you sure you are trying exactly what I posted?

POSTED BY: Mikayel Egibyan
Posted 7 years ago

I have tried the updated code and have done the exact same thing which you posted but i am still getting an empty folder without anything in it and the kernel is quitting within seconds of evaluating the notebook.

POSTED BY: Pushkar D

Please retry.

POSTED BY: Mikayel Egibyan
Posted 7 years ago
POSTED BY: Pushkar D

Would something like this work for you?

pathToFiles = "/yourPath/..";
SetDirectory[pathToFiles];
newPath = FileNameJoin[{pathToFiles, "newPath"}];
If[!DirectoryQ[newPath],
    CreateDirectory[newPath];
];
files = FileNames["*.jpg", pathToFiles];
Export[FileNameJoin[{newPath, FileNameTake[#, -1]}], ColorConvert[Import[#], "Grayscale"]] & /@ files

referenced from here

POSTED BY: Mikayel Egibyan
Posted 7 years ago

had tried that already before posting the question, I am getting the following error when i do that

ColorConvert::ccvinput: C:\Users\Pushkar\Desktop\validate\test\car\00001.jpg should be a valid image, a color directive, a list of machine-sized real numbers of length up to 3, or a list of such objects.

I get the above error around 5 times and then i get the following error

General::stop: Further output of ColorConvert::ccvinput will be suppressed during this calculation.

after this error the notebook quits the kernel

POSTED BY: Pushkar D
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard