• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

Help please - Splitting images into pieces

Hi,

what I need is to split an image file(.bmp, .jpg, .gif, .tiff) into a predefined number of pieces(image files in the same file format). All pieces to have the same image size.
The ideal solution will be

function split_img(img_path, divide_X, divide_Y)
.....
End function

where img_path is the path to the image file to be splitted.
divide_X, divide_Y will define how many pieces to split the image into

If called like
split_img("C:\Test\img.gif", 3, 2)
the result should be 6 gif files named img1...img6.gif, each of them having an width of img.gif_width/3 and heigth of img.gif_heigth/2, saved in the same folder where the original file is.
If img.gif is let's say 1000x1000 pixels, the result should be 6 files 333x500 pixels.

Thanks
0
peterned
Asked:
peterned
1 Solution
 
peternedAuthor Commented:
Looks like I can't assign more than 300 points to a question, but for a working answer to this(a piece of code that will do what I explained) I'll assign 600 by asking a fake question or something like that - I relly badly need a solution.
0
 
amebaCommented:
img1.BMP to img6.BMP isn't OK, right?
0
 
Richie_SimonettiIT OperationsCommented:
hearing...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Richie_SimonettiIT OperationsCommented:
Function split_img(img_path As String, divide_X As Integer, divide_Y As Integer)
Dim pic As StdPicture
Dim PicH As Long, PicW As Long
Dim sectionH As Long, sectionW As Long
Dim l As Integer, a As Integer, iCount As Integer
Dim filename As String, strRet

filename = Mid$(img_path, InStrRev(img_path, "\", , vbTextCompare) + 1)
strRet = Split(filename, ".", , vbTextCompare)
Set pic = LoadPicture(img_path)
PicH = ScaleY(pic.Height, vbHimetric, vbPixels) \ 1
PicW = ScaleX(pic.Width, vbHimetric, vbPixels) \ 1
sectionH = PicH \ divide_Y
sectionW = PicW \ divide_X
PicH = PicH - sectionH
PicW = PicW - sectionW
For a = 0 To PicH Step sectionH
    For l = 0 To PicW Step sectionW
        Picture2.PaintPicture pic, 0, 0, sectionW, sectionH, l, a, _
                      sectionW, sectionH, vbSrcCopy
        iCount = iCount + 1
        SavePicture Picture2.Image, "c:\" & strRet(0) & iCount & ".bmp"
    Next l
Next a
End Function





0
 
Richie_SimonettiIT OperationsCommented:
This is another simplest posibility. Take care that some values are hard-coded:


VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Object = "{27395F88-0C0C-101B-A3C9-08002B2F49FB}#1.1#0"; "PICCLP32.OCX"
Begin VB.Form frmPicSplit
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin MSComDlg.CommonDialog cdiag
      Left            =   2100
      Top             =   1350
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
      DefaultExt      =   "bmp"
      DialogTitle     =   "Image to split"
      FileName        =   "*.bmp"
   End
   Begin VB.TextBox txtSize
      Height          =   300
      Index           =   0
      Left            =   930
      TabIndex        =   0
      Top             =   75
      Width           =   450
   End
   Begin VB.TextBox txtSize
      Height          =   300
      Index           =   1
      Left            =   2385
      TabIndex        =   1
      Top             =   90
      Width           =   450
   End
   Begin VB.CommandButton Command1
      Caption         =   "&Split"
      Height          =   375
      Left            =   3045
      TabIndex        =   3
      Top             =   60
      Width           =   1215
   End
   Begin PicClip.PictureClip PClip
      Left            =   150
      Top             =   465
      _ExtentX        =   2646
      _ExtentY        =   1323
      _Version        =   393216
   End
   Begin VB.Label Label1
      Appearance      =   0  'Flat
      AutoSize        =   -1  'True
      BackColor       =   &H80000005&
      BackStyle       =   0  'Transparent
      Caption         =   "Divide X:"
      ForeColor       =   &H80000008&
      Height          =   195
      Index           =   0
      Left            =   105
      TabIndex        =   4
      Top             =   120
      Width           =   645
   End
   Begin VB.Label Label1
      Appearance      =   0  'Flat
      AutoSize        =   -1  'True
      BackColor       =   &H80000005&
      BackStyle       =   0  'Transparent
      Caption         =   "Divide Y:"
      ForeColor       =   &H80000008&
      Height          =   195
      Index           =   1
      Left            =   1560
      TabIndex        =   2
      Top             =   150
      Width           =   645
   End
End
Attribute VB_Name = "frmPicSplit"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub Command1_Click()
Dim pCells As Integer
pCells = (txtSize(0).Text * txtSize(1).Text) - 1
With cdiag
    .ShowOpen
    PClip.Cols = txtSize(0).Text
    PClip.Rows = txtSize(1).Text
    PClip.Picture = LoadPicture(.FileName)
    Dim i As Integer
    For i = 0 To pCells
        SavePicture PClip.GraphicCell(i), "c:\image" & i & ".bmp"
    Next i
End With
End Sub

0
 
peternedAuthor Commented:
Richie,

the first example gives error in
Picture2.PaintPicture pic, 0, 0, sectionW, sectionH, l, a,_
     sectionW, sectionH, vbSrcCopy
line
The second one in
pclip.Cols = txtSize(0).Text
line

Should I add some referencies or something?
Also it's very important to save the pieces in the original file format, not always as .bmp
0
 
ProgramITCommented:
Why not use the Microsoft PictureClip control.
A graphic can be loaded into it, then split using the Cols and Row properties.
Then by calling the GraphicCell properties a section of the picture can be used.
The cell then is treated like a seperate pic, which can be manipulated copied etc.
0
 
peternedAuthor Commented:
According to VB help only .bmp's can be loaded into PictureClip control, but I also need this to work for .jpg and .gif
0
 
peternedAuthor Commented:
Richie,

I did what I needed based on your two examples.
Thanx!
I'll now ask another  question named 'For Richie Simonetti', please post a reply and I'll accept it.
Thanks once again, you helped me a lot.

Peter
0
 
peternedAuthor Commented:
BTW there is no problem to save the pieces in the file format of the input file, not .bmp
0
 
peternedAuthor Commented:
about animated .gif's - if I split the file, the animation is lost, only the initial frame is splitted and the pieces are not animated.
I guess it's not possible to retain the animation in the pieces(I'm really lame in regard to images), but if it's possible please advice
0
 
peternedAuthor Commented:
Oh well,

I was completely wrong, nothing works :(

My default image viewer is Opera browser, which somehow displays the pieces of the splitted file.
But they can't be displayed in any other app, like IE5 or Photoshop.
So my question remains.
Since I already accepted an answer, if a working answer appears I'll ask this question again and will accept the really working answer for all the points I have now (nearly 500)
0
 
Richie_SimonettiIT OperationsCommented:
Ok. There is a bug in my code so take note of this modifications:

Function split_img(img_path As String, divide_X As Integer, divide_Y As Integer)
Dim pic As StdPicture
Dim PicH As Long, PicW As Long
Dim sectionH As Long, sectionW As Long
Dim l As Integer, a As Integer, iCount As Integer
Dim filename As String, strRet As String

strRet = Mid$(img_path, InStrRev(img_path, "\", , vbTextCompare) + 1)
filename = Left$(strRet, InStr(1, strRet, ".", vbTextCompare) - 1)
Set pic = LoadPicture(img_path)
PicH = ScaleY(pic.Height, vbHimetric, vbPixels) \ 1
PicW = ScaleX(pic.Width, vbHimetric, vbPixels) \ 1
sectionH = PicH \ divide_Y
sectionW = PicW \ divide_X
PicH = PicH - sectionH
PicW = PicW - sectionW
For a = 0 To PicH Step sectionH
    For l = 0 To PicW Step sectionW
        With Picture1
            .ScaleWidth = sectionW
            .ScaleHeight = sectionH
            .PaintPicture pic, 0, 0, sectionW, sectionH, l, a, _
                      sectionW, sectionH, vbSrcCopy
            iCount = iCount + 1
            SavePicture .Image, "c:\" & filename & iCount & ".bmp"
            .Picture = LoadPicture("")
        End With
    Next l
Next a
End Function

Thanks for "A" grade.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now