Convert image to character representation

To execute under python3, you first need to install pillow

pip3 install pillow

 

from PIL import Image

image_name='logo.jpg'
img=Image.open(image_name)

print(img.size)
print(img.mode)

#convert to black/white picture
img=img.convert('L')
#img.show()
#img.save('f.jpg')
         
#Reduce the picture 
w,h= img.size

#If the image is too large, scale the height and width 
if w > 100 :
    h=int((100/w)*h)
    w=100

#Prevent the quality from degrading when the image is zoomed, add a filter parameter 
img= img.resize((w,h),Image.ANTIALIAS)
img.save('ff.jpg')

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324587228&siteId=291194637