0

l have images of different size : (94,102) ,(42,62), (67,27), (23,21), (23,17). All the characters are in the center of the image and the rest it s black or white. I want to standardize the image into (32, 32)

Here is the link to my sample of data : https://drive.google.com/open?id=0B-QJnh0Uw96lbFVHbVBPclhycFk

enter image description here

enter image description here

vincent
  • 1,558
  • 4
  • 21
  • 34
  • Have you come across `cv2.resize()` function? You can resize all your characters to particular size.. – Jeru Luke Mar 28 '17 at 04:44
  • 1
    Possible duplicate of [Resize image maintaining aspect ratio AND making portrait and landscape images exact same size?](http://stackoverflow.com/questions/9103257/resize-image-maintaining-aspect-ratio-and-making-portrait-and-landscape-images-e) – Michał Gacka Mar 28 '17 at 07:11

1 Answers1

2

You want to scale the images uniformly, by a scaling factor computed separately for each image. If you take the longer dimension and divide it into 32 (32.0 / max(width, height)), then you'll get a number that you can multiply the width and height by.

scale = 32.0 / max(width, height)
scaledWidth = round(width * scale, 0)
scaledHeight = round(height * scale, 0)

This gets you halfway there, because the larger dimension is now 32 -- the smaller one might be something less than 32. So, you need to pad the image in the smaller dimension to make up the difference. How much do you pad it? Well, the total padding is going to be 32 - min(scaledWidth, scaledHeight). You want to put half on one side and half on the other. One potential problem is that it might be an odd number of pixels -- a simple way to handle that is to put half on one side and the rest on the other.

totalPadding = 32 - min(scaledWidth, scaledHeight)
paddingBefore = math.floor(totalPadding / 2) # <-- "half" before
paddingAfter = totalPadding - paddingBefore  # <-- "the rest" after

Then you just need to resize the image. I must confess that I am actually unfamiliar with the image processing functions you have at your disposal. However, the gist of it is:

  1. Create a new image that is 32x32.
  2. (Optional) If you want to ensure that the extended background is the same as the original image's background, since you mentioned it will be either black or white, then fill the 32x32 image with the same colour you find at coordinate (0, 0) of the original image.
  3. Draw the original image into the new image, specifying that you want to resize it. You'll need to determine whether the image is portrait or landscape -- if it is portrait, then you want to draw it at position (paddingBefore, 0), otherwise you want to draw it at position (0, paddingBefore). Most graphics frameworks I have worked with allow you to specify a "source" and a "destination" rectangle, so your source rectangle will be (0, 0)-(width, height), and your dest rectangle will be of size (scaledWidth, scaledHeight), and at the origin described above, depending on whether the image is wider or taller.
Jonathan Gilbert
  • 3,526
  • 20
  • 28
  • @Gilbert , thank you for these clarification. l'm still a littble confused or l didn t catch the whole idea of you explanation. after the instruction 'paddingAfter = totalPadding - paddingBefore' l get the image resized into (32, 32) ? l put a link to my sample of data – vincent Mar 28 '17 at 09:19
  • I'm not sure how else to explain it. I included specific steps at the end of the post. The first step is to create a target image to put the resized data into, and it specifically says to create a 32x32 image. That's how you end up with a 32x32 image. The third step is where you use the "draw scaled image" function of the imaging library you're working with to fit the provided image to the 32x32 box. It references the calculations explained earlier. – Jonathan Gilbert Mar 28 '17 at 15:20