Consider an image (img) as

The dimension of the image is:

{w, h} = ImageDimensions[img]


{488, 275}

I want to change the dimension to the power of 2, which comes very next to the highest dimension (488) i.e. 512.

How can I do this?

Edit 1:

I am manually trying to do this using

Image[ArrayPad[ImageData[img], {a, b}]]


But cannot figure out the suitable values for a and b.

• Try ImageResize – Ulrich Neumann Jan 31 '18 at 11:48
• @UlrichNeumann I don't want to resize the image because it will distort the shape of the different features. – Majis Jan 31 '18 at 11:49
• Ok! ImageCrop[img] gives an image of size 255x259 (nearby 256) – Ulrich Neumann Jan 31 '18 at 11:54
• ... ImageCrop[img,{256,256}] – Ulrich Neumann Jan 31 '18 at 12:02

Take the image dimensions and calculate the log with base 2. Then take the Ceiling to get the next best exponent that is large or equal to it and use ImageCrop:

img = Import["https://i.stack.imgur.com/yzWZx.png"]
ImageCrop[img, 2^(Ceiling[Log[2, #]] & /@ ImageDimensions[img])]


There is an inbuilt function ImagePad to do this.

Firstly,

{wdesired, hdesired} = 2^Ceiling[Log[2, #]] & /@ ImageDimensions[img]


(Thanks to @halirutan)

This will give me the desired dimensions.

Now,

ImagePad[img, {{Ceiling[(wdesired - w)/2],
Floor[(wdesired - w)/2]}, {Ceiling[(hdesired - h)/2],
Floor[(hdesired - h)/2]}}]


gives me my desired image.