Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropping and Saving TIFF files in Python

I am new to python coding. I have been trying to crop a series of .tif images generated by X-Rays. Now, it crops successfully and shows the image generated but I cannot save them. Images when saved come out to be completely black. The line of code I am using is as follows:

    from PIL import Image
    import numpy
    startx = 421
    starty = 118
    stopx = 1182
    stopy = 336
    startfile = 1722
    stopfile = 1951
    for i in range(startfile,stopfile+1):
        image = Image.open("Filepath"+str(startfile)+".tif")
        cropped_image = image.crop((startx,starty,stopx,stopy))
        print("ipp"+str(startfile)+" cropped")
        image1 = cropped_image.rotate(180,0,1)
        image1.save("Filepath"+str(startfile),".tif")
        print("ipp"+str(startfile)+" saved")
        startfile = startfile + 1
        starty = starty + 3
        stopy = stopy + 3

Need Help!

like image 968
Ankit Bose Avatar asked Aug 21 '26 12:08

Ankit Bose


1 Answers

I think the error lies here:

image1.save("Filepath"+str(startfile),".tif")

The first argument should be the filename, including the extension. The second argument is optional, and gives a format. The filename you give, does not have an extension. This line should be:

image1.save("Filepath"+str(startfile)+".tif")
like image 143
physicalattraction Avatar answered Aug 23 '26 02:08

physicalattraction



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!