0

I am working on a project and my first task is converting some tiff files to jpeg. I am working with a script i previously used and it worked so i modified the path to fix my current project. i want to convert these files and then save them in the same location. my script will run and i added print to make sure the program can access the files correctly but if i add print i the loop after im.save() i just get NONE so something isn't correct here but i cant figure out what im doing incorrectly.

#!/usr/bin/env python3
import os
import sys
from PIL import Image

cwd = os.getcwd()
path = cwd + '/supplier-data/images/'


for files in os.listdir(path):
        print(files)
        if not files.startswith('.') and not files.startswith('README')and not files.startswith('LICENSE'):
            with Image.open(path + files) as im:
                im=im.convert('RGB').resize((600, 400))
                im=im.save(path + files, 'jpeg', quality=100)
                print(im)

  • 2
    `None` is a completely normal return value for operations that don't _need_ to return anything. Unless `save()` is documented to return something (and I don't see why it would be), that print returning None is presumably behaving as expected. – Charles Duffy Jun 17 '21 at 22:49
  • 1
    Silly question: Have you tried modifying the filename to have a new extension before you tell PIL to save to that file? – Charles Duffy Jun 17 '21 at 22:52
  • 1
    Why are you reassigning `im` after each operation (i.e. `im = im.foo(...)`)? Doing that destroys the reference to your `Image` object. – Selcuk Jun 17 '21 at 22:54
  • charles - i thought printing im would verify the new file was converted and give me a confirmation when i run the program. The script im using was from a previous exercise we did in converting from tiff to jpeg since we need to change the size and format – david taxer Jun 17 '21 at 22:56
  • Selcuk, I'm not sure i understand what the issue is. i wanted to convert then i want to save. I'm not ruining anything that im aware of – david taxer Jun 17 '21 at 23:01
  • What Selcuk is telling you (correctly) is that you're overwriting your reference to the `im` object. That's not good practice. Typically when you get a new thing, you should give it a new and separate name. Not saying it's specifically causing a particular bug, just that it's a code smell -- a practice that gives the impression of making software's behavior more likely to be buggy or hard to maintain. – Charles Duffy Jun 17 '21 at 23:11
  • The `.save` method returns `None` according to the [docs](https://www.google.com/amp/s/www.geeksforgeeks.org/python-pil-image-save-method/amp/) so what are you expecting with your 2nd call to `print`? –  Jun 18 '21 at 02:11

1 Answers1

0

so my script was actually working but it converted the files and replaced them with the same name file ending in .tiff even though they converted to jpeg. i have corrected the script with suggestions from others on here and now is does the conversions and saves as the same name but i added + ".jpeg" in the .save() so instead of replacing the old file it created a 2nd file image and saves in the same directory as instructed in the first part on the final project. i am no longer overwriting im and using im2 and im3 in the script. I also used bash to check the original and new image file.

here is an example of how i did that in bash using the original image that i assumed was a tiff file:

file -s 001

this is what is displayed:

001: TIFF image data, little-endian, direntries=11, height=192, bps=8, compression=none, PhotometricIntepretation=BlackIsZero, width=192

and here is the updated version of the script:

#!/usr/bin/env python3
import os
import sys
from PIL import Image

cwd = os.getcwd()
path = cwd + '/supplier-data/images/'

for files in os.listdir(path):
        print(files)
        if not files.startswith('.') and not files.startswith('README')and not files.startswith('LICENSE'):
            with Image.open(path + files) as im:
                im2=im.convert('RGB').resize((600, 400))
                im3=im2.save(path + files + ".jpg", 'jpeg', quality=100)