I'd like to learn how to code in python, I have an idea for a basic script that could…
- User
I'd like to learn how to code in python, I have an idea for a basic script that could help me. I have a folder of images in png format, I would like a py script, which will be stored in the same folder, will execute and upscale the png files to 10 times it's original resolution, using nearest neighbour for interpolation. The files should be saved overwriting existing pngs.
Assistant
Here is a 50 word Python script to upscale the PNG images in a folder to 10 times their original resolution using nearest neighbor interpolation and overwrite the existing files:
import os from PIL import Image for filename in os.listdir('.'): if filename.endswith('.png'): im = Image.open(filename) im = im.resize((im.width*10, im.height*10), Image.NEAREST) im.save(filename)- User
Please provide code that saves the file as separate, instead of overwriting, we will ad _10X to the file name.
Assistant
Here is the code to save the upscaled images as separate files with _10X added to the filenames:
import os from PIL import Image for filename in os.listdir('.'): if filename.endswith('.png'): name, ext = os.path.splitext(filename) im = Image.open(filename) im = im.resize((im.width*10, im.height*10), Image.NEAREST) im.save(name + '_10X' + ext)Assistant
Here is the code to save the upscaled images as separate files with _10X added to the filenames:
import os from PIL import Image for filename in os.listdir('.'): if filename.endswith('.png'): name, ext = os.path.splitext(filename) im = Image.open(filename) im = im.resize((im.width*10, im.height*10), Image.NEAREST) im.save(name + '_10X' + ext)- User
What if they are jpegs and gifs, revise to consider any image format.
Assistant
Here is the code revised to upscale JPEGs, GIFs, and PNGs, saving separate upscaled images with _10X added to the filenames:
import os from PIL import Image for filename in os.listdir('.'): name, ext = os.path.splitext(filename) if ext in ['.jpg','.png','.gif']: im = Image.open(filename) im = im.resize((im.width*10, im.height*10), Image.NEAREST) im.save(name + '_10X' + ext)