How can I get the information about image? I need file size (bytes), image size (pixels), color
mode, bits per pixel. I've already found image size (pixels) but I can't find another.
I have:
from skimage.io import imread
im = imread('abc.png')
print("Size: ", im.size, im.shape)
I recommend using the Pillow library.
This will give you all the requested information about the image except the file size.
from PIL import Image
im = Image.open('whatever.png')
width, height = im.size
Refer to this link for the attributes for the image object
In order to get the file size use this snippet below.
import os
print os.stat('somefile.ext').st_size
Related
I am trying to capture image with each function call
import imageio as iio
camera = iio.get_reader("<video0>")
screenshot = camera.get_data(0)
plt.imsave(filename, screenshot)
camera.close()
I am able to save the image but the image size is varying sometime it is of the size of 960540 and sometime 1280720.
I went through the documentation of imageio but did not find any attribute to set its shape. I want a fix shape of image all the time.
I have tried OpenCV, it has its own limitation w.r.t my requirements.
So please suggest something in this package only.
Could anyone please help.
Your comment made clear you want to keep the resolution consistent for the duration of the whole video. As imageio does not provide a resize operation I suggest you to use skimage to handle that part.
import imageio as iio
import matplotlib.pyplot as plt
from skimage.transform import resize
camera = iio.get_reader("<video0>")
filename = "experiment_"
# randomly set to 30 frames for this example
for i in range(0, 30):
screenshot = camera.get_data(i)
# skimage resize function here
screenshot = resize(screenshot, (1280, 720))
final_filename = str(i) + ".jpg"
plt.imsave(filename+final_filename, screenshot)
camera.close()
I am trying to add some text on my image using PIL, see the code below,
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import sys
image = Image.open('image.png')
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('arial',40)
draw.text((700, 470),'Text',(0,0,0),font=font)
img.save('out-image.png','PNG')
But I lost the original colors of the image, see below images,
Original Image
After adding text
How I can preserve the original colors.
Thank You
That looks like a bug in PIL to me. I think it is because your image is palettised and the draw.text() is messing up the palette.
For a work-around, you can convert to an RGB image when you open it to avoid palette issues. Change to this:
image = Image.open('image.png').convert('RGB')
Assuming one has a base64 encoded image.
How can one extract the image dimensions from the string, preferably without storing the string to disc as an image?
For PNG files, I can get this from bytes 16-24 of the string which are part of the PNG header, but for JPEG images, it appears no such hack exists.
What are some nice ways of getting the image dimensions in this case?
Using the pillow library one can do:
import io
import PIL
from PIL import Image
imgdata = base64.b64decode(base64_str)
im = Image.open(io.BytesIO(imgdata))
width, height = im.size
When I read an image using opencv imread function, I find its height and width being swapped as what it should be. Like my original image is of dimensions (610 by 406) but on being read using opencv::imread function, its dimensions are 406 by 610. Also, if I rotate my original image before passing it to the function then also, no change. The image read still has original dimensions.
Please see example code and images for clarification:
So, below I have provided the input images: one is original and second one is rotated (I rotated it using windows rotate command, by right-clicking and selecting 'rotate right'). Output I get for both the images is same. It seems to me that rotating image did not actually change its shape. I think so because, when I try to put the rotated image here then also, it was showing the un-rotated version of it only (in the preview) so, I had to take a screen-capture of it and then, paste it here.
This is the code:
import cv2
import numpy as np
import sys
import os
image = cv2.imread("C:/img_8075.jpg")
print "image shape: ",image.shape
cv2.imshow("image",image)
cv2.waitKey(0)
image2 = cv2.imread("C:/img_8075_Rotated.jpg")
print "image shape: ",image2.shape
cv2.imshow("image",image2)
cv2.waitKey(0)
The result I get for this is: image shape: (406,610,3)
image shape: (406,610,3)
for both the images.
I am unable to paste input/output pictures here since, it says you should have '10 reputations' and I have just joined.
Any suggestions would be helpful. thanks!
I believe you are just getting the conventions mixed up. OpenCV Mat structures can be accessed (ROW,COLUMN).
So a 1920x1080 image will be 1080 ROWS by 1920 COLUMNS (1080,1920)
Commonly Mat.rows represent the image's height,and the Mat.cols represent the image's width.
Is there anyway to get image width and height without downloading from original location in PYTHON. I have an idea how to get image info when it is in our server. Buy no idea this can do with online resource image in PYTHON.
Finally done this as follow in python. Anyway have to download and get image info
import cStringIO
import urllib
import Image
file = urllib.urlopen('http://static.php.net/www.php.net/images/php.gif')
im = cStringIO.StringIO(file.read())
img = Image.open(im)
print img.format, img.size, img.mode
GIF (120, 67) P
width, height = img.size
print width, height
You can't. You must download a certain amount of the file before you get to the metadata that contains the dimensions of the image.