Make a thumbnail with PIL, enhanced way - python

I know there is a thumbnail method in PIL. What I want to make differently is how it resizes the original image. Assume I have a 300x360px vertical image. I want to resize it to a constrained box that is 150x100px horizontal image. So I need to find the smallest side of the original image, resize to it, and then crop the rest to the center from the largest side.
How can I do it?

from PIL import Image
width = 150
height = 100
infile = Image.open(in_filename)
im = infile.copy()
if im.size[0] >= im.size[1]:
im = im.resize((height * im.size[0]/im.size[1], height))
im = im.crop(((im.size[0] - width)/2, 0, (im.size[0] + width)/2, height))
else:
im = im.resize((width, width * im.size[1]/im.size[0]))
im = im.crop((0, (im.size[1] - height)/2, width, (im.size[1] + height)/2))
im.save(out_filename)
There might be a faster way to do this, but this should work.

Related

Shrink image using python?

import image
img = image.Image("test_image.png")
ratio = 0.5
#Make new image screen.
new_width = img.getWidth()*(ratio)
new_height = img.getHeight()*(ratio)
new_img = image.EmptyImage(new_width,new_height)
win = image.ImageWin(new_width,new_height)
for col in range(img.getHeight()):
for row in range(img.getWidth()):
p = img.getPixel(row*ratio, col*ratio) #Color value stays the same.
new_img.setPixel(row*ratio,col*ratio,p)
new_img.draw(win)
win.exitonclick()
This is my attempt to shrink an image using Python. I know my algorithm is clearly not correct but I can't seem to wrap my head around what I should do instead.
It's giving me an image of the two upper corners of the original image put side by side in my new smaller image screen. What am I missing?

How to change the color of a pixel using PIL?

I was trying to change pixel of an image in python using this question. If mode is 0, it changes first pixel in top right corner of image to grey(#C8C8C8). But it doesn't change. There is not enough documentation about draw.point(). What is the problem with this code?
import random
from PIL import Image, ImageDraw
mode = 0
image = Image.open("dom.jpg")
draw = ImageDraw.Draw(image)
width = image.size[0]
height = image.size[1]
pix = image.load()
string = "kod"
n = 0
if (mode == 0):
draw.point((0, 0), (200, 200, 200))
if(mode == 1):
print(pix[0,0][0])
image.save("dom.jpg", "JPEG")
del draw
Is using PIL a must in your case? If not then consider using OpenCV (cv2) for altering particular pixels of image.
Code which alter (0,0) pixel to (200,200,200) looks following way in opencv:
import cv2
img = cv2.imread('yourimage.jpg')
height = img.shape[0]
width = img.shape[1]
img[0][0] = [200,200,200]
cv2.imwrite('newimage.bmp',img)
Note that this code saves image in .bmp format - cv2 can also write .jpg images, but as jpg is generally lossy format, some small details might be lost. Keep in mind that in cv2 [0][0] is left upper corner and first value is y-coordinate of pixel, while second is x-coordinate, additionally color are three values from 0 to 255 (inclusive) in BGR order rather than RGB.
For OpenCV tutorials, including installation see this.

How to insert small image into corner of a photo with Python Pillow/Image?

I'm trying to insert a small 252 x 40 px image in the bottom right hand corner of an image using the Pillow library.
Currently, this is how I am trying to do it based off the general method for overlaying an image using Pillow/Image:
back = Image.open("images/background.png")
width, height = back.size
image1 = Image.open("images/picture.png")
width1, height1 = image1.size
image1 = image1.convert("RGBA")
back.paste(image, (width - width1, width - height1), image1)
Doing this fails to overlay the image. Any tips or other libraries I should use to accomplish this?
Figured it out.
back.paste(image1, (width - width1, height - height1), image2)

Adding borders to an image using python

I have a large number of images of a fixed size (say 500*500). I want to write a python script which will resize them to a fixed size (say 800*800) but will keep the original image at the center and fill the excess area with a fixed color (say black).
I am using PIL. I can resize the image using the resize function now, but that changes the aspect ratio. Is there any way to do this?
You can create a new image with the desired new size, and paste the old image in the center, then saving it. If you want, you can overwrite the original image (are you sure? ;o)
import Image
old_im = Image.open('someimage.jpg')
old_size = old_im.size
new_size = (800, 800)
new_im = Image.new("RGB", new_size) ## luckily, this is already black!
box = tuple((n - o) // 2 for n, o in zip(new_size, old_size))
new_im.paste(old_im, box)
new_im.show()
# new_im.save('someimage.jpg')
You can also set the color of the new border with a third argument of Image.new() (for example: Image.new("RGB", new_size, "White"))
Yes, there is.
Make something like this:
from PIL import Image, ImageOps
ImageOps.expand(Image.open('original-image.png'),border=300,fill='black').save('imaged-with-border.png')
You can write the same at several lines:
from PIL import Image, ImageOps
img = Image.open('original-image.png')
img_with_border = ImageOps.expand(img,border=300,fill='black')
img_with_border.save('imaged-with-border.png')
And you say that you have a list of images. Then you must use a cycle to process all of them:
from PIL import Image, ImageOps
for i in list-of-images:
img = Image.open(i)
img_with_border = ImageOps.expand(img,border=300,fill='black')
img_with_border.save('bordered-%s' % i)
Alternatively, if you are using OpenCV, they have a function called copyMakeBorder that allows you to add padding to any of the sides of an image. Beyond solid colors, they've also got some cool options for fancy borders like reflecting or extending the image.
import cv2
img = cv2.imread('image.jpg')
color = [101, 52, 152] # 'cause purple!
# border widths; I set them all to 150
top, bottom, left, right = [150]*4
img_with_border = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
Sources: OpenCV border tutorial and
OpenCV 3.1.0 Docs for copyMakeBorder
PIL's crop method can actually handle this for you by using numbers that are outside the bounding box of the original image, though it's not explicitly stated in the documentation. Negative numbers for left and top will add black pixels to those edges, while numbers greater than the original width and height for right and bottom will add black pixels to those edges.
This code accounts for odd pixel sizes:
from PIL import Image
with Image.open('/path/to/image.gif') as im:
old_size = im.size
new_size = (800, 800)
if new_size > old_size:
# Set number of pixels to expand to the left, top, right,
# and bottom, making sure to account for even or odd numbers
if old_size[0] % 2 == 0:
add_left = add_right = (new_size[0] - old_size[0]) // 2
else:
add_left = (new_size[0] - old_size[0]) // 2
add_right = ((new_size[0] - old_size[0]) // 2) + 1
if old_size[1] % 2 == 0:
add_top = add_bottom = (new_size[1] - old_size[1]) // 2
else:
add_top = (new_size[1] - old_size[1]) // 2
add_bottom = ((new_size[1] - old_size[1]) // 2) + 1
left = 0 - add_left
top = 0 - add_top
right = old_size[0] + add_right
bottom = old_size[1] + add_bottom
# By default, the added pixels are black
im = im.crop((left, top, right, bottom))
Instead of the 4-tuple, you could instead use a 2-tuple to add the same number of pixels on the left/right and top/bottom, or a 1-tuple to add the same number of pixels to all sides.
It is important to consider old dimension, new dimension and their difference here. If the difference is odd (not even), you will need to specify slightly different values for left, top, right and bottom borders.
Assume the old dimension is ow,oh and new one is nw,nh.
So, this would be the answer:
import Image, ImageOps
img = Image.open('original-image.png')
deltaw=nw-ow
deltah=nh-oh
ltrb_border=(deltaw/2,deltah/2,deltaw-(deltaw/2),deltah-(deltah/2))
img_with_border = ImageOps.expand(img,border=ltrb_border,fill='black')
img_with_border.save('imaged-with-border.png')
You can load the image with scipy.misc.imread as a numpy array. Then create an array with the desired background with numpy.zeros((height, width, channels)) and paste the image at the desired location:
import numpy as np
import scipy.misc
im = scipy.misc.imread('foo.jpg', mode='RGB')
height, width, channels = im.shape
# make canvas
im_bg = np.zeros((height, width, channels))
im_bg = (im_bg + 1) * 255 # e.g., make it white
# Your work: Compute where it should be
pad_left = ...
pad_top = ...
im_bg[pad_top:pad_top + height,
pad_left:pad_left + width,
:] = im
# im_bg is now the image with the background.
ximg = Image.open(qpath)
xwid,xhgt = func_ResizeImage(ximg)
qpanel_3 = tk.Frame(Body,width=xwid+10,height=xhgt+10,bg='white',bd=5)
ximg = ximg.resize((xwid,xhgt),Image.ANTIALIAS)
ximg = ImageTk.PhotoImage(ximg)
panel = tk.Label(qpanel_3,image=ximg)
panel.image = ximg
panel.grid(row = 2)
from PIL import Image
from PIL import ImageOps
img = Image.open("dem.jpg").convert("RGB")
This part will add black borders at the sides (10% of width)
img_side = ImageOps.expand(img, border=(int(0.1*img.size[0]),0,int(0.1*img.size[0]),0), fill=(0,0,0))
img_side.save("sunset-sides.jpg")
This part will add black borders at the bottom & top (10% of height)
img_updown = ImageOps.expand(img, border=(0,int(0.1*img.size[1]),0,int(0.1*img.size[1])), fill=(0,0,0))
img_updown.save("sunset-top_bottom.jpg")
This part will add black borders at the bottom,top & sides (10% of height-width)
img_updown_side = ImageOps.expand(img, border=(int(0.1*img.size[0]),int(0.1*img.size[1]),int(0.1*img.size[0]),int(0.1*img.size[1])), fill=(0,0,0))
img_updown_side.save("sunset-all_sides.jpg")
img.close()
img_side.close()
img_updown.close()
img_updown_side.close()

Does Python PIL resize maintain the aspect ratio?

Does PIL resize to the exact dimensions I give it no matter what? Or will it try to keep the aspect ratio if I give it something like the Image.ANTIALIAS argument?
Yes it will keep aspect ratio using thumbnail method:
image = Image.open(source_path)
image.thumbnail((500,500), Image.ANTIALIAS)
image.save(dest_path, "JPEG")
How do I resize an image using PIL and maintain its aspect ratio?
Image.resize from PIL will do exactly as told. No behind scenes aspect ratio stuff.
Recent Pillow version (since 8.3) have the following method to have an image resized to fit in given box with keeping aspect ratio.
image = ImageOps.contain(image, (2048,2048))
Yes. the thumbnail() method is what is needed here... One thing that hasn't been mentioned in this or other posts on the topic is that 'size' must be either a list or tuple. So, to resize to a maximum dimension of 500 pixels, you would call:
image.thumbnail((500,500), Image.ANTIALIAS)
See also this post on the subject:
How do I resize an image using PIL and maintain its aspect ratio?
No, it does not. But you can do something like the following:
im = PIL.Image.open("email.jpg"))
width, height = im.size
im = im.resize((width//2, height//2))
Here the height and width are divided by the same number, keeping the same aspect ratio.
Okay, so this requires a couple of lines of code to get what you want.
First you find the ratio, then you fix a dimension of the image that you want (height or width). In my case, I wanted height as 100px and let the width adjust accordingly, and finally use that ratio to find new height or new width.
So first find the dimensions:
width, height = logo_img.size
ratio = width / height
Then, fix one of the dimension:
new_height = 100
Then find new width for that height:
new_width = int(ratio * new_height)
logo_img = logo_img.resize((new_width, new_height))
It depends on your demand, if you want you can set a fixed height and width or if you want you can resize it with aspect-ratio.
For the aspect-ratio-wise resize you can try with the below codes :
To make the new image half the width and half the height of the original image:
from PIL import Image
im = Image.open("image.jpg")
resized_im = im.resize((round(im.size[0]*0.5), round(im.size[1]*0.5)))
#Save the cropped image
resized_im.save('resizedimage.jpg')
To resize with fixed width and ratio wise dynamic height :
from PIL import Image
new_width = 300
im = Image.open("img/7.jpeg")
concat = int(new_width/float(im.size[0]))
size = int((float(im.size[1])*float(concat)))
resized_im = im.resize((new_width,size), Image.ANTIALIAS)
#Save the cropped image
resized_im.save('resizedimage.jpg')
Recent versions of Pillow have some useful methods in PIL.ImageOps.
Depending on exactly what you're trying to achieve, you may be looking for one of these:
ImageOps.fit(image, size [, …]) (docs) – resizes your image, maintaining its aspect ratio, and crops it to fit the given size
ImageOps.contain(image, size [, …]) (docs) – resizes your image, maintaining its aspect ratio, so that it fits within the given size
One complete example:
import PIL.Image
class ImageUtils(object):
#classmethod
def resize_image(cls, image: PIL.Image.Image, width=None, height=None) -> PIL.Image.Image:
if height is None and width is not None:
height = image.height * width // image.width
elif width is None and height is not None:
width = image.width * height // image.height
elif height is None and width is None:
raise RuntimeError("At lease one of width and height must be present")
return image.resize((width, height))
def main():
ImageUtils.resize_image(PIL.Image.open("old.png"), width=100).save("new.png")
if __name__ == '__main__':
main()
I think following is the easiest way to do:
Img1 = img.resize((img.size),PIL.Image.ANTIALIAS)

Categories

Resources