Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from PIL import Image
- from pathlib import Path
- import io
- pathlist = Path(".\\assets\\").glob('**/*.png') # This gets all of the files ending in .png in the pack.
- for file in pathlist: # Loops over the files
- try: # This makes sure the program doesn't exit if there's an error
- print("Processing file:", file)
- toreplace=Image.open(".\\"+file) # Opens the iumage
- w, h = toreplace.size # Puts the width of the image into w and the height into h
- dividea = w/h # Works out the ratio of the height and width (aspect ratio)
- print("Old dimensions:",w,"by",h)
- toreplace.close() # Closes the file
- name=file.split('\\')[::-1][0].split('.')[0] # Splits the file path at every \ (although in Python there need to be two slashes to escape the slash), reverses the list with [::-1] and gets the first element, which is the last part of the path.
- print("Search term:", name.replace("_"," "), "-minecraft") # The Minecraft file names include underscores, so those are repla
- response=requests.get('https://www.google.co.uk/search?q='+name.replace("_","+")+'+-minecraft&tbm=isch')
- response=response.content.decode('utf-8', 'ignore')
- print(response)
- r=requests.get(response[response.find('src="',response.find('<img ')):].split('"')[1])
- image = Image.open(io.BytesIO(r.content))
- w, h = image.size
- divide=w/h
- if dividea>divide:
- newh=int(h/(dividea/divide))
- box=(0, (h-newh)/2, w, h-((h-newh)/2))
- image.crop(box).save(".\\"+file)
- print("Saved with new dimensions:",w,"by",newh)
- elif divide>dividea:
- neww=int(w*(dividea/divide))
- box=((w-neww)/2, 0, w-((w-neww)/2), h)
- image.crop(box).save(".\\"+file)
- print("Saved with new dimensions:",neww,"by",h)
- elif divide == dividea:
- image.save(".\\"+file)
- print("Saved with new dimensions:",w,"by",h,"(not croppped)")
- #open('google.jpeg', 'wb').write(r.content)
- r=0
- print("Done")
- print()
- except:
- pass
- print("Error list:",errors)
Add Comment
Please, Sign In to add comment