Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing image in Python

I'm using the code from this question to convert some raw images into png.

import matplotlib.pyplot as plt
import numpy as np

# Parameters.
input_filename = "JPCLN001.IMG"
shape = (2048, 2048) # matrix size
dtype = np.dtype('>u2') # big-endian unsigned integer (16bit)
output_filename = "JPCLN001.PNG"

# Reading.
fid = open(input_filename, 'rb')
data = np.fromfile(fid, dtype)
image = data.reshape(shape)

# Display.
plt.imshow(image, cmap = "gray")
plt.savefig(output_filename)
plt.show()

The thing is, I'm expecting a png size of 2048x2048 but all I'm getting is images under 500x500. Any advice on how to fix this?

like image 763
MrD Avatar asked Jan 29 '26 02:01

MrD


1 Answers

If you just want to save the array as a .png without plotting it, you can use matplotlib.image.imsave:

import numpy as np
from matplotlib import pyplot as plt

# some random data
img = np.random.randint(256, size=(2048, 2048))

# creates a 2048 x 2048 .png image
plt.imsave('img.png', img, cmap='gray')
like image 69
ali_m Avatar answered Jan 31 '26 15:01

ali_m