Why resizing image lead to increase in channels? - python

I have grayscale images of different dimensions so I need to convert them to same dimension (say, 28*28) for my experiments. I tried to do it using different methods and I was able to do it but I observed that resizing of image lead to increase in number of channels. I am new to python and image processing so please help.
from PIL import Image
image = Image.open('6.tif')
image = image.resize((28, 28), Image.ANTIALIAS)
image.save('6.png', 'PNG', quality=100)
And then following code shows different dimensions:
import imageio
image_data = imageio.imread("6.tif").astype(float)
print(image_data.shape)
image_data = imageio.imread("6.png").astype(float)
print(image_data.shape)
and result is:
(65, 74)
(28, 28, 4)
I don't need the last dimension. How is this coming? I get the similar results even with "from resizeimage import resizeimage".

There are a number of issues with your code...
If you are expecting a greyscale image, make sure that is what you get. So, change this:
image = Image.open('6.tif')
to:
image = Image.open('6.tif').convert('L')
When you resize an image, you need to use one of the correct resampling methods:
PIL.Image.NEAREST
PIL.Image.BOX
PIL.Image.BILINEAR
PIL.Image.HAMMING
PIL.Image.BICUBIC
PIL.Image.LANCZOS
So, you need to replace the ANTI_ALIAS with something from the above list on this line:
image = image.resize((28, 28), Image.ANTIALIAS)
When you save as PNG, it is always loss-less. The quality factor does not work the same as for JPEG images, so you should omit it unless you have a good understanding of how it affects the PNG encoder.
If you make these changes, specifically the first, I think your problem will go away. Bear in mind though that the PNG encoder may take an RGB image and save it as a palletised image, or it may take a greyscale image and encode it as RGB, or RGB alpha.

Related

How to save a .ico image with certain dimensions using pillow?

I need to save an image with x and y dimensions, I am using pillow to do so, the problem is that it is saving in default dimension, in my case 16x16, I tried using resize like this:
new_image = image.resize((40, 40))
but still the same result, the only difference is that in the preview of the image it gets smaller, but it stays 16x16, Does anyone have ideas?
image_byte = b"image_bytes"
b = base64.b64decode(image_byte)
image = Image.open(io.BytesIO(b))
new_image = image.resize((40, 40))
new_image.save(icon_path)
Based on the discussion in the comments:
When saving ICO files, you will need to specify the sizes to save as (since ICOs can contain multiple sizes and formats of the same (or different!) image):
new_image.save('icon.ico', sizes=[(256, 256), (128, 128)])
If you don't need an ICO file, just use e.g. PNG (which contains a single format and size):
new_image.save('icon.png')

Python PIL read/open TIFF is black only

I try to read a TIFF file with pillow/PIL (7.2.0) in Python (3.8.3), e.g. this image.
The resulting file seems to be corrupted:
from PIL import Image
import numpy as np
myimage = Image.open('moon.tif')
myimage.mode
# 'L'
myimage.format
# 'TIFF'
myimage.size
# (358, 537)
# so far all good, but:
np.array(myimage)
# shows only zeros in the array, likewise
np.array(myimage).sum()
# 0
It doesn't seem to be a problem of the conversion to numpy array only, since if I save it to a jpg (myimage.save('moon.jpg')) the resulting jpg image has the appropriate dimensions but is all black, too.
Where did I do wrong or is it a bug?
I am not an expert in coding but i had same problem and found the TIFF file has 4 layers. R, G ,B and Alpha. When you convert it using PIL it is black.
try to view the image as plt.imshow(myimage[:, :, 0])
you could also remove the Alpha layer by saving the read image ( i used plt.imread('image')) and then saving it as image=image[:,:,3]. Now its a RGB image.
I don't know if i answered your question, but i felt this info might be of help.

How to convert PNG to JPG in Python?

I'm trying to compare two images, one a .png and the other a .jpg. So I need to convert the .png file to a .jpg to get closer values for SSIM. Below is the code that I've tried, but I'm getting this error:
AttributeError: 'tuple' object has no attribute 'dtype'
image2 = imread(thisPath + caption)
image2 = io.imsave("jpgtest.jpg", (76, 59))
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
image2 = resize(image2, (76, 59))
imshow("is it a jpg", image2)
cv2.waitKey()
Before demonstrating how to convert an image from .png to .jpg format, I want to point out that you should be consistent on the library that you use. Currently, you're mixing scikit-image with opencv. It's best to choose one library and stick with it instead of reading in an image with scikit and then converting to grayscale with opencv.
To convert a .png to .jpg image using OpenCV, you can use cv2.imwrite. Note with .jpg or .jpeg format, to maintain the highest quality, you must specify the quality value from [0..100] (default value is 95). Simply do this:
import cv2
# Load .png image
image = cv2.imread('image.png')
# Save .jpg image
cv2.imwrite('image.jpg', image, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
The function skimage.io.imsave expects you to give it a filename and an array that you want to save under that filename. For example:
skimage.io.imsave("image.jpg", image)
where image is a numpy array.
You are using it incorrectly here:
image2 = io.imsave("jpgtest.jpg", (76, 59))
you are assigning the output of the imsave function to image2 and I don't think that is what you want to do.
You probably don't need to convert the image to JPG because the skimage library already handles all of this conversion by itself. You usually only load the images with imread (does not matter whether they are PNG or JPG, because they are represented in a numpy array) and then perform all the necessary computations.
Python script to convert all .png in the folder into .jpg
import cv2 as cv
import glob
import os
import re
png_file_paths = glob.glob(r"*.png")
for i, png_file_path in enumerate(png_file_paths):
jpg_file_path = png_file_path[:-3] + "jpg";
# Load .png image
image = cv.imread(png_file_path)
# Save .jpg image
cv.imwrite(jpg_file_path, image, [int(cv.IMWRITE_JPEG_QUALITY), 100])
pass
Simply use opencv's cvtColor. Assuming image read using cv2.imread(); image color channels arranged as BGR.
To convert from PNG to JPG
jpg_img = cv2.cvtColor(png_img, cv2.COLOR_RGBA2BGR)
To convert from JPG to PNG
png_img = cv2.cvtColor(jpg_img, cv2.COLOR_BGR2BGRA)

Error when overlaying two images in OpenCV and or PIL

I've tried overlaying two images in openCV both in openCV and in PIL, but to no avail. I'm using a 1000x1000x3 array of np.zeros for the background (aka, a black background) and this random image of my monitor, but I really can't get it to work for some reason unbeknownst to me.
Trying with OpenCV only: (result(if you pay attention, you can see a couple of weird lines and dots in the middle))
base_temp = np.zeros((1000,1000,3))
foreground_temp = cv2.imread('exampleImageThatILinkedAbove.png')
base_temp[offset_y:offset_y+foreground_temp.shape[0], offset_x:offset_x+foreground_temp.shape[1]] = foreground_temp
Trying with PIL: (The result is literally the same as the OpenCV version)
base_temp = cv2.convertScaleAbs(self.base) #Convert to uint8 for cvtColor
base_temp = cv2.cvtColor(base_temp, cv2.COLOR_BGR2RGB) #PIL uses RGB and OpenCV uses BGR
base_temp = Image.fromarray(base_temp) #Convert to PIL Image
foreground_temp = cv2.cvtColor(cv2.convertScaleAbs(self.last_img), cv2.COLOR_BGR2RGB)
foreground_temp = Image.fromarray(foreground_temp)
base_temp.paste(foreground_temp, offset)
I'm using python3.5 and and OpenCV3.4 on Windows 10, if that's any help.
I'd like to avoid any solutions that require saving the cv2 images and then reloading them in another module to convert them but if it's unavoidable that's okay too. Any help would be appreciated!
If you check the type of base_temp, you will see it is float64 and that is going to cause you problems when you try to save it as a JPEG which expects unsigned 8-bit values.
So the solution is to create your base_temp image with the correct type:
base_temp = np.zeros((1000,1000,3), dtype=np.uint8)
The complete code and result look like this:
import cv2
import numpy as np
from PIL import Image
# Make black background - not square, so it shows up problems with swapped dimensions
base_temp=np.zeros((768,1024,3),dtype=np.uint8)
foreground_temp=cv2.imread('monitor.png')
# Paste with different x and y offsets so it is clear when indices are swapped
offset_y=80
offset_x=40
base_temp[offset_y:offset_y+foreground_temp.shape[0], offset_x:offset_x+foreground_temp.shape[1]] = foreground_temp
Image.fromarray(base_temp).save('result.png')

convert greyscale image back to vector

I have a list called w (size: 784), which I outputted to a png greyscale image:
import matplotlib.pyplot as plt
tmp = 1/(1+np.exp(-10*w/w.max()))
plt.imshow(tmp.reshape(28,28),cmap="gray")
plt.draw()
plt.savefig("final_weight_vector")
Now I want to read the png image back to be a vector.
The solutions I found so far:
First:
import matplotlib.image as mpimg
img=mpimg.imread('final_weight_vector.png')
but img appears to not be greyscale, because its dimensions turend out to be (600, 800, 4).
Second:
reading the file as RGB and converting to greyscale:
im = Image.open('final_weight_vector.png').convert('LA')
However, I couldn't find how to iterate over im so I have no idea as to what's inside. Further, I am not sure the output of im will have the exact same values as the original w.
Help please?
The problem is that what you saved is probably a plot of the 28x28 image, not the image itself.
To be sure, please preview the image. I bet it is 600x800, not 28x28. I also suppose it contains many additional elements, like axes and padding.
If you want to store your array in a loadable format, you may use numpy.save() (and numpy.load() to load it).
You may also use PIL to save your array as image (e.g. using something similar to: http://code.activestate.com/recipes/577591-conversion-of-pil-image-and-numpy-array/)

Categories