Python PIL如何在所有黑色方块中粘贴图像

问题描述 投票:0回答:2

目标是将透明PNG文件粘贴到1000 x 1000的基本图像上。到目前为止,我的代码将250 x 250的图像粘贴到基本图像上,但随机放置在整个基本图像中。结果看起来像这个enter image description here

这是一段代码,所以你们可以看到发生了什么。

import random
from PIL import Image, ImageDraw, ImageOps, ImageFont 

###This creates the base image ###
base = Image.new('RGB',(1000,1000),'black')
base.save('base_image.jpg')

### Opens up all the images that will be used###
jon = Image.open('jon_snow.jpg')
rejon = Image.open('resized_jon.jpg')
wolf = Image.open('wolf.png')


### The List of locations around the base image ###
location = [(0,0),(0,250),(0,500),(0,750),(250,0),(250,250),(250,500),(250,750),(500,0),(500,250),(500,500),(500,750),(750,0),(750,250),(750,500),(750,750),(0,0),(0,250),(0,500),(0,750),(250,0),(250,250),(250,500),(250,750),(500,0),(500,250),(500,500),(500,750),(750,0),(750,250),(750,500),(750,750)]
### Opertaions used ###
def image_resize(image,size):
  image.resize((size))
  image.save('resized_jon.jpg')
  return


def image_invert(image):
  inverted = ImageOps.invert(image)
  base.paste(inverted,random.choice(location))
  base.save('base_image.jpg')
  return

def fill_base():
  for x in range(6):
    image_invert(rejon)

我没有添加所有操作只是为了节省时间。因此,当您可以看到使用随机时,它不会在生成所有黑色方块时填满。所以这就是我想创建一个for循环可能或者什么东西来检查那些正方形是黑色的,那么我可以在那个位置粘贴一个PNG文件。有可能检查那些黑色方块吗?任何帮助

python image python-imaging-library
2个回答
2
投票

您可以跟踪random.choice的结果,以便您已经知道哪些方块留空(/黑色),例如使用瓷砖粘贴功能的返回值:

def image_invert(image):
    inverted = ImageOps.invert(image)
    r = random.choice(location)
    base.paste(inverted, r)
    base.save('base_image.jpg')
    return r

(注意:如果函数没有返回值,则不需要return语句)


每个粘贴瓷砖缩小location的Mcve:

import random
from PIL import Image

###This creates the base image ###
base = Image.new('RGB',(1000,1000),'black')
location = [(250*a, 250*b) for a in range(4) for b in range(4)]

jon = Image.new('RGB', (250, 250), 'red')

print('locations used:')
for i in range(10):
    r = random.choice(location)
    location.pop(location.index(r))
    base.paste(jon, r)
    print(r)

base.save('base_image.jpg')
print('\nlocations left black:')
print(location)

结果:

# locations used:
# (500, 250)                                              
# (0, 750)                                                   
# (250, 0)                                                   
# (750, 500)                                                  
# (0, 500)                                                    
# (250, 500)                                                 
# (750, 0)                                                    
# (750, 750)                                                  
# (0, 250)                                                   
# (500, 500)                                

# locations left black:                                      
# [(0, 0), (250, 250), (250, 750), (500, 0), (500, 750), (750, 250)]        

0
投票

如果您真的想要测试图像的图块而不是完全黑色,则可以将图像转换为numpy数组:

import numpy as np
B = np.array(base)
(B[0:250, 0:250]==0).all()
# True or False

您可以将此测试用于所有黑色瓷砖的列表

blk_loc = [l for l in location if (B[l[1]:l[1]+250, l[0]:l[0]+250]==0).all()]

(注意,你必须将x和y的顺序换成numpy的行和列索引。)

© www.soinside.com 2019 - 2024. All rights reserved.