MrD
MrD

Reputation: 5086

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?

Upvotes: 3

Views: 5038

Answers (1)

ali_m
ali_m

Reputation: 74154

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')

Upvotes: 1

Related Questions