PIL Image can not open transformed image with skimage.io - python

I have performed a shear and horizona flip transformations on an float array saved in a PFM file, then I have saved them as tif files through this code:
import os
from scipy import ndarray
# image processing library
import skimage as sk
from skimage.transform import warp
from skimage import util
from skimage import io
import numpy as np
def shear(image_array: ndarray, shear=10):
tf_augment = sk.transform.AffineTransform(shear=np.deg2rad(shear))
return warp(image_array, tf_augment, order=1, preserve_range=True,
mode='edge')
def horizontal_flip(image_array: ndarray):
return image_array[:, ::-1]
image_path='./train/0006.pfm'
image_to_transform, scale=readPFM(image_path)
flipped_image1=horizontal_flip(image_to_transform)
io.imsave("flipped_image1.tif", flipped_image1)
sheared_image1 =shear(image_to_transform,5)
io.imsave("sheared_image1.tif", sheared_image1)
However, when I try to open these images with PIL framework, it works with the flipped image but not with sheared one:
from PIL import Image
Image.open("sheared_image1.tif")
I got the error
OSError: cannot identify image file 'sheared_image1.tif'
Could anyone explain to me the reason, or how to save my transformed images through skimage framework to be able to read them with PIL (I have to read them with PIL for latter purposes)

Related

ROI for image dataset

I'm trying to find out ROI for an image dataset. I'm able to find ROI for a single image, but when it comes to the whole dataset or multiple images I can't do it.
I tried to use different code from the internet, but nothing worked.
`
import cv2
import numpy as np
import os
import random
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
img_folder=r'Shrimp/train/Resized_Dataset'
img_dir = r'Cropped_shrimp'
for i in range(30):
file = random.choice(os.listdir(img_folder))
image_path= os.path.join(img_folder, file)
img=mpimg.imread(image_path)
ax=plt.subplot(1,30,i+1)
ax.title.set_text(file)
plt.imshow(img)
roi = cv2.selectROI(img_folder)
ROI_image = img_folder[int(r[1]):int(r[1]+r[3]),
int(r[0]):int(r[0]+r[2])]
cv2.imshow("ROI_image", ROI_image)
cv2.waitKey(0)
os.chdir(img_dir)
cv2.imwrite("ROI_image", JPG)
`
This is my last approach. I think there're lots of mistake because I'm trying this for the first time.

how do i create an image using a matrix with pycharm and python?

I'm trying to create some pixelart using a matrix in pycharm. The problem is that I have never used this program. It's supposed to work just by simply selecting if you're working with the RGB model, but it doesn't.
import cv2
import numpy as np
from matplotlib import pyplot as plt
pixels = ([0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0])
([0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0])
([0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0])
([0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0])
print (pixels[2][4])
cv2.waitKey()
You need to save pixels as a numpy array with type uint8 and then let cv2 display it. If you pass 0 to waitKey the window will stay open until you close it manually.
import cv2
import numpy as np
pixels = np.array([[0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0],[0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0],[0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0],[0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0]], np.uint8)
cv2.imshow("My Image", pixels)
cv2.waitKey(0)
You can use the Pillow library.
from PIL import Image
import numpy as np
pixels = np.array(pixels).astype(np.uint8) # converts pixels to a numpy array
image = Image.fromarray(pixels)
# now you can save your image using
image.save('newimage.png')

Load svg image in opencv

I'm trying to download an svg image and open it in opencv for further processing. What I am doing is to convert from svg to png format with cairosvg, open it using Pillow and finally convert to opencv. The problem here is that the image converted in opencv does not look exactly the same as the one in PIL format.
from io import BytesIO
import numpy as np
import requests
from PIL import Image
from cairosvg import svg2png
import cv2
svg_url = 'https://logincdn.msauth.net/16.000.28611.4/content/images/microsoft_logo_ee5c8d9fb6248c938fd0dc19370e90bd.svg'
# Get svg data
svg_data = requests.get(svg_url).content
# Convert from svg to png
png = svg2png(bytestring=svg_data)
# Open png in PIL
pil_img = Image.open(BytesIO(png))
pil_img.show() # This looks good
# Convert to opencv
cv_img = np.array(pil_img.convert('RGB'))[:, :, ::-1].copy() # Taken from https://stackoverflow.com/questions/14134892/convert-image-from-pil-to-opencv-format
cv2.imshow('cv_img', cv_img) # This does not look right
cv2.waitKey(0)
These are the resulting images from PIL and opencv format respectively:
The opencv image does not look right, e.g. the text does not have space between characters.
Why is that and how can I fix it?
As suggested, you have to preserve alpha channel.
import numpy as np
import requests
from io import BytesIO
from PIL import Image
from cairosvg import svg2png
import cv2
svg_url = 'https://logincdn.msauth.net/16.000.28611.4/content/images/microsoft_logo_ee5c8d9fb6248c938fd0dc19370e90bd.svg'
svg_data = requests.get(svg_url).content
png = svg2png(bytestring=svg_data)
pil_img = Image.open(BytesIO(png)).convert('RGBA')
pil_img.save('output/pil.png')
cv_img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGBA2BGRA)
cv2.imwrite('cv.png', cv_img)

How convert image to an array in python

I want to convert image (.jpg) to binary array. Because I have to use this array to my scrambler operating on it saved in file. Which library and and functions should I use?
You should take a look at the openCV library.
import cv2
img = cv2.imread('image.jpg', flags=cv2.IMREAD_COLOR)
You can use the python library: PIL & numpy. Click here to learn more about image handling in python.
import numpy
import PIL
img = PIL.Image.open("foo.jpg").convert("L")
imgarr = numpy.array(img)
You can use this code to convert your image to array
# Import the necessary libraries
from PIL import Image
from numpy import asarray
# load the image and convert into
# numpy array
img = Image.open('test.jpg')
arraydata = asarray(img)
# data
print(arraydata)

Python reading TIF images using matplotlib, cv, and numpy reduces resolution

This is my code in python to read TIF and save it as png file.
import numpy as np
from PIL import Image
import matplotlib.image as mpimg
#im.show()
from skimage import io
import matplotlib.pyplot as plt
import matplotlib
import imageio
I = plt.imread('Shoes.TIF')
im= Image.fromarray(I)
print im.size
imageio.imsave('ShoesTest.png', I)
input_filename = "Shoes.TIF"
img =mpimg.imread(input_filename)
imgg= Image.fromarray(img)
print img
imgg.show()
However, when the new image is saved. I get an issue. When I try to check the resolution of the image in photoshop, I get this result.
Original : 240 pixels / inch
Saved Image : 96 pixels / inch
How do I save image in python that retains the resolution of the image? That is why I noticed visual changes in the images. Please help.

Categories