Get image width and height without downloading in PYTHON - python

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.

Related

Information about image

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

Is it possible to get the size of an image using Bytes.IO?

I'm working on a project where two imported librairies are not working well with each other, I know it is possible to get the size of an image using :
from PIL import Image
img = Image.open(logo)
width, height = img.size
But I'd like to know if it is also possible to do that usin io ? I couldn't find anything on that
logo = request.FILES.get('logo')
img = io.BytesIO(logo.read())
... ?

How to download and rotate image onsite?

I use python with BeautifulSoup to download images, but all these images have to rotate 90 degree for view, for save time, I want to rotate it before save to disk, is there any easier way?
By the way, I can download images without rotation.
This is the way to download an image -
# Import Pillow:
from PIL import Image
import urllib2
from io import StringIO
url = "http://matthiaseisen.com/pp/static/p0201_2.jpg"
# Load the original image:
img = Image.open(urllib2.urlopen(url))
# Counterclockwise 90 degree
img3 = img.rotate(90)
img3.save("img3.jpg")
# Clockwise
img4 = img.rotate(-90)
img4.save("img4.jpg")
# Diable Cropping
img5 = img.rotate(90, expand=True)
img5.save("img5.jpg")
So basically you can extract the src attributes from the img tags and try running the above piece of code. You may have to do some handling where there are Relative URLs used. [concatenating the domain and all]

Create, Modify, and Save an image in python 3.x

I'm currently making a program that renders information to a buffer, and I want to save the information as an image file of some sort to my working directory. I've seen some examples using PIL, but that library isn't supported for python 3.x. Are there better alternatives?
First uninstall PIL than install Pillow
its a PIL's clone which works on python 3.x.
from PIL import Image
img = Image.open("test1.jpg") #jpg, png, etc.
pix = img.load()
print img.size #Get the width and height of the image for iterating over
print pix[15,15] #Get the RGBA Value of the a pixel of an image
pix[15, 15] = value # Set the RGBA Value of the image (tuple)
img.save("out.jpg") # Saves the modified pixels to image

Preserve time stamp when shrinking an image

My digital camera takes pictures with a very high resolution, and I have a PIL script to shrink them to 800x600 (or 600x800). However, it would be nice for the resultant file to retain the original timestamp. I noticed in the docs that I can use a File object instead of a name in PIL's image save method, but I don't know if that will help or not.
My code is basically
name, ext = os.path.splitext(filename)
# open an image file (.bmp,.jpg,.png,.gif) you have in the working folder
image = Image.open(filename)
width = 800
height = 600
w, h = image.size
if h > w:
width = 600
height = 800
name = name + ".jpg"
shunken = image.resize((width, height), Image.ANTIALIAS)
shunken.save(name)
Thank you for any help you can give!
Use shutil.copystat
It appears that PIL does not save EXIF metadata.
To copy the EXIF data using Python you could use
pyexiv2. This is how Phatch, a batch photo resizer program written in Python, deals with EXIF data, for example.
I'm not sure if you're using Ubuntu, but if so, installation is easy since pyexiv2 is provided by python-pyexiv2 package.
Edit: If you don't mind losing the EXIF metadata, and would simply like to use the EXIF datetime stamp as the resized image's modification date, then you can do it without the pyexiv2 package, thus saving you an extra dependency. Here's how:
import os
import time
import Image
import ExifTags # This is provided by PIL
img=Image.open(filename,'r')
PIL can read EXIF data, but cannot yet write EXIF data. We can access the data using the _getexif() method:
d = dict((ExifTags.TAGS[k], v) for k, v in img._getexif().items())
print(d['DateTimeOriginal'])
Parsing the timestamp may depend on what format the camera uses. This works for my camera; YMMV. The dateutils package allows you to parse a wide variety of timestamps without you having to pre-specify the format, but that's another story.
timestamp=time.strptime(d['DateTimeOriginal'],"%Y:%m:%d %H:%M:%S")
Here's an alternative way to swap the width and height:
w, h = img.size
width,height = 800,600
if h > w: width,height = height,width
Resizing the image, and using os.utime to fix the atime and mtime:
filename = filename + "-800x600.jpg"
shunken = img.resize((width, height), Image.ANTIALIAS)
shunken.save(filename)
st = os.stat(filename)
os.utime(filename,(st.st_atime,time.mktime(timestamp)))

Categories

Resources