I would like to rescale and rotate image which I transform to numpy array with Pillow library (Image.open(..))
But i can use only numpy array manipulation.
Any tip or article how to do that? Without use of cv etc..
You can use PIL to resize and rotate like this
from PIL import Image
img = Image.open('Your/image/path')
img_120 = img.rotate(120)
img_re = img.resize((width, hight), Image.ANTIALIAS)
Related
I want to convert an rgb image to 2d matrix in gray. How can I do this using loops and PIL? I don't want to use a canned function. How can I do that ?
I manipulate a lot of images as NumPy arrays like so:
import numpy as np
from PIL import Image
# Load image
imgIn = Image.open(''c:/path/to/my/input/file.jpg'')
imgArray = np.array(imgIn)
#Do whatever manipulations to the image you need to, e.g.,
grayArray = np.mean(imgArray,axis=2)
#Save the final result
imgOut = Image.fromarray(grayArray)
imgOut.save('c:/path/to/my/output/file.jpg')
I have an image that looks like this:
array.resize(20,20,3)
img = Image.fromarray(array, 'RGB')
img.save('my.png',quality=90)
img.show()
It is currently a 500x500x3 NumPy array. The underlying space is a 20x20 grid of cells and I want to resize the image so that each grid cell has entries in a 20x20x3 NumPy Array corresponding to it's RGB values instead of (500/20)*(500/20)*3 entries per cell.
The code above does not seem to work unfortunately as it seems to be giving more entries per cell than I expected although I am not 100% sure.
To resize image with pillow you can use Image.resize()
from PIL import Image
import urllib.request
import numpy as np
data = urllib.request.urlopen('https://i.stack.imgur.com/7bPlZ.png')
old_img = Image.open(data)
new_img = old_img.resize((20, 20))
new_img.save('my.png',quality=90)
new_img.show()
array = np.array(new_img)
print(array)
But resizing image you can create pixels with half-tones.
Maybe you should get values directly from numpy.array. You have solid colors so you could get single pixel from every cell - because every cell has size 25x25 so it could be:
new_array = old_array[::25,::25,:]
and then you don't have to convert to image.
And if you convert this array to image then it should be sharper than create with Image.resize.
from PIL import Image
import urllib.request
import numpy as np
data = urllib.request.urlopen('https://i.stack.imgur.com/7bPlZ.png')
old_img = Image.open(data)
old_array = np.array(old_img)
new_array = array[::25,::25,:]
print(new_array)
new_img = Image.fromarray(new_array)
new_img.save('my.png',quality=90)
new_img.show()
Try this
size = 20, 20
img = Image.fromarray(array, 'RGB')
img.thumbnail(size, Image.ANTIALIAS)
img.save('my.png',quality=90)
img.show()
I've loaded an image using:
import numpy as np
from PIL import Image
imag = Image.open("image.png")
I = np.asarray(imag)
Where the shape of I is (951, 1200, 3)
But I would like to average each pixel roughly to it's luma values ((r*g*b)/3) to make the shape (951, 1200, 1).
What is the proper numpy operator to do this?
I think the easiest thing is to use Pillow's built-in conversion to Luminance as follows:
import numpy as np
from PIL import Image
# Load image and convert to luminance, and thence to Numpy array
imag = Image.open("image.png").convert('L')
I = np.asarray(imag)
I want to change the mode to grayscale and reshape the image to 28x28 pixels.
So far I have done this.
from PIL import Image
import numpy as np
img = Image.open('image2.jpg')
print(img.format, img.size, img.mode)
ndarray = np.array(img)
[Image]
I want to convert a PythonMagick Image Object to a NumPy array that can be used in OpenCV, and then I want to convert it into a PIL image object. I have searched Google but cannot find any sources explaining how to do this. Can someone show me how to convert image objects between these different modules?
The fastest way that I've found consist in saving and opening it:
import PythonMagic
import cv2
# pm_img is a PythonMagick.Image
pm_img.write('path/to/temporary/file.png')
np_img = cv2.imread('path/to/temporary/file.png')
I haven't found any satisfactory way to convert PythonMagick images to NumPy arrays without saving them, but there is a slow way that involves using python loops:
import PythonMagick
import numpy as np
pm_img = PythonMagick.Image('path/to/image.jpg')
h, w = pm_img.size().height(), pm_img.size().width()
np_img = np.empty((h, w, 3), np.uint16) # PythonMagick opens images with 16 bit depth
# It seems to store the same byte twice (weird)
for i in range(h):
for j in range(w):
# OpenCV stores pixels as BGR
np_img[i, j] = (pm_img.pixelColor(j, i).quantumBlue(),
pm_img.pixelColor(j, i).quantumGreen(),
pm_img.pixelColor(j, i).quantumRed())
np_img = np_img.astype(np.uint8)
Converting NumPy arrays to PIL images is easier:
from PIL import Image
pil_img = Image.fromarray(np_img[:, :, ::-1].astype(np.uint8))
Since PIL stores images in RGB but OpenCV stores them in BGR it's necessary to change the order of the channels ([:, :, ::-1]).
Image.fromarray() takes a NumPy array with dtype np.uint8.