简体   繁体   English

Python PIL:如果照片区域不够,则使用黑色区域进行图像裁剪

[英]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. 它工作正常,但如果我的新拇指区域小于裁剪1,则所有空白区域都填充黑色。 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? 也许有一些选项如max_height用于裁剪方法或其他任何东西?

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? 如果图像的最大值超过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 would do it this way: 我会这样做:

  • If the image is wide, then scale it to be 100px tall. 如果图像很宽,则将其缩放为100px高。 If it's tall, scale it to be 100px wide. 如果它很高,则将其缩放为100px宽。

  • Crop out the middle 100x100. 裁剪出中间的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") 

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! 谢谢你的回复!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM