Wednesday, October 26, 2011
Tuesday, October 11, 2011
Crop all images from folder
private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo Dir = new DirectoryInfo(@"C:\Documents and Settings\Desktop\brodatz_database\");
string newDir = @"C:\Documents and Settings\\Desktop\brodatz_database\new\";
FileInfo[] FileList = Dir.GetFiles("*.GIF", SearchOption.AllDirectories);
int i = 0;
foreach (FileInfo FI in FileList)
{
Bitmap b = new Bitmap(FI.FullName);
string path = newDir + @"\" + i.ToString();
Directory.CreateDirectory(path);
string cale = path +"\\" + FI.Name.Replace(".GIF","") + "_1.jpg";
cropImage(b,new Rectangle(0,0,b.Width/3,b.Height/3),cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_2.jpg";
cropImage(b, new Rectangle(0, b.Height / 3, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_3.jpg";
cropImage(b, new Rectangle(0, 2*b.Height / 3, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_4.jpg";
cropImage(b, new Rectangle(b.Width / 3, 0, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_5.jpg";
cropImage(b, new Rectangle(b.Width / 3, b.Height / 3, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_6.jpg";
cropImage(b, new Rectangle(b.Width / 3, 2 * b.Height / 3, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_7.jpg";
cropImage(b, new Rectangle(2*b.Width / 3, 0, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_8.jpg";
cropImage(b, new Rectangle(2*b.Width / 3, b.Height / 3, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_9.jpg";
cropImage(b, new Rectangle(2*b.Width / 3, 2 * b.Height / 3, b.Width / 3, b.Height / 3), cale);
i++;
}
}
private void cropImage(Image img, Rectangle cropArea, string cale)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
bmpCrop.Save(cale, ImageFormat.Jpeg);
}
{
DirectoryInfo Dir = new DirectoryInfo(@"C:\Documents and Settings\Desktop\brodatz_database\");
string newDir = @"C:\Documents and Settings\\Desktop\brodatz_database\new\";
FileInfo[] FileList = Dir.GetFiles("*.GIF", SearchOption.AllDirectories);
int i = 0;
foreach (FileInfo FI in FileList)
{
Bitmap b = new Bitmap(FI.FullName);
string path = newDir + @"\" + i.ToString();
Directory.CreateDirectory(path);
string cale = path +"\\" + FI.Name.Replace(".GIF","") + "_1.jpg";
cropImage(b,new Rectangle(0,0,b.Width/3,b.Height/3),cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_2.jpg";
cropImage(b, new Rectangle(0, b.Height / 3, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_3.jpg";
cropImage(b, new Rectangle(0, 2*b.Height / 3, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_4.jpg";
cropImage(b, new Rectangle(b.Width / 3, 0, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_5.jpg";
cropImage(b, new Rectangle(b.Width / 3, b.Height / 3, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_6.jpg";
cropImage(b, new Rectangle(b.Width / 3, 2 * b.Height / 3, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_7.jpg";
cropImage(b, new Rectangle(2*b.Width / 3, 0, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_8.jpg";
cropImage(b, new Rectangle(2*b.Width / 3, b.Height / 3, b.Width / 3, b.Height / 3), cale);
cale = path + "\\" + FI.Name.Replace(".GIF", "") + "_9.jpg";
cropImage(b, new Rectangle(2*b.Width / 3, 2 * b.Height / 3, b.Width / 3, b.Height / 3), cale);
i++;
}
}
private void cropImage(Image img, Rectangle cropArea, string cale)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
bmpCrop.Save(cale, ImageFormat.Jpeg);
}
Subscribe to:
Posts (Atom)