How to convert a .tif PIL image to a torch tensor? - python-imaging-library

I have some .tif images and I'm reading them in as PIL image.
I know there is a ToPILimage transform
but I could not find a from_PILimage() akin to from_numpy()
as of right now I have this ugly looking thing:
img = torch.from_numpy(np.array(Image.open('path/image.tif')))
Could you show me a better way?
Thanks in advance!

Similar to torchvision.transforms.ToPILImage(), you can use torchvision.transforms.ToTensor() directly. Example from PyTorch docs
There's also the functional equivalent torchvision.functional.to_tensor().
img = Image.open('someimg.png')
import torchvision.transforms.functional as TF
TF.to_tensor(img)
from torchvision import transforms
transforms.ToTensor()(img)

Related

how to use an image created by pillow library as input in neural network

I have a function that creates an image object from scratch using the PIL library. I want to use this image as an input to neural network without having to save the image and load it again
the following code creates an image with a rectangle in it
from PIL import Image, ImageDraw
img_size = 300
image = Image.new(mode='RGB', size=(img_size, img_size), color=0)
draw = ImageDraw.Draw(image)
draw.rectangle([(50, 50), (250, 250)], fill=255, outline=255, width=1)
image.show()
How would one go about using this object 'image' as in input to any neural network?
tensorflow and pytorch both are fine, I just need something that works
This is how you can pass PIL image to torch model
If image is your PIL <Image> class
Then
from torchvision import transforms
pil_to_tensor = transforms.ToTensor()(image).unsqueeze_(0)
output = model(pil_to_tensor ) #

How to convert .png to bitmap (.bmp) in MATLAB?

I am having difficulties converting a png file of simple black-coloured patterns I made using Illustrator into a bitmap. I need to do this in order to 3D print it (vector printer).
I was instructed to use MATLAB to do it however I tried using imread and imwrite but I'm rather confused as to what the first argument of imwrite, A, should be? Is there a particular format I need to use for it to work?
I tried doing it with an online converter and it gave me the same exact image but of type .bmp. Is that what's meant to happen?
I would appreciate any insight on the problem.
Use imread to read your png, then imwrite to save it in bmp format.
Implementation:
pic = imread('mypic.png');
imwrite(pic,'mypic.bmp','bmp');

ipython notebook read multiple images and display in CELL

How do I do the above ? This is my code but it doesn't work nothing is displayed
from PIL import Image
import glob
image_list = []
for filename in glob.glob('<my directory>.pgm'):
im=Image.open(filename)
image_list.append(im)
import matplotlib.pyplot as plt
for i in range(10):
plt.figure()
plt.imshow(image_list[i])
I would like it to be displayed in the cell
If you're interested in a much simpler and faster way of displaying images I recommend IPyPlot package:
import ipyplot
ipyplot.plot_images(images_list, max_images=20, img_width=150)
It's capable of displaying hundreds of images in a grid-like format within just 60-70ms
You would get a plot similar to this:
In your case, you should add %matplotlib inlinebefore your code and make sure that after plt.imshow(images_list) you add plt.show() as well so matplotlib renders your images.

how to save imageof axes for high resolution in matlab GUI

I trying save image with the comand 'getframe' but resolution is very low, so I can not give zoom. for now I'm using the code:
[arq,dir] = uiputfile('*.jpg','Output Files');
fileName=fullfile(dir,arq);
f=getframe(handles.axes1);
[x,map]=frame2im(f);
imwrite(x,fileName,'jpg');
I need save in jpg and also need save the label (x,y) in graph. How do ? There is another command better than the getframe??
Print is a good command?
Thanks
try using something other than jpg, it doesn't scale well. eps is a good choice as it is a vector format.
Try exportfig from FEX: exportfig

improving resolution of jpeg images in matlab

I have a jpeg which has reasonable resolution. I am looking for a method of improving the resolution of the image and then exporting it into a pdf file.
Currently I have imported the jpeg:
Img = imread('F:Work\fig.jpg');
From here I was hoping of exporting the figure by using the export_fig function:
https://sites.google.com/site/oliverwoodford/software/export_fig
But I am struggling on generating the figure which is required prior to exporting the image.
What part of the figure creation process are you struggling with? The general approach I would use is
Img = imread('F:Work\fig.jpg');
bigImg = imresize(Img,2);
imshow(bigImg);
export_fig test.png -native;
Check Matlab's documentation for different interpolation options that can be used when resizing images.