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

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

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

Python PIL create gif fails

I was trying to create a gif using PIL as explained in:
http://www.pythoninformer.com/python-libraries/pillow/creating-animated-gif/
And the code they show to save a bunch of images "names" into a gif:
# Open all the frames
images = []
for n in names:
frame = Image.open(n)
images.append(frame)
# Save the frames as an animated GIF
images[0].save('anicircle.gif',
save_all=True,
append_images=images[1:],
duration=100,
loop=0)
However, when saving the gif it only saves one image, what I am doing wrong?
I am using PIL version 1.1.7 in python 2.7
I don't think there is anything wrong with your code. I would check what Pillow version you are using.
from PIL import Image
Image.PILLOW_VERSION
append_images was added in Pillow 3.4.0, so you will want to use at least that version.

How to force Pillow to resize an image to an arbitrary size?

I need to resize images, from all different sizes to 144x144.
All sizes: from 968x565, from 25x48, from 400x400, etc. Don't know the input. I'm using Pillow library from Python. I don't mind losing aspect ratio.
Problem is: when using resize method, some images get complete random sizes. Not always, but just some of them don't respect the arbitrary size of 144x144 I gave to the method. Here is a sample.
I tried resizing these images using Mac OS X Yosemite's Preview and it works flawlessly. What do I need to do in Pillow to force 144x144 size and work at least like Preview in mac?
I can't post the whole code, but the snippet that resizes is:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PIL import Image
im = Image.open('canvas_wheat.jpg')
im.resize((144, 144), Image.ANTIALIAS)
im.save('144.jpg', optimize=True, quality=20)
The problem you are getting there is that the resize method returns a
new image object, you are not storing anywhere. The original image im is the same that has been loaded.
Check the examples on this interactive session:
>>> from PIL import Image
>>> img = Image.open("image.jpg")
>>> img.size
(952, 804)
>>> img2 = img.resize((144,144), Image.ANTIALIAS)
>>> img.size
(952, 804)
>>> img2.size
(144, 144)
>>>

python imaging library save function

I have just done some image processing using the python image library (PIL) and I can't get the save function to work. the whole code works fine but it just wont save the resulting image. The code is below:
im=Image.new("rgb",(200,10),"#ddd")
draw=Image.draw.draw(im)
draw.text((10,10),"run away",fill="red")
im.save("g.jpeg")
Saving gives an error as unknown extension and even removing the dot doesn't help.
Use .jpg:
im.save("g.jpg")
The image library determines what encoder to use by the extension, but in certain versions of PIL the JPEG encoder do not register the .jpeg extension, only .jpg.
Another possibility is that your PIL installation doesn't support JPEG at all; try saving the image as a PNG, for example.
Replace
draw=Image.draw.draw(im)
with
draw = ImageDraw.Draw(im)
and make sure the height of the new image is tall enough to accomodate the text.
import Image
import ImageDraw
im = Image.new("RGB", (200, 30), "#ddd")
draw = ImageDraw.Draw(im)
draw.text((10, 10), "run away", fill="red")
im.save("g.jpeg")
yields
please save with .jpg extention eg:
im.save("g.jpg")

Resize image in Python without losing EXIF data

I need to resize jpg images with Python without losing the original image's EXIF data (metadata about date taken, camera model etc.). All google searches about python and images point to the PIL library which I'm currently using, but doesn't seem to be able to retain the metadata. The code I have so far (using PIL) is this:
img = Image.open('foo.jpg')
width,height = 800,600
if img.size[0] < img.size[1]:
width,height = height,width
resized_img = img.resize((width, height), Image.ANTIALIAS) # best down-sizing filter
resized_img.save('foo-resized.jpg')
Any ideas? Or other libraries that I could be using?
There is actually a really simple way of copying EXIF data from a picture to another with only PIL. Though it doesn't permit to modify the exif tags.
image = Image.open('test.jpg')
exif = image.info['exif']
# Your picture process here
image = image.rotate(90)
image.save('test_rotated.jpg', 'JPEG', exif=exif)
As you can see, the save function can take the exif argument which permits to copy the raw exif data in the new image when saving. You don't actually need any other lib if that's all you want to do. I can't seem to find any documentation on the save options and I don't even know if that's specific to Pillow or working with PIL too. (If someone has some kind of link, I would enjoy if they posted it in the comments)
import jpeg
jpeg.setExif(jpeg.getExif('foo.jpg'), 'foo-resized.jpg')
http://www.emilas.com/jpeg/
You can use pyexiv2 to copy EXIF data from source image. In the following example image is resized using PIL library, EXIF data copied with pyexiv2 and image size EXIF fields are set with new size.
def resize_image(source_path, dest_path, size):
# resize image
image = Image.open(source_path)
image.thumbnail(size, Image.ANTIALIAS)
image.save(dest_path, "JPEG")
# copy EXIF data
source_image = pyexiv2.Image(source_path)
source_image.readMetadata()
dest_image = pyexiv2.Image(dest_path)
dest_image.readMetadata()
source_image.copyMetadataTo(dest_image)
# set EXIF image size info to resized size
dest_image["Exif.Photo.PixelXDimension"] = image.size[0]
dest_image["Exif.Photo.PixelYDimension"] = image.size[1]
dest_image.writeMetadata()
# resizing local file
resize_image("41965749.jpg", "resized.jpg", (600,400))
Why not using ImageMagick?
It is quite a standard tool (for instance, it is the standard tool used by Gallery 2); I have never used it, however it has a python interface as well (or, you can also simply spawn the command) and most of all, should maintain EXIF information between all transformation.
Here's an updated answer as of 2018. piexif is a pure python library that for me installed easily via pip (pip install piexif) and worked beautifully (thank you, maintainers!). https://pypi.org/project/piexif/
The usage is very simple, a single line will replicate the accepted answer and copy all EXIF tags from the original image to the resized image:
import piexif
piexif.transplant("foo.jpg", "foo-resized.jpg")
I haven't tried yet, but it looks like you could also perform modifcations easily by using the load, dump, and insert functions as described in the linked documentation.
For pyexiv2 v0.3.2, the API documentation refers to the copy method to carry over EXIF data from one image to another. In this case it would be the EXIF data of the original image over to the resized image.
Going off #Maksym Kozlenko, the updated code for copying EXIF data is:
source_image = pyexiv2.ImageMetadata(source_path)
source_image.read()
dest_image = pyexiv2.ImageMetadata(dest_path)
dest_image.read()
source_image.copy(dest_image,exif=True)
dest_image.write()
You can use pyexiv2 to modify the file after saving it.
from PIL import Image
img_path = "/tmp/img.jpg"
img = Image.open(img_path)
exif = img.info['exif']
img.save("output_"+img_path, exif=exif)
Tested in Pillow 2.5.3
It seems #Depado's solution does not work for me, in my scenario the image does not even contain an exif segment.
pyexiv2 is hard to install on my Mac, instead I use the module pexif https://github.com/bennoleslie/pexif/blob/master/pexif.py. To "add exif segment" to an image does not contain exif info, I added the exif info contained in an image which owns a exif segment
from pexif import JpegFile
#get exif segment from an image
jpeg = JpegFile.fromFile(path_with_exif)
jpeg_exif = jpeg.get_exif()
#import the exif segment above to the image file which does not contain exif segment
jpeg = JpegFile.fromFile(path_without_exif)
exif = jpeg.import_exif(jpeg_exif)
jpeg.writeFile(path_without_exif)
Updated version of Maksym Kozlenko
Python3 and py3exiv2 v0.7
# Resize image and update Exif data
from PIL import Image
import pyexiv2
def resize_image(source_path, dest_path, size):
# resize image
image = Image.open(source_path)
# Using thumbnail, then 'size' is MAX width or weight
# so will retain aspect ratio
image.thumbnail(size, Image.ANTIALIAS)
image.save(dest_path, "JPEG")
# copy EXIF data
source_exif = pyexiv2.ImageMetadata(source_path)
source_exif.read()
dest_exif = pyexiv2.ImageMetadata(dest_path)
dest_exif.read()
source_exif.copy(dest_exif,exif=True)
# set EXIF image size info to resized size
dest_exif["Exif.Photo.PixelXDimension"] = image.size[0]
dest_exif["Exif.Photo.PixelYDimension"] = image.size[1]
dest_exif.write()
PIL handles EXIF data, doesn't it? Look in PIL.ExifTags.

Categories

Resources