Last active
July 12, 2020 08:12
-
-
Save p0we7/b1b1a02ffc9d8c2a6bd1cd17a9244b8f to your computer and use it in GitHub Desktop.
每隔一个4x4像素方格隔开一个像素
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
import numpy as np | |
# import matplotlib.pyplot as plt | |
img_array = np.array(Image.open('t3.png').convert('RGBA')) | |
w = img_array.shape[0] # 获取图片宽度 | |
h = img_array.shape[1] # 获取图片高度 | |
# 根据原图尺寸放大创建一个放大后的图片 | |
# new_array = np.zeros((round(h/4)+h, round(w/4)+w, 4), dtype=np.uint8) | |
new_array = np.zeros((600, 600, 4), dtype=np.uint8) | |
# round 用于四舍五入,否则如果是奇数的照片尺寸会导致出错. | |
xadd = 0 | |
for x in range(0,img_array.shape[0]): | |
yadd = 0 | |
if x % 4 == 0 and x != 0: | |
xadd += 1 | |
for y in range(0,img_array.shape[1]): | |
if y % 4 == 0 and y != 0: # 遇到整除4的像素点就偏移1px | |
yadd += 1 | |
new_array[x + xadd ,y + yadd] = img_array[x, y] | |
else: | |
new_array[x + xadd ,y + yadd] = img_array[x, y] | |
# plt.figure("4px") | |
# plt.imshow(new_array) | |
# plt.axis('off') | |
# plt.show() | |
new_img = Image.fromarray(new_array, 'RGBA') | |
new_img.save('t3+1.png') | |
new_img.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
脚本用途如上。