Saving Image in tensor array as jpg or png - python

I am trying to detect the face using mtcnn. The main aim is to detect face, crop and save the cropped image as jpg or png file type. The code implemented is below.
from facenet_pytorch import MTCNN
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
img = Image.open("example.jpg")
mtcnn = MTCNN(margin=20, keep_all=True, post_process=False)
faces = mtcnn(img)
print(faces.shape)
This gives the shape
torch.Size([1, 3, 160, 160])
How to save this cropped portion as jpg file.

torch.save(faces, "faces.torch")
that wont be saved as an image, if you want to save it as an image:
img = Image.fromarray(faces.cpu().detach().numpy()[0])
img.save("faces.png")

Related

Read Grayscale raster in opencv

I have a raster image which contains only one band. I am trying to apply histogram equalization on this raster. The problem i cannot read it as grayscale. if i read it as unchanged it show the shape and the values.
import cv2
import numpy as np
img = cv2.imread('blur3.tif',cv2.IMREAD_UNCHANGED)
print(img.shape)
shape
(1678, 1064)
But when i change the code to read grayscale it shows empty.
import cv2
import numpy as np
img = cv2.imread('blur3.tif',cv2.IMREAD_GRAYSCALE)

Saving grayscale image to a directory in python

I have a piece of code that takes in image data as grayscale values, and then converts into an image using matplotlib below
import matplotlib.pyplot as plt
import numpy
image_data = image_result.GetNDArray()
numpy.savetxt('data.cvs', image_data)
# Draws an image on the current figure
image = plt.imshow(image_data, cmap='gray')
I want to be able to export this data to LabView as a .png file. So I need to save these image to a folder where LabView and display them. Is there a function with pillow or os that can do this?
plt.imsave('output.png', image)
Does this work?
If image_data is a Numpy array of shape height x width with dtype=np.uint8 or dtype=np.uint16, you can make a PIL Image and save it as a PNG like this:
from PIL import Image
# Make PIL Image from Numpy array
pImage = Image.fromarray(image_data)
pImage.save('forLabView.png')
You can equally use OpenCV to save a Numpy array as a PNG for LabView like this:
import cv2
# Save Numpy array as PNG
cv2.imwrite('forLabView.png', image_data)
Check what your array is with:
print(image_data.shape, image_data.dtype)

Displaying Hole Filled image in python with OpenCV

I am working on some image analysis in python using OpenCV. I want to display an image that I filled in holes with using scipy.ndimage.binary_filled_holes. Upon doing this I could not see anything being displayed when I used cv2.imshow, so I used plt.imshow and saw that the holes in my original image were filled. I want to use the cv2.imshow function to display the image. I did convert the image so that the datatype is uint8, yet still, nothing shows up. Any help would be appreciated.
import cv2
import matplotlib.pyplot as plt
import numpy as np
import scipy.ndimage
img = cv2.imread('Funky 647.jpg', cv2.IMREAD_GRAYSCALE)
dst = cv2.fastNlMeansDenoising(img,None,10,7,21)
ret, thresh2 = cv2.threshold(dst, 40, 255, cv2.THRESH_BINARY)
hole_filled= np.uint8(scipy.ndimage.binary_fill_holes(thresh2))
# plt.imshow(hole_filled)
cv2.imshow('No Holes', hole_filled)
cv2.waitKey(0)
cv2.destroyAllWindows()
Hole Filled Image via matplotlib:

wand.Image object to PIL Image

I currently have an image in the wand.image format. I need to convert it to PIL format so that I can continue working with it. It is and RGB image. I cant seem to find anywhere how to do it. I have tried to convert it to an np array and then read the np array into the PIL image. Thank you for any help. Here is my code if it helps:
from PIL import Image, ImageFilter
from wand.image import Image as Image2
with Image2(filename=join(img_path,file)) as img:
img.virtual_pixel = 'transparent'
test_image = Image.fromarray(np.array(img), 'RGB')
data_image = Image.open(io.BytesIO(img.make_blob("png"))).convert('RGB') fixes the issue for some reason

CMYK image color wrong when plotting

I'm trying to import a CMYK image into python to check the total ink coverage. I saved the image with adobe illustrator as jpg, and it looks OK in the windows viewer. When open the image in python some of the colors are distorted. In the example image, the brown is completely black. I've tried starting from a TIF file but same problem occured.
from PIL import Image
import matplotlib.pyplot as plt
imgName = 'filename.jpg' # Filename
img = Image.open(imgName)
plt.figure()
plt.imshow(img)

Categories

Resources