我需要使用PIL将此图像切割成三个部分并选择中间部分。 我该怎么做?
http://thedilbertstore.com/images/periodic_content/dilbert/dt110507dhct.jpg
答案 0 :(得分:28)
假设你有这么长的照片。
现在你想把它分成更小的垂直位,因为它太长了。
这是一个Python脚本,可以做到这一点。这对于我为LaTeX文档准备非常长的图像非常有用。
from __future__ import division
import Image
import math
import os
def long_slice(image_path, out_name, outdir, slice_size):
"""slice an image into parts slice_size tall"""
img = Image.open(image_path)
width, height = img.size
upper = 0
left = 0
slices = int(math.ceil(height/slice_size))
count = 1
for slice in range(slices):
#if we are at the end, set the lower bound to be the bottom of the image
if count == slices:
lower = height
else:
lower = int(count * slice_size)
#set the bounding box! The important bit
bbox = (left, upper, width, lower)
working_slice = img.crop(bbox)
upper += slice_size
#save the slice
working_slice.save(os.path.join(outdir, "slice_" + out_name + "_" + str(count)+".png"))
count +=1
if __name__ == '__main__':
#slice_size is the max height of the slices in pixels
long_slice("longcat.jpg","longcat", os.getcwd(), 300)
这是输出
答案 1 :(得分:9)
我想投票Gourneau's解决方案,但缺乏足够的声誉。但是,我想我会发布由于他的答案而开发的代码,以防它可能对其他人有帮助。我还添加了迭代文件结构的功能,并选择图像宽度。
import Image
import os
# Set the root directory
rootdir = 'path/to/your/file/directory'
def long_slice(image_path, out_name, outdir, sliceHeight, sliceWidth):
img = Image.open(image_path) # Load image
imageWidth, imageHeight = img.size # Get image dimensions
left = 0 # Set the left-most edge
upper = 0 # Set the top-most edge
while (left < imageWidth):
while (upper < imageHeight):
# If the bottom and right of the cropping box overruns the image.
if (upper + sliceHeight > imageHeight and \
left + sliceWidth > imageWidth):
bbox = (left, upper, imageWidth, imageHeight)
# If the right of the cropping box overruns the image
elif (left + sliceWidth > imageWidth):
bbox = (left, upper, imageWidth, upper + sliceHeight)
# If the bottom of the cropping box overruns the image
elif (upper + sliceHeight > imageHeight):
bbox = (left, upper, left + sliceWidth, imageHeight)
# If the entire cropping box is inside the image,
# proceed normally.
else:
bbox = (left, upper, left + sliceWidth, upper + sliceHeight)
working_slice = img.crop(bbox) # Crop image based on created bounds
# Save your new cropped image.
working_slice.save(os.path.join(outdir, 'slice_' + out_name + \
'_' + str(upper) + '_' + str(left) + '.jpg'))
upper += sliceHeight # Increment the horizontal position
left += sliceWidth # Increment the vertical position
upper = 0
if __name__ == '__main__':
# Iterate through all the files in a set of directories.
for subdir, dirs, files in os.walk(rootdir):
for file in files:
long_slice(subdir + '/' + file, 'longcat', subdir, 128, 128)
答案 2 :(得分:7)
对于这个特定的图像你会做
import Image
i = Image.open('dt110507dhct.jpg')
frame2 = i.crop(((275, 0, 528, 250)))
frame2.save('dt110507dhct_frame2.jpg')
答案 3 :(得分:3)
查看PIL的crop()方法
http://effbot.org/imagingbook/image.htm
(需要知道图像的边界框...假设图像每天都有相同的尺寸,你应该能够确定一次边界框并一直使用它。)
答案 4 :(得分:3)
如果之前不知道这些方框,我会在图像(x和y方向)上运行一个简单的边缘搜索过滤器,以找到方框的边界。
一个简单的方法是:
如果你认为盒子的边框总是黑色,你可以先提取一些黑色(或接近黑色)的像素进行一些预处理。但我怀疑它是否必要,因为上述方法应该非常稳定。
答案 5 :(得分:0)