Crop a PNG image to its minimum size - python

How to cut off the blank border area of a PNG image and shrink it to its minimum size using Python?
NB: The border size is not a fixed value, but may vary per image.

PIL's getbbox is working for me
im.getbbox() => 4-tuple or None
Calculates the bounding box of the
non-zero regions in the image. The
bounding box is returned as a 4-tuple
defining the left, upper, right, and
lower pixel coordinate. If the image
is completely empty, this method
returns None.
Code Sample that I tried, I have tested with bmp, but it should work for png too.
import Image
im = Image.open("test.bmp")
im.size # (364, 471)
im.getbbox() # (64, 89, 278, 267)
im2 = im.crop(im.getbbox())
im2.size # (214, 178)
im2.save("test2.bmp")

Here is ready-to-use solution:
import numpy as np
from PIL import Image
def bbox(im):
a = np.array(im)[:,:,:3] # keep RGB only
m = np.any(a != [255, 255, 255], axis=2)
coords = np.argwhere(m)
y0, x0, y1, x1 = *np.min(coords, axis=0), *np.max(coords, axis=0)
return (x0, y0, x1+1, y1+1)
im = Image.open('test.png')
print(bbox(im)) # (33, 12, 223, 80)
im2 = im.crop(bbox(im))
im2.save('test_cropped.png')
Example input (download link if you want to try):
Output:

I had the same problem today. Here is my solution to crop the transparent borders. Just throw this script in your folder with your batch .png files:
from PIL import Image
import numpy as np
from os import listdir
def crop(png_image_name):
pil_image = Image.open(png_image_name)
np_array = np.array(pil_image)
blank_px = [255, 255, 255, 0]
mask = np_array != blank_px
coords = np.argwhere(mask)
x0, y0, z0 = coords.min(axis=0)
x1, y1, z1 = coords.max(axis=0) + 1
cropped_box = np_array[x0:x1, y0:y1, z0:z1]
pil_image = Image.fromarray(cropped_box, 'RGBA')
print(pil_image.width, pil_image.height)
pil_image.save(png_image_name)
print(png_image_name)
for f in listdir('.'):
if f.endswith('.png'):
crop(f)

https://gist.github.com/3141140
import Image
import sys
import glob
# Trim all png images with alpha in a folder
# Usage "python PNGAlphaTrim.py ../someFolder"
try:
folderName = sys.argv[1]
except :
print "Usage: python PNGPNGAlphaTrim.py ../someFolder"
sys.exit(1)
filePaths = glob.glob(folderName + "/*.png") #search for all png images in the folder
for filePath in filePaths:
image=Image.open(filePath)
image.load()
imageSize = image.size
imageBox = image.getbbox()
imageComponents = image.split()
if len(imageComponents) < 4: continue #don't process images without alpha
rgbImage = Image.new("RGB", imageSize, (0,0,0))
rgbImage.paste(image, mask=imageComponents[3])
croppedBox = rgbImage.getbbox()
if imageBox != croppedBox:
cropped=image.crop(croppedBox)
print filePath, "Size:", imageSize, "New Size:",croppedBox
cropped.save(filePath)

You can use PIL to find rows and cols of your image that are made up purely of your border color.
Using this information, you can easily determine the extents of the inlaid image.
PIL again will then allow you to crop the image to remove the border.

I think it's necessary to supplement #Frank Krueger's answer. He makes a good point, but it doesn't include how to properly crop extra border color out of an image. I found that here. Specifically, I found this useful:
from PIL import Image, ImageChops
def trim(im):
bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
diff = ImageChops.difference(im, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
im = Image.open("bord3.jpg")
im = trim(im)
im.show()

The other answers did not work for me while writing a Blender script (cannot use PIL), so maybe someone else will find this useful.
import numpy as np
def crop(crop_file):
"""crop the image, removing invisible borders"""
image = bpy.data.images.load(crop_file, check_existing=False)
w, h = image.size
print("Original size: " + str(w) + " x " + str(h))
linear_pixels = image.pixels[:]
pixels4d = np.reshape(linear_pixels, (h, w, 4))
mask = pixels4d [:,:,3] != 0.
coords = np.argwhere(mask)
y0, x0 = coords.min(axis=0)
y1, x1 = coords.max(axis=0) + 1
cropped_box = pixels4d[y0:y1, x0:x1, :]
w1, h1 = x1 - x0, y1 - y0
print("Crop size: " + str(w1) + " x " + str(h1))
temp_image = bpy.data.images.new(crop_file, alpha=True, width=w1, height=h1)
temp_image.pixels[:] = cropped_box.ravel()
temp_image.filepath_raw = crop_file
temp_image.file_format = 'PNG'
temp_image.alpha_mode = 'STRAIGHT'
temp_image.save()

Related

How to change bit depth? [duplicate]

I'm using PIL to convert a transparent PNG image uploaded with Django to a JPG file. The output looks broken.
Source file
Code
Image.open(object.logo.path).save('/tmp/output.jpg', 'JPEG')
or
Image.open(object.logo.path).convert('RGB').save('/tmp/output.png')
Result
Both ways, the resulting image looks like this:
Is there a way to fix this? I'd like to have white background where the transparent background used to be.
Solution
Thanks to the great answers, I've come up with the following function collection:
import Image
import numpy as np
def alpha_to_color(image, color=(255, 255, 255)):
"""Set all fully transparent pixels of an RGBA image to the specified color.
This is a very simple solution that might leave over some ugly edges, due
to semi-transparent areas. You should use alpha_composite_with color instead.
Source: http://stackoverflow.com/a/9166671/284318
Keyword Arguments:
image -- PIL RGBA Image object
color -- Tuple r, g, b (default 255, 255, 255)
"""
x = np.array(image)
r, g, b, a = np.rollaxis(x, axis=-1)
r[a == 0] = color[0]
g[a == 0] = color[1]
b[a == 0] = color[2]
x = np.dstack([r, g, b, a])
return Image.fromarray(x, 'RGBA')
def alpha_composite(front, back):
"""Alpha composite two RGBA images.
Source: http://stackoverflow.com/a/9166671/284318
Keyword Arguments:
front -- PIL RGBA Image object
back -- PIL RGBA Image object
"""
front = np.asarray(front)
back = np.asarray(back)
result = np.empty(front.shape, dtype='float')
alpha = np.index_exp[:, :, 3:]
rgb = np.index_exp[:, :, :3]
falpha = front[alpha] / 255.0
balpha = back[alpha] / 255.0
result[alpha] = falpha + balpha * (1 - falpha)
old_setting = np.seterr(invalid='ignore')
result[rgb] = (front[rgb] * falpha + back[rgb] * balpha * (1 - falpha)) / result[alpha]
np.seterr(**old_setting)
result[alpha] *= 255
np.clip(result, 0, 255)
# astype('uint8') maps np.nan and np.inf to 0
result = result.astype('uint8')
result = Image.fromarray(result, 'RGBA')
return result
def alpha_composite_with_color(image, color=(255, 255, 255)):
"""Alpha composite an RGBA image with a single color image of the
specified color and the same size as the original image.
Keyword Arguments:
image -- PIL RGBA Image object
color -- Tuple r, g, b (default 255, 255, 255)
"""
back = Image.new('RGBA', size=image.size, color=color + (255,))
return alpha_composite(image, back)
def pure_pil_alpha_to_color_v1(image, color=(255, 255, 255)):
"""Alpha composite an RGBA Image with a specified color.
NOTE: This version is much slower than the
alpha_composite_with_color solution. Use it only if
numpy is not available.
Source: http://stackoverflow.com/a/9168169/284318
Keyword Arguments:
image -- PIL RGBA Image object
color -- Tuple r, g, b (default 255, 255, 255)
"""
def blend_value(back, front, a):
return (front * a + back * (255 - a)) / 255
def blend_rgba(back, front):
result = [blend_value(back[i], front[i], front[3]) for i in (0, 1, 2)]
return tuple(result + [255])
im = image.copy() # don't edit the reference directly
p = im.load() # load pixel array
for y in range(im.size[1]):
for x in range(im.size[0]):
p[x, y] = blend_rgba(color + (255,), p[x, y])
return im
def pure_pil_alpha_to_color_v2(image, color=(255, 255, 255)):
"""Alpha composite an RGBA Image with a specified color.
Simpler, faster version than the solutions above.
Source: http://stackoverflow.com/a/9459208/284318
Keyword Arguments:
image -- PIL RGBA Image object
color -- Tuple r, g, b (default 255, 255, 255)
"""
image.load() # needed for split()
background = Image.new('RGB', image.size, color)
background.paste(image, mask=image.split()[3]) # 3 is the alpha channel
return background
Performance
The simple non-compositing alpha_to_color function is the fastest solution, but leaves behind ugly borders because it does not handle semi transparent areas.
Both the pure PIL and the numpy compositing solutions give great results, but alpha_composite_with_color is much faster (8.93 msec) than pure_pil_alpha_to_color (79.6 msec). If numpy is available on your system, that's the way to go. (Update: The new pure PIL version is the fastest of all mentioned solutions.)
$ python -m timeit "import Image; from apps.front import utils; i = Image.open(u'logo.png'); i2 = utils.alpha_to_color(i)"
10 loops, best of 3: 4.67 msec per loop
$ python -m timeit "import Image; from apps.front import utils; i = Image.open(u'logo.png'); i2 = utils.alpha_composite_with_color(i)"
10 loops, best of 3: 8.93 msec per loop
$ python -m timeit "import Image; from apps.front import utils; i = Image.open(u'logo.png'); i2 = utils.pure_pil_alpha_to_color(i)"
10 loops, best of 3: 79.6 msec per loop
$ python -m timeit "import Image; from apps.front import utils; i = Image.open(u'logo.png'); i2 = utils.pure_pil_alpha_to_color_v2(i)"
10 loops, best of 3: 1.1 msec per loop
Here's a version that's much simpler - not sure how performant it is. Heavily based on some django snippet I found while building RGBA -> JPG + BG support for sorl thumbnails.
from PIL import Image
png = Image.open(object.logo.path)
png.load() # required for png.split()
background = Image.new("RGB", png.size, (255, 255, 255))
background.paste(png, mask=png.split()[3]) # 3 is the alpha channel
background.save('foo.jpg', 'JPEG', quality=80)
Result #80%
Result # 50%
By using Image.alpha_composite, the solution by Yuji 'Tomita' Tomita become simpler. This code can avoid a tuple index out of range error if png has no alpha channel.
from PIL import Image
png = Image.open(img_path).convert('RGBA')
background = Image.new('RGBA', png.size, (255, 255, 255))
alpha_composite = Image.alpha_composite(background, png)
alpha_composite.save('foo.jpg', 'JPEG', quality=80)
The transparent parts mostly have RGBA value (0,0,0,0). Since the JPG has no transparency, the jpeg value is set to (0,0,0), which is black.
Around the circular icon, there are pixels with nonzero RGB values where A = 0. So they look transparent in the PNG, but funny-colored in the JPG.
You can set all pixels where A == 0 to have R = G = B = 255 using numpy like this:
import Image
import numpy as np
FNAME = 'logo.png'
img = Image.open(FNAME).convert('RGBA')
x = np.array(img)
r, g, b, a = np.rollaxis(x, axis = -1)
r[a == 0] = 255
g[a == 0] = 255
b[a == 0] = 255
x = np.dstack([r, g, b, a])
img = Image.fromarray(x, 'RGBA')
img.save('/tmp/out.jpg')
Note that the logo also has some semi-transparent pixels used to smooth the edges around the words and icon. Saving to jpeg ignores the semi-transparency, making the resultant jpeg look quite jagged.
A better quality result could be made using imagemagick's convert command:
convert logo.png -background white -flatten /tmp/out.jpg
To make a nicer quality blend using numpy, you could use alpha compositing:
import Image
import numpy as np
def alpha_composite(src, dst):
'''
Return the alpha composite of src and dst.
Parameters:
src -- PIL RGBA Image object
dst -- PIL RGBA Image object
The algorithm comes from http://en.wikipedia.org/wiki/Alpha_compositing
'''
# http://stackoverflow.com/a/3375291/190597
# http://stackoverflow.com/a/9166671/190597
src = np.asarray(src)
dst = np.asarray(dst)
out = np.empty(src.shape, dtype = 'float')
alpha = np.index_exp[:, :, 3:]
rgb = np.index_exp[:, :, :3]
src_a = src[alpha]/255.0
dst_a = dst[alpha]/255.0
out[alpha] = src_a+dst_a*(1-src_a)
old_setting = np.seterr(invalid = 'ignore')
out[rgb] = (src[rgb]*src_a + dst[rgb]*dst_a*(1-src_a))/out[alpha]
np.seterr(**old_setting)
out[alpha] *= 255
np.clip(out,0,255)
# astype('uint8') maps np.nan (and np.inf) to 0
out = out.astype('uint8')
out = Image.fromarray(out, 'RGBA')
return out
FNAME = 'logo.png'
img = Image.open(FNAME).convert('RGBA')
white = Image.new('RGBA', size = img.size, color = (255, 255, 255, 255))
img = alpha_composite(img, white)
img.save('/tmp/out.jpg')
Here's a solution in pure PIL.
def blend_value(under, over, a):
return (over*a + under*(255-a)) / 255
def blend_rgba(under, over):
return tuple([blend_value(under[i], over[i], over[3]) for i in (0,1,2)] + [255])
white = (255, 255, 255, 255)
im = Image.open(object.logo.path)
p = im.load()
for y in range(im.size[1]):
for x in range(im.size[0]):
p[x,y] = blend_rgba(white, p[x,y])
im.save('/tmp/output.png')
It's not broken. It's doing exactly what you told it to; those pixels are black with full transparency. You will need to iterate across all pixels and convert ones with full transparency to white.
import numpy as np
import PIL
def convert_image(image_file):
image = Image.open(image_file) # this could be a 4D array PNG (RGBA)
original_width, original_height = image.size
np_image = np.array(image)
new_image = np.zeros((np_image.shape[0], np_image.shape[1], 3))
# create 3D array
for each_channel in range(3):
new_image[:,:,each_channel] = np_image[:,:,each_channel]
# only copy first 3 channels.
# flushing
np_image = []
return new_image
from PIL import Image
def fig2img ( fig ):
"""
#brief Convert a Matplotlib figure to a PIL Image in RGBA format and return it
#param fig a matplotlib figure
#return a Python Imaging Library ( PIL ) image
"""
# put the figure pixmap into a numpy array
buf = fig2data ( fig )
w, h, d = buf.shape
return Image.frombytes( "RGBA", ( w ,h ), buf.tostring( ) )
def fig2data ( fig ):
"""
#brief Convert a Matplotlib figure to a 4D numpy array with RGBA channels and return it
#param fig a matplotlib figure
#return a numpy 3D array of RGBA values
"""
# draw the renderer
fig.canvas.draw ( )
# Get the RGBA buffer from the figure
w,h = fig.canvas.get_width_height()
buf = np.fromstring ( fig.canvas.tostring_argb(), dtype=np.uint8 )
buf.shape = ( w, h, 4 )
# canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode
buf = np.roll ( buf, 3, axis = 2 )
return buf
def rgba2rgb(img, c=(0, 0, 0), path='foo.jpg', is_already_saved=False, if_load=True):
if not is_already_saved:
background = Image.new("RGB", img.size, c)
background.paste(img, mask=img.split()[3]) # 3 is the alpha channel
background.save(path, 'JPEG', quality=100)
is_already_saved = True
if if_load:
if is_already_saved:
im = Image.open(path)
return np.array(im)
else:
raise ValueError('No image to load.')

OpenCV subplots images with titles and space around borders

I am looking to display some images in OpenCV Python with titles and borders around the each subplot. something like this (courtesy of the following stackoverflow post: OpenCV (Python) video subplots):
WHAT I WANT:
But I only manage to get this with that code adapted.
import cv2
im1 = cv2.imread('Lenna.png')
final_frame = cv2.hconcat((im1, im1))
cv2.imshow('lena', final_frame)
WHAT I HAVE
Is it possible to obtain this using OpenCV?
I know a workaround would be to put text on the images, but that's not what I want because it will cover important information that way.
UPDATE
My bad, I didn't specify initially: I have 4 subplots (so 4 different images) and not two like in the example. Also, I want the solution to be as fast as possible since I have video (time restrictions)
I have a pretty quick and dirty solution. You can refine it to suit your needs. I have the explanation alongside the code as well:
import cv2
import numpy as np
img1 = cv2.imread('lena.jpg')
#--- Here I am creating the border---
black = [0,0,0] #---Color of the border---
constant=cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=black )
cv2.imshow('constant',constant)
You can find many other options for different borders ON THIS PAGE
#--- Here I created a violet background to include the text ---
violet= np.zeros((100, constant.shape[1], 3), np.uint8)
violet[:] = (255, 0, 180)
#--- I then concatenated it vertically to the image with the border ---
vcat = cv2.vconcat((violet, constant))
cv2.imshow('vcat', vcat)
#--- Now I included some text ---
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(vcat,'FRAME',(30,50), font, 2,(0,0,0), 3, 0)
cv2.imshow('Text', vcat)
#--- I finally concatenated both the above images horizontally---
final_img = cv2.hconcat((vcat, vcat))
cv2.imshow('Final', final_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
The general idea would be to create a new image with width += width/10 and height += height/20. Write some text as heading and place the input image along the center as:
import cv2
import numpy as np
img = cv2.imread("/Users/anmoluppal/Downloads/Lenna.png")
height, width, ch = img.shape
new_width, new_height = width + width/20, height + height/8
# Crate a new canvas with new width and height.
canvas = np.ones((new_height, new_width, ch), dtype=np.uint8) * 125
# New replace the center of canvas with original image
padding_top, padding_left = 60, 10
if padding_top + height < new_height and padding_left + width < new_width:
canvas[padding_top:padding_top + height, padding_left:padding_left + width] = img
else:
print "The Given padding exceeds the limits."
text1 = "Sample Image 1"
text2 = "Sample Image 2"
img1 = cv2.putText(canvas.copy(), text1, (int(0.25*width), 30), cv2.FONT_HERSHEY_COMPLEX, 1, np.array([255, 0, 0]))
img2 = cv2.putText(canvas.copy(), text2, (int(0.25*width), 30), cv2.FONT_HERSHEY_COMPLEX, 1, np.array([255, 0, 0]))
final = cv2.hconcat((img1, img2))
cv2.imwrite("./debug.png", final)
I used the other answers to make a generalizable function which works for arbitrary row/columns:
def cvSubplot(imgs, # 2d np array of imgs (each img an np arrays of depth 1 or 3).
pad=10, # number of pixels to use for padding between images. must be even
titles=None, # (optional) np array of subplot titles
win_name='CV Subplot' # name of cv2 window
):
'''
Makes cv2 based subplots. Useful to plot image in actual pixel size
'''
rows, cols = imgs.shape
subplot_shapes = np.array([list(map(np.shape, x)) for x in imgs])
sp_height, sp_width, depth = np.max(np.max(subplot_shapes, axis=0), axis=0)
title_pad = 30
if titles is not None:
pad_top = pad + title_pad
else:
pad_top = pad
frame = np.zeros((rows*(sp_height+pad_top), cols*(sp_width+pad), depth ))
for r in range(rows):
for c in range(cols):
img = imgs[r, c]
h, w, _ = img.shape
y0 = r * (sp_height+pad_top) + pad_top//2
x0 = c * (sp_width+pad) + pad//2
frame[y0:y0+h, x0:x0+w, :] = img
if titles is not None:
frame = cv2.putText(frame, titles[r, c], (x0, y0-title_pad//4), cv2.FONT_HERSHEY_COMPLEX, .5, (255,255,255))
cv2.imshow(win_name, frame)
cv2.waitKey(0)
Below is an example usage:
import cv2
import numpy as np
a1 = np.random.random((40,400,1))
a2 = np.random.random((200,200,1))
a3 = np.random.random((100,100,1))
a4 = np.random.random((300,150,1))
a5 = np.random.random((100,150,1))
filler = np.zeros((0,0,1))
titles = np.array([['A', 'B', 'C'], ['D', 'E', 'Filler']])
imgs = np.array([[a1, a2, a3], [a4, a5, filler]])
cvSubplot(imgs, pad=20, titles=titles)
That script produces the following cv2 image:

How do I crop an image on a white background with python?

I am scanning old photos, so I have the image and a white background from the scanner. My aim is to take the picture, removing the white background. How can I do that ?
An example picture is the following:
My simple approach:
import os
import time
from PIL import Image
from collections import Counter
import numpy as np
def get_cropped_image(image, crop_folder, threshold):
image_name = image.split("\\")[-1]
im = Image.open(image)
pixels = im.load()
width, height = im.size
rows = []
for h_index in xrange(height):
row = []
for w_index in xrange(width):
row.append(pixels[((w_index, h_index))])
color_count = Counter(row)[(255, 255, 255)] / float(len(row))
rows.append([h_index, color_count])
columns = []
for w_index in xrange(width):
column = []
for h_index in xrange(height):
column.append(im.getpixel((w_index, h_index)))
color_count = Counter(column)[(255, 255, 255)] / float(len(column))
columns.append([w_index, color_count])
image_data = csv.writer(open("image_data.csv", "wb")).writerows(zip(rows, columns))
rows_indexes = [i[0] for i in rows if i[1] < threshold]
columns_indexes = [i[0] for i in columns if i[1] < threshold]
x1, y1, x2, y2 = columns_indexes[0], rows_indexes[0], columns_indexes[-1], rows_indexes[-1]
im.crop((x1, y1, x2, y2)).save(os.path.join(cropped_folder, "c_" + image_name))
In the example below, I create a mask by selecting all pixels that are close to white (close, because the values right outside the area of interest are not exactly white). I then invert the mask to find pixels potentially belonging to the image. I then calculate the bounding box of those pixels, and use it to extract the region of interest.
from skimage import io, img_as_float
import matplotlib.pyplot as plt
import numpy as np
image = img_as_float(io.imread('universe.jpg'))
# Select all pixels almost equal to white
# (almost, because there are some edge effects in jpegs
# so the boundaries may not be exactly white)
white = np.array([1, 1, 1])
mask = np.abs(image - white).sum(axis=2) < 0.05
# Find the bounding box of those pixels
coords = np.array(np.nonzero(~mask))
top_left = np.min(coords, axis=1)
bottom_right = np.max(coords, axis=1)
out = image[top_left[0]:bottom_right[0],
top_left[1]:bottom_right[1]]
plt.imshow(out)
plt.show()

Python PIL: image crop with black area if not enough photo region

I want to make thumb and crop it to needed size. It works fine, but if my new thumb area is smaller than crop one, all empty space fills with black color.
Code:
import os
from PIL import Image
def resize(file_path):
file, ext = os.path.splitext(file_path)
im = Image.open(file_path)
size = (100, 'auto')
new_path = file + "_.jpg"
im.thumbnail(size, Image.ANTIALIAS)
region = im.crop((0, 0, 100, 100))
region.save(new_path, "JPEG")
Maybe there is some option like max_height for crop method or anything else?
Thanks!
You will need to apply some simple algorithm there instead of a blind cropping.
Get the square of maximum size possible in the image with square center aligning with the center of the image.
Square of maximum size would be having side equal to max of height or width of the image.
After getting the square, resample it to the size of your thumbnail dimensions.
This should work fine for most images, however if you are generating thumbnails for face images, this might not be a good method, and you might need some face recognition techniques for better output.
Are you trying to only conditionally crop the image if its LARGER than 100x100?
If so,
def resize(file_path):
file, ext = os.path.splitext(file_path)
im = Image.open(file_path)
size = (100, 'auto')
new_path = file + "_.jpg"
im.thumbnail(size, Image.ANTIALIAS)
if im.size[1] > 100:
im = im.crop((0, 0, 100, 100))
im.save(new_path, "JPEG")
I found solution:
import os
from PIL import Image
def resize(file_path):
file, ext = os.path.splitext(file_path)
im = Image.open(file_path)
size = (100, 'auto')
new_path = file + "_.jpg"
im.thumbnail(size)
(width, height) = im.size
if height >= width: box = (0, 0, 100, 100)
else: box = (0, 0, 100, height)
region = im.crop(box)
region.save(new_path, "JPEG")
Thanks for your responses!
I would do it this way:
If the image is wide, then scale it to be 100px tall. If it's tall, scale it to be 100px wide.
Crop out the middle 100x100.
def resize(file_path):
file, ext = os.path.splitext(file_path)
im = Image.open(file_path)
w, h = im.size
size = (100, 'auto') if h > w else ('auto', 100)
new_path = file + "_.jpg"
im.thumbnail(size, Image.ANTIALIAS)
w, h = im.size
region = im.crop((w/2 - 50, h/2 - 50, w/2 + 50, h/2 + 50))
region.save(new_path, "JPEG")

How to Split Image Into Multiple Pieces in Python

I'm trying to split a photo into multiple pieces using PIL.
def crop(Path,input,height,width,i,k,x,y,page):
im = Image.open(input)
imgwidth = im.size[0]
imgheight = im.size[1]
for i in range(0,imgheight-height/2,height-2):
print i
for j in range(0,imgwidth-width/2,width-2):
print j
box = (j, i, j+width, i+height)
a = im.crop(box)
a.save(os.path.join(Path,"PNG","%s" % page,"IMG-%s.png" % k))
k +=1
but it doesn't seem to be working. It splits the photo but not in an exact way (you can try it).
Splitting image to tiles of MxN pixels (assuming im is numpy.ndarray):
tiles = [im[x:x+M,y:y+N] for x in range(0,im.shape[0],M) for y in range(0,im.shape[1],N)]
In the case you want to split the image to four pieces:
M = im.shape[0]//2
N = im.shape[1]//2
tiles[0] holds the upper left tile
Edit: I believe this answer missed the intent to cut an image into rectangles in columns and rows. This answer cuts only into rows. It looks like other answers cut in columns and rows.
Simpler than all these is to use a wheel someone else invented :) It may be more involved to set up, but then it's a snap to use.
These instructions are for Windows 7; they may need to be adapted for other OSs.
Get and install pip from here.
Download the install archive, and extract it to your root Python installation directory. Open a console and type (if I recall correctly):
python get-pip.py install
Then get and install the image_slicer module via pip, by entering the following command at the console:
python -m pip install image_slicer
Copy the image you want to slice into the Python root directory, open a python shell (not the "command line"), and enter these commands:
import image_slicer
image_slicer.slice('huge_test_image.png', 14)
The beauty of this module is that it
Is installed in python
Can invoke an image split with two lines of code
Accepts any even number as an image slice parameter (e.g. 14 in this example)
Takes that parameter and automagically splits the given image into so many slices, and auto-saves the resultant numbered tiles in the same directory, and finally
Has a function to stitch the image tiles back together (which I haven't yet tested); files apparently must be named after the convention which you will see in the split files after testing the image_slicer.slice function.
from PIL import Image
def crop(path, input, height, width, k, page, area):
im = Image.open(input)
imgwidth, imgheight = im.size
for i in range(0,imgheight,height):
for j in range(0,imgwidth,width):
box = (j, i, j+width, i+height)
a = im.crop(box)
try:
o = a.crop(area)
o.save(os.path.join(path,"PNG","%s" % page,"IMG-%s.png" % k))
except:
pass
k +=1
As an alternative solution, we will construct the tiles by generating a grid of coordinates using itertools.product. We will ignore partial tiles on the edges, only iterating through the cartesian product between the two intervals, i.e. range(0, h-h%d, d) X range(0, w-w%d, d).
Given filename: the image file name, d: the tile size, dir_in: the path to the directory containing the image, and dir_out: the directory where tiles will be outputted:
from PIL import Image
from itertools import product
def tile(filename, dir_in, dir_out, d):
name, ext = os.path.splitext(filename)
img = Image.open(os.path.join(dir_in, filename))
w, h = img.size
grid = product(range(0, h-h%d, d), range(0, w-w%d, d))
for i, j in grid:
box = (j, i, j+d, i+d)
out = os.path.join(dir_out, f'{name}_{i}_{j}{ext}')
img.crop(box).save(out)
crop would be a more reusable
function if you separate the
cropping code from the
image saving
code. It would also make the call
signature simpler.
im.crop returns a
Image._ImageCrop instance. Such
instances do not have a save method.
Instead, you must paste the
Image._ImageCrop instance onto a
new Image.Image
Your ranges do not have the right
step sizes. (Why height-2 and not
height? for example. Why stop at
imgheight-(height/2)?).
So, you might try instead something like this:
import Image
import os
def crop(infile,height,width):
im = Image.open(infile)
imgwidth, imgheight = im.size
for i in range(imgheight//height):
for j in range(imgwidth//width):
box = (j*width, i*height, (j+1)*width, (i+1)*height)
yield im.crop(box)
if __name__=='__main__':
infile=...
height=...
width=...
start_num=...
for k,piece in enumerate(crop(infile,height,width),start_num):
img=Image.new('RGB', (height,width), 255)
img.paste(piece)
path=os.path.join('/tmp',"IMG-%s.png" % k)
img.save(path)
Here is a concise, pure-python solution that works in both python 3 and 2:
from PIL import Image
infile = '20190206-135938.1273.Easy8thRunnersHopefully.jpg'
chopsize = 300
img = Image.open(infile)
width, height = img.size
# Save Chops of original image
for x0 in range(0, width, chopsize):
for y0 in range(0, height, chopsize):
box = (x0, y0,
x0+chopsize if x0+chopsize < width else width - 1,
y0+chopsize if y0+chopsize < height else height - 1)
print('%s %s' % (infile, box))
img.crop(box).save('zchop.%s.x%03d.y%03d.jpg' % (infile.replace('.jpg',''), x0, y0))
Notes:
The crops that go over the right and bottom of the original image are adjusted to the original image limit and contain only the original pixels.
It's easy to choose a different chopsize for w and h by using two chopsize vars and replacing chopsize as appropriate in the code above.
Not sure if this is the most efficient answer, but it works for me:
import os
import glob
from PIL import Image
Image.MAX_IMAGE_PIXELS = None # to avoid image size warning
imgdir = "/path/to/image/folder"
# if you want file of a specific extension (.png):
filelist = [f for f in glob.glob(imgdir + "**/*.png", recursive=True)]
savedir = "/path/to/image/folder/output"
start_pos = start_x, start_y = (0, 0)
cropped_image_size = w, h = (500, 500)
for file in filelist:
img = Image.open(file)
width, height = img.size
frame_num = 1
for col_i in range(0, width, w):
for row_i in range(0, height, h):
crop = img.crop((col_i, row_i, col_i + w, row_i + h))
name = os.path.basename(file)
name = os.path.splitext(name)[0]
save_to= os.path.join(savedir, name+"_{:03}.png")
crop.save(save_to.format(frame_num))
frame_num += 1
This is mostly based on DataScienceGuy answer here
Here is a late answer that works with Python 3
from PIL import Image
import os
def imgcrop(input, xPieces, yPieces):
filename, file_extension = os.path.splitext(input)
im = Image.open(input)
imgwidth, imgheight = im.size
height = imgheight // yPieces
width = imgwidth // xPieces
for i in range(0, yPieces):
for j in range(0, xPieces):
box = (j * width, i * height, (j + 1) * width, (i + 1) * height)
a = im.crop(box)
try:
a.save("images/" + filename + "-" + str(i) + "-" + str(j) + file_extension)
except:
pass
Usage:
imgcrop("images/testing.jpg", 5, 5)
Then the images will be cropped into pieces according to the specified X and Y pieces, in my case 5 x 5 = 25 pieces
Here is another solution, just using NumPy built-in np.array_split :
def divide_img_blocks(img, n_blocks=(5, 5)):
horizontal = np.array_split(img, n_blocks[0])
splitted_img = [np.array_split(block, n_blocks[1], axis=1) for block in horizontal]
return np.asarray(splitted_img, dtype=np.ndarray).reshape(n_blocks)
It returns a NumPy array with the dimension passed as n_blocks.
Each element of the array is a block, so to access each block and save it as an image you should write something like the following:
result = divide_img_blocks(my_image)
for i in range(result.shape[0]):
for j in range(result.shape[1]):
cv2.imwrite(f"my_block_{i}_{j}.jpg", result[i,j])
This answer is very fast, faster than #Nir answer, which among the posted ones was the cleanest. Additionally is almost three orders of magnitude faster than the suggested package (i.e. image_slicer).
Time taken by divide_img_blocks: 0.0009832382202148438
Time taken by Nir answer: 0.002960681915283203
Time taken by image_slicer.slice: 0.4419238567352295
Hope it can still be useful.
I find it easier to skimage.util.view_as_windows or `skimage.util.view_as_blocks which also allows you to configure the step
http://scikit-image.org/docs/dev/api/skimage.util.html?highlight=view_as_windows#skimage.util.view_as_windows
import os
import sys
from PIL import Image
savedir = r"E:\new_mission _data\test"
filename = r"E:\new_mission _data\test\testing1.png"
img = Image.open(filename)
width, height = img.size
start_pos = start_x, start_y = (0, 0)
cropped_image_size = w, h = (1024,1024)
frame_num = 1
for col_i in range(0, width, w):
for row_i in range(0, height, h):
crop = img.crop((col_i, row_i, col_i + w, row_i + h))
save_to= os.path.join(savedir, "testing_{:02}.png")
crop.save(save_to.format(frame_num))
frame_num += 1
For anyone looking for a simple approach to this, here is a simple working function for splitting an image into NxN sections.
def slice_image(filename, N):
i = Image.open(filename)
width = i.width
height = i.height
for x in range(N):
for y in range(N):
index = (x * pieces) + 1 + y
img = i.crop((x * width/N, y * height/N,
x * width/N+ width/N, y * height/N+ height/N))
img.save(f"{filename}_sliced_{index}.jpeg")
Thanks #Ivan for teaching me something about itertools and grids. Came here to split up tomographic 3D image data (tif-files) into smaller regions for evaluation. I adapted the script to 3D-TIF files (using the tiffile library) and added a "centered" approach. So the tiles don't start in the upper-left corner but are centered and crop too small tiles at the borders at each direction. Maybe this also help other people.
from itertools import product
import tifffile as tif
import numpy as np
path = 'PATH'
filename= 'FILENAME.tif'
img = tif.imread(path+filename)
depth, height, width = img.shape
tilesize = 100
grid = product(range(int((depth%tilesize)/2), int(depth-(depth%tilesize)/2), tilesize),
range(int((width%tilesize)/2), int(width-((width%tilesize)/2)), tilesize),
range(int((height%tilesize)/2), int(height-(height%tilesize)/2), tilesize))
for z,y,x in grid:
crop = img[z:z+tilesize, y:y+tilesize, x:x+tilesize]
tif.imwrite(path+filename+f'{z:04d}z_{y:04d}y_{x:04d}x.tif', crop, dtype = np.uint8)
This is my script tools, it is very sample to splite css-sprit image into icons:
Usage: split_icons.py img dst_path width height
Example: python split_icons.py icon-48.png gtliu 48 48
Save code into split_icons.py :
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import sys
import glob
from PIL import Image
def Usage():
print '%s img dst_path width height' % (sys.argv[0])
sys.exit(1)
if len(sys.argv) != 5:
Usage()
src_img = sys.argv[1]
dst_path = sys.argv[2]
if not os.path.exists(sys.argv[2]) or not os.path.isfile(sys.argv[1]):
print 'Not exists', sys.argv[2], sys.argv[1]
sys.exit(1)
w, h = int(sys.argv[3]), int(sys.argv[4])
im = Image.open(src_img)
im_w, im_h = im.size
print 'Image width:%d height:%d will split into (%d %d) ' % (im_w, im_h, w, h)
w_num, h_num = int(im_w/w), int(im_h/h)
for wi in range(0, w_num):
for hi in range(0, h_num):
box = (wi*w, hi*h, (wi+1)*w, (hi+1)*h)
piece = im.crop(box)
tmp_img = Image.new('L', (w, h), 255)
tmp_img.paste(piece)
img_path = os.path.join(dst_path, "%d_%d.png" % (wi, hi))
tmp_img.save(img_path)
I tried the solutions above, but sometimes you just gotta do it yourself.
Might be off by a pixel in some cases but works fine in general.
import matplotlib.pyplot as plt
import numpy as np
def image_to_tiles(im, number_of_tiles = 4, plot=False):
"""
Function that splits SINGLE channel images into tiles
:param im: image: single channel image (NxN matrix)
:param number_of_tiles: squared number
:param plot:
:return tiles:
"""
n_slices = np.sqrt(number_of_tiles)
assert int(n_slices + 0.5) ** 2 == number_of_tiles, "Number of tiles is not a perfect square"
n_slices = n_slices.astype(np.int)
[w, h] = cropped_npy.shape
r = np.linspace(0, w, n_slices+1)
r_tuples = [(np.int(r[i]), np.int(r[i+1])) for i in range(0, len(r)-1)]
q = np.linspace(0, h, n_slices+1)
q_tuples = [(np.int(q[i]), np.int(q[i+1])) for i in range(0, len(q)-1)]
tiles = []
for row in range(n_slices):
for column in range(n_slices):
[x1, y1, x2, y2] = *r_tuples[row], *q_tuples[column]
tiles.append(im[x1:y1, x2:y2])
if plot:
fig, axes = plt.subplots(n_slices, n_slices, figsize=(10,10))
c = 0
for row in range(n_slices):
for column in range(n_slices):
axes[row,column].imshow(tiles[c])
axes[row,column].axis('off')
c+=1
return tiles
Hope it helps.
I would suggest to use multiprocessing instead of a regular for loop as follows:
from PIL import Image
import os
def crop(infile,height,width):
im = Image.open(infile)
imgwidth, imgheight = im.size
for i in range(imgheight//height):
for j in range(imgwidth//width):
box = (j*width, i*height, (j+1)*width, (i+1)*height)
yield im.crop(box)
def til_image(infile):
infile=...
height=...
width=...
start_num=...
for k,piece in enumerate(crop(infile,height,width),start_num):
img=Image.new('RGB', (height,width), 255)
img.paste(piece)
path=os.path.join('/tmp',"IMG-%s.png" % k)
img.save(path)
from multiprocessing import Pool, cpu_count
try:
pool = Pool(cpu_count())
pool.imap_unordered(tile_image, os.listdir(root), chunksize=4)
finally:
pool.close()
the easiest way:
import image_slicer
image_slicer.slice('/Address of image for exp/A1.png',16)
this command splits the image into 16 slices and saves them in the directory that the input image is there.
you should first install image_slicer:
pip install image_slicer
Splitting an image into squares of a specific size
I adapted a solution so that it accepts a specific tile size instead of an amount of tiles because I needed to cut the image up into a grid of 32px squares.
The parameters are the image_path and the size of the tiles in pixels.
I tried to make the code as readable as possible.
# Imports
from PIL import Image
import os
import random
# Function
def image_to_tiles(im, tile_size = 32):
"""
Function that splits an image into tiles
:param im: image: image path
:param tile_size: width in pixels of a tile
:return tiles:
"""
image = Image.open(im)
w = image.width
h = image.height
row_count = np.int64((h-h%tile_size)/tile_size)
col_count = np.int64((w-w%tile_size)/tile_size)
n_slices = np.int64(row_count*col_count)
# Image info
print(f'Image: {im}')
print(f'Dimensions: w:{w} h:{h}')
print(f'Tile count: {n_slices}')
r = np.linspace(0, w, row_count+1)
r_tuples = [(np.int64(r[i]), np.int64(r[i])+tile_size) for i in range(0, len(r)-1)]
q = np.linspace(0, h, col_count+1)
q_tuples = [(np.int64(q[i]), np.int64(q[i])+tile_size) for i in range(0, len(q)-1)]
#print(f'r_tuples:{r_tuples}\n\nq_tuples:{q_tuples}\n')
tiles = []
for row in range(row_count):
for column in range(col_count):
[y1, y2, x1, x2] = *r_tuples[row], *q_tuples[column]
x2 = x1+tile_size
y2 = y1+tile_size
tile_image = image.crop((x1,y1,x2,y2))
tile_coords = {'x1':x1,'y1':y1,'x2':x2,'y2':y2}
tiles.append({'image':tile_image,'coords':tile_coords})
return tiles
# Testing:
img_path ='/home/user/path/to/image.jpg'
tiles = image_to_tiles(img_path)
for i in range(20):
tile = random.choice(tiles)
tile['image'].show()
you can use numpy stride tricks to achive this, but be careful, as this function has to be used with extreme care (doc)
import numpy as np
from numpy.lib.stride_tricks import as_strided
def img_pieces(img, piece_size):
height, width, chanels = img.shape
n_bytes = img.strides[-1]
return np.reshape(
as_strided(
img,
(
height // piece_size,
width // piece_size,
piece_size,
piece_size,
chanels
),
(
n_bytes * chanels * width * piece_size,
n_bytes * chanels * piece_size,
n_bytes * chanels * width,
n_bytes * chanels,
n_bytes
)
),
(
-1,
piece_size,
piece_size,
chanels
)
)
Here's my attempt on a grayscale image with only numpy based on the solution from here, with some minor tweaks (adding channels) it might suit your needs:
import numpy as np
# Seperate grayscale images to w * h tiles, add padding with zeros if image not scaled
def to_tiles(arr: np.ndarray, tilesize: tuple[int, int]) -> np.ndarray:
def f(x: tuple[int, int]) -> tuple[int, int]:
tmp = list(x)
if tmp[1] > 0:
tmp[0] = tmp[0] + 1
return tuple(tmp)
# # Stride Implementation
# bytelength = np.int8(np.divide(arr.nbytes, arr.size))
assert arr.ndim == 2, "array must be 2d (grayscale) image"
a_h, a_w = arr.shape
h, w = tilesize
assert a_h > h, "tile height is larger than arr height"
assert a_w > w, "tile width is larger than arr width"
row, row_r = f(np.divmod(a_h, h))
col, col_r = f(np.divmod(a_w, w))
arr = np.pad(
arr,
[
(
np.int8(np.ceil(np.divide(h-row_r, 2))) if row_r != 0 else 0,
np.int8(np.floor(np.divide(h-row_r, 2))) if row_r != 0 else 0,
),
(
np.int8(np.ceil(np.divide(w-col_r, 2))) if col_r != 0 else 0,
np.int8(np.floor(np.divide(w-col_r, 2))) if col_r != 0 else 0,
),
],
"constant",
constant_values=(0),
)
# # Stride Implementation
# arr = np.lib.stride_tricks.as_strided(
# arr, shape=(row, col, h, w), strides=(h*a_w*bytelength, w*bytelength, a_w*bytelength, bytelength)
# )
arr = arr.reshape(row, h, col, w).swapaxes(1, 2)
arr = arr.reshape(-1, h, w)
return arr
Here's an example of the result. Image from FUNSD dataset.
def split(img,nbxsplit,nbysplit):
xdemi=int(img.shape[0]/nbxsplit)
ydemi=int(img.shape[1]/nbxsplit)
arr=[]
for i in range(0,img.shape[0]-xdemi+1,xdemi):
for j in range(0,img.shape[1]-ydemi+1,ydemi):
arr.append(img[i:i+xdemi][j:j+ydemi])
a=np.reshape(a,(img.shape[0]-xdemi,img.shape[1]-xdemi))
return a
Not sure if it's still relevant, but my attempt is following:
(I am assuming the image is a numpy array. I am not using Pil or anything, since i didn't want to have any dependencies other than numpy.)
def cut_image_grid(image:np.ndarray, grid_size:int=4):
height, width = image.shape[0], image.shape[1]
piece_height, piece_width = height//grid_size, width//grid_size
pieces = []
for i in range(grid_size):
for j in range(grid_size):
y = i * piece_height
x = j * piece_width
h = (i+1) * piece_height if i < grid_size else None
w = (j+1) * piece_width if j < grid_size else None
piece = image[y:h, x:w]
pieces.append(piece)
return np.array(pieces)
As input the function is receiving a numpy image and an integer (which you could also turn into tuples, but i wanted to have evenly spaced grid cells always with same amount of cells row and column wise).
At first, the code calculates the width and height of the cells based on the given grid_size. After that the code iterates over all rows and columns and generates x, y Coordinates, as well as x0 and y0 (y+height, x+width) for defining the cells.
Every cell is saved as a list into pieces, which is then transformed into a numpy array and returned.
import cv2
def crop_image(image_path, output_path):
im = cv2.imread(os.listdir()[2])
imgheight=im.shape[0]
imgwidth=im.shape[1]
y1 = 0
M = 2000
N = 2000
for y in range(0,imgheight,M):
for x in range(0, imgwidth, N):
y1 = y + M
x1 = x + N
tiles = im[y:y+M,x:x+N]
if tiles.shape[0] < 100 or tiles.shape[1]<100:
continue
cv2.rectangle(im, (x, y), (x1, y1), (0, 255, 0))
cv2.imwrite(output_path + str(x) + '_' + str(y)+"{}.png".format(image_path),tiles)
crop_image(os.listdir()[2], './cutted/')

Categories