简体   繁体   English

PIL Image 没有按照我想要的方式裁剪

[英]PIL Image not cropping the way I want

I was trying to make a contact sheet of a colored image.我试图制作彩色图像的联系表。 fully desaturated to full color, but the problem is the output is 10 images of the first cropped image.完全去饱和到全色,但问题是 output 是第一张裁剪图像的 10 张图像。

Here is the code:这是代码:

import PIL
from PIL import ImageEnhance
from PIL import Image

image =  Image.open("img.jpg").convert("RGB")

contact_sheet = PIL.Image.new(image.mode,(1920,1080))

enhancer = ImageEnhance.Color(image)

images = []
current_location = 0   
for i in range(10):
    images.append(enhancer.enhance(i/10))

for img in images:
    contact_sheet.paste(img,(current_location,0))
    image.crop((current_location,0,current_location+192,0+1080))
    current_location+=192 

imagesave = contact_sheet.save("CroppedContactSheet.jpg")

这是裁剪后的图像:

You need to assign the result of the crop function to a variable.您需要将crop function 的结果分配给一个变量。 The image is not altered by the crop function, but the cropped version is returned, see also the docs , using this input: crop function 未更改图像,但返回裁剪后的版本,另请参阅文档,使用此输入:

在此处输入图像描述

And this modified code:而这个修改后的代码:

import PIL
from PIL import ImageEnhance
from PIL import Image

image =  Image.open("img.jpg").convert("RGB")

contact_sheet = PIL.Image.new(image.mode,(1920,1080))

enhancer = ImageEnhance.Color(image)

images = []
current_location = 0   
for i in range(10):
    images.append(enhancer.enhance(i/10))

for img in images:
    # Changed here slightly, current_slice is what we want to paste into the new image
    current_slice = img.crop((current_location,0,current_location+192,0+1080))
    contact_sheet.paste(current_slice,(current_location,0))
    current_location+=192

#display(contact_sheet)
imagesave = contact_sheet.save("CroppedContactSheet.jpg")


We get this output:我们得到这个 output:

在此处输入图像描述

Image taken from http://www.desktopwallpaperhd.net/view/raptor-sample-ford-auto-cartoon-205618.html图片取自http://www.desktopwallpaperhd.net/view/raptor-sample-ford-auto-cartoon-205618.html

Addition:添加:

I don't know if it was intended, but from your question it sounds like you wanted to go from enhancer.enhance(0) to enhancer.enhance(1) , but in the code you provided you are going from 0 to 9/10 , so the last slice is not at full color, but at 90%.我不知道它是否有意,但从你的问题来看,你似乎想要 go 从enhancer.enhance(0)enhancer.enhance(1) ,但在你提供的代码中,你将从09/10 ,所以最后一片不是全彩,而是 90%。 If we change the code to enhancer.enhance(i/9) , then the ouptut is:如果我们将代码更改为enhancer.enhance(i/9) ,则输出为:

在此处输入图像描述

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

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