Skip to content

Instantly share code, notes, and snippets.

@p0we7
Last active July 12, 2020 08:12
Show Gist options
  • Select an option

  • Save p0we7/b1b1a02ffc9d8c2a6bd1cd17a9244b8f to your computer and use it in GitHub Desktop.

Select an option

Save p0we7/b1b1a02ffc9d8c2a6bd1cd17a9244b8f to your computer and use it in GitHub Desktop.
每隔一个4x4像素方格隔开一个像素
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()
@p0we7
Copy link
Copy Markdown
Author

p0we7 commented Jul 12, 2020

example

脚本用途如上。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment