2020년 12월 7일 월요일

사진읽어와 데이터베이스에 저장하기

사진을 정리하여 PDF 파일로 만들어 프린터로 출력하거나 보관하기 위해서 사진을 데이터베이스에 읽어와 일괄작업을 하려고 했더니 

요즈음은 휴대폰의 카메라 성능이 너무 좋아 프린트 출력을 위한 용도로는 파일의 크기가 너무커서 데이터 베이스 작업이 너무 어렵고,

또한 사진의 방향의 제각각으로 읽혀지는 경우가 많습니다.

이곳저곳 기웃기웃하여 좋은 방법을 찾았습니다.

본 예제른 사진의 가로폭을 1280으로 조절하고, 사진의 방향을 맞추어 데이터 베이스에 저장하는 것 까지 보여줍니다.

uses JPEG, GDIPAPI, GDIPOBJ;
procedure TForm1.acAddJpgExecute(Sender: TObject);
var
idx: integer;
jpg: TjpegImage;
GPImage: TGPimage;
GPGraphics: TGPGraphics;
pPropItem: PPropertyItem;
BufferSize: Cardinal;
Orientation: Byte;
RotateType: TRotateFlipType;
Bitmap: TBitmap;
TempBitmap: TBitmap;
iHeight: integer;
begin
if OpenPictureDialog1.Execute then begin
for idx := 0 to OpenPictureDialog1.Files.Count - 1 do begin
ZQuery1.Append;
ZQuery1.FieldByName('ino').AsInteger := idx*10;
ZQuery1.FieldByName('idate').AsString := DateToStr(DatePicker1.Date);
GPImage := TGPimage.Create(OpenPictureDialog1.Files[idx]);
try
BufferSize := GPImage.GetPropertyItemSize(PropertyTagOrientation);
if BufferSize > 0 then begin
GetMem(pPropItem, BufferSize);
try
GPImage.GetPropertyItem(PropertyTagOrientation, BufferSize, pPropItem);
Orientation := PByte(pPropItem.value)^;
case Orientation of
1: RotateType := RotateNoneFlipNone; // Horizontal - No rotation required
2: RotateType := RotateNoneFlipX;
3: RotateType := Rotate180FlipNone;
4: RotateType := Rotate180FlipX;
5: RotateType := Rotate90FlipX;
6: RotateType := Rotate90FlipNone;
7: RotateType := Rotate270FlipX;
8: RotateType := Rotate270FlipNone;
else RotateType := RotateNoneFlipNone; // Unknown rotation?
end;
if RotateType <> RotateNoneFlipNone then GPImage.RotateFlip(RotateType);
Bitmap := TBitmap.Create;
TempBitmap := TBitmap.Create;
try
Bitmap.Width := GPImage.GetWidth;
Bitmap.Height := GPImage.GetHeight;
Bitmap.Canvas.Lock;
try
GPGraphics := TGPGraphics.Create(Bitmap.Canvas.Handle);
try
GPGraphics.DrawImage(GPImage, 0, 0, GPImage.GetWidth, GPImage.GetHeight);
if Bitmap.Width > 1280 then begin
iHeight := Ceil((Bitmap.Height * 1280) / Bitmap.Width);
TempBitmap.Width := 1280;
TempBitmap.Height := iHeight;
SetStretchBltMode(TempBitmap.Canvas.Handle, HALFTONE);
StretchBlt(TempBitmap.Canvas.Handle, 0, 0, TempBitmap.Width, TempBitmap.Height, Bitmap.Canvas.Handle, 0, 0, Bitmap.Width, Bitmap.Height, SRCCOPY);
DBImage1.Picture.Assign(TempBitmap);
end
else DBImage1.Picture.Assign(Bitmap);
ZQuery1.Post;
finally GPGraphics.Free;
end;
finally Bitmap.Canvas.Unlock;
end;
finally
Bitmap.Free;
TempBitmap.Free;
end;
finally FreeMem(pPropItem);
end;
end
else begin
Bitmap := TBitmap.Create;
TempBitmap := TBitmap.Create;
try
Bitmap.Width := GPImage.GetWidth;
Bitmap.Height := GPImage.GetHeight;
Bitmap.Canvas.Lock;
try
GPGraphics := TGPGraphics.Create(Bitmap.Canvas.Handle);
try
GPGraphics.DrawImage(GPImage, 0, 0, GPImage.GetWidth, GPImage.GetHeight);
if Bitmap.Width > 1280 then begin
iHeight := Ceil((Bitmap.Height * 1280) / Bitmap.Width);
TempBitmap.Width := 1280;
TempBitmap.Height := iHeight;
SetStretchBltMode(TempBitmap.Canvas.Handle, HALFTONE);
StretchBlt(TempBitmap.Canvas.Handle, 0, 0, TempBitmap.Width, TempBitmap.Height, Bitmap.Canvas.Handle, 0, 0, Bitmap.Width, Bitmap.Height, SRCCOPY);
DBImage1.Picture.Assign(TempBitmap);
end
else DBImage1.Picture.Assign(Bitmap);
ZQuery1.Post;
finally GPGraphics.Free;
end;
finally Bitmap.Canvas.Unlock;
end;
finally
Bitmap.Free;
TempBitmap.Free;
end;
end;
finally GPImage.Free
end;
end;
StatusBar1.SimpleText := '사진을 읽어왔습니다, 작업사항을 입력하십시요.'
end; // for
end;

댓글 없음:

tensorflow gpu 사용하기에서

 tensorflow 설치시 주의해야 한다. # Anything above 2.10 is not supported on the GPU on Windows Native python - m pip install "tensorflow<2.11...