Created
August 26, 2021 02:35
-
-
Save hashABCD/c553cea1adcd95193494e99f40129dad to your computer and use it in GitHub Desktop.
Function which converts photo into pixel art
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
def photo2pixelart(image, i_size, o_size): | |
""" | |
image: path to image file | |
i_size: size of the small image eg:(8,8) | |
o_size: output size eg:(10000,10000) | |
""" | |
#read file | |
img=Image.open(image) | |
#convert to small image | |
small_img=img.resize(i_size,Image.BILINEAR) | |
#resize to output size | |
res=small_img.resize(img.size, Image.NEAREST) | |
#Save output image | |
filename=f'mario_{i_size[0]}x{i_size[1]}.png' | |
res.save(filename) | |
#Display images side by side | |
plt.figure(figsize=(16,10)) | |
#original image | |
plt.subplot(1,2,1) | |
plt.title('Original image', size=18) | |
plt.imshow(img) #display image | |
plt.axis('off') #hide axis | |
#pixel art | |
plt.subplot(1,2,2) | |
plt.title(f'Pixel Art {i_size[0]}x{i_size[1]}', size=18) | |
plt.imshow(res) | |
plt.axis('off') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment