Skip to content

Instantly share code, notes, and snippets.

@amatelin
Last active November 8, 2021 15:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amatelin/f78ab4af5f631e39c01c to your computer and use it in GitHub Desktop.
Save amatelin/f78ab4af5f631e39c01c to your computer and use it in GitHub Desktop.
A simple script to automatically downsize all the images in a folder using Gimp python console (Gimp->Filters/Python-Fu/console).
from os import listdir
from os.path import isfile, join
## Set image folder path
dir_path = "C:/img_dir/"
## Store files names in folder
files = [f for f in listdir(dir_path) if isfile(join(dir_path, f))]
for file in files:
image = pdb.file_jpeg_load(dir_path+file, dir_path+file)
## Don't forget to create the 'small/' folder in your image directory
save_path = dir_path+"small/"+ file
## save image with 85% of original quality
pdb.file_jpeg_save(image, image.active_drawable, save_path, save_path, 0.85, 0, 0, 0, "", 0, 0, 0, 0)
@Allan5525
Copy link

Good Morning! I'm just learning to use Python and I need to open several pdf files, increase them to 500 ppi, convert them to grayscale and save them to jpg. I used part of your code as a reference to open the files and it worked really well. However, even increasing the pixels, I am not getting the same image quality when I do this operation manually. Could you help me?

`from os import listdir
from os.path import isfile, join

Configura o caminho dos arquivos

dir_path = "C:/Users/paul.paa/Documents/"

Grava o nome dos arquivos na lista files

files = [f for f in listdir(dir_path) if isfile(join(dir_path, f))]

for file_in in files:
file_out_cin = str.rstrip(file_in, '.pdf') + ' - CIN.jpg'
img = pdb.file_pdf_load(dir_path + file_in, dir_path + file_in)
pdb.gimp_image_set_resolution(img, 500, 500)
pdb.gimp_convert_grayscale(img)
pdb.file_jpeg_save(img, img.active_drawable, dir_path + "JPEGS/" + file_out_cin, dir_path + "JPEGS/" + file_out_cin , 1, 0, 0, 0, "", 0, 0, 0, 0)`

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