用像素点把两张图片重合起来会怎样?

最近在学习pygame模块,正好有个demo是在surface上随机产生各种颜色的像素点,感觉很有趣。于是想到把图片以这种方式显示出来,效果还不错,之后又突发奇想,如果把两张图片合在一起会怎么样?

 

pixel-image

代码如下,非常简单

import pygame
from pygame.locals import *
from sys import exit
from random import randint
from PIL import Image

flag = True
im1 = Image.open('image1')
im2 = Image.open('image2')
wide = im1.size[0] if im1.size[0] > im2.size[0] else im2.size[0]
high = im1.size[1] if im1.size[1] > im2.size[1] else im2.size[1]
im1 = im1.resize((wide, high))
im2 = im2.resize((wide, high))

pygame.init()
screen = pygame.display.set_mode((wide, high), 0, 32)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

        if event.type == MOUSEBUTTONDOWN:
            pygame.draw.circle(screen, (0, 0, 0), pygame.mouse.get_pos(), 20, 20)

        if event.type == KEYDOWN:
            if event.key == K_SPACE:
                pygame.image.save(screen, 'img.png')

    for _ in range(10000):
         rand_pos = (randint(0, wide - 1), randint(0, high - 1))
         screen.set_at(rand_pos, im1.getpixel(rand_pos))

    for _ in range(10000):
         rand_pos = (randint(0, wide - 1), randint(0, high - 1))
         screen.set_at(rand_pos, im2.getpixel(rand_pos))

    pygame.display.update()

注意:

1、两张图片尺寸可以不一致(但最好不要一张横图,一张竖图)

2、如觉得效果不错,可以按空格保存图片(当前文件路径)

3、图片尺寸取两张图片的最大值

4、空格重置,“s”保存当前图片,点击清除一小块区域

5、密集恐惧症请右上角

图片1

 

图片1

 

图片2

 

图片2

 

效果图

 

效果图

 

Github地址

  • 用支付宝打我
  • 用微信打我

Long may the sunshine

发表评论

电子邮件地址不会被公开。 必填项已用*标注

召唤蕾姆