0
  • I want to create a python script which searches for all .png images in the current direction. Then create a .jpg file for each .png image.
  • The .jpg file should have the same name as the .png file and a width of 800px and a height of 800px. The .png image should be placed in the center of the .jpg image.
  • Create a folder with the same name and move the .png image and the .jpg image into this folder.

The problem of my script is that the transparency of the .png is lost. The result is that there is a big black border around the png images after they are placed in the jpg image. How can i get rid of this strange behavior?

Before: [File "1.png"] [File "2.png"]

After: [Folder "1"] [Folder "2"]

Content of [Folder "1"]: [File "1.png"] [File "1.jpg"]

Thats what ive done so far:

` import os from PIL import Image

    #Search for all .png images in the current directory
    for filename in os.listdir("."):
    if filename.endswith(".png"):
    # Open the .png image
    image = Image.open(filename)
    
    # Get the original size of the image
    width, height = image.size
    
    # Create a .jpg image with a width of 800px and a height of 800px
    jpg_image = Image.new("RGB", (800, 800), (255, 255, 255))
    
    # Calculate the new x and y position to place the .png image in the center of the .jpg image
    x = (800 - width) // 2
    y = (800 - height) // 2
    
    # Place the .png image in the center of the .jpg image
    jpg_image.paste(image, (x, y))
    
    # Save the .jpg image
    jpg_filename = filename.replace(".png", ".jpg")
    jpg_image.save(jpg_filename)
    
    # Create a folder with the same name as the .png image
    folder_name = filename.replace(".png", "")
    if not os.path.exists(folder_name):
        os.makedirs(folder_name)
    
    # Move the .png image and the .jpg image into the folder
    os.rename(filename, folder_name + "/" + filename)
    os.rename(jpg_filename, folder_name + "/" + jpg_filename)`
  • 1
    img = Image.open("image.png") # Resize the image to a specific size img = img.resize((200, 200)) # Save the image as a JPEG img.save("image.jpg", "JPEG") – Rajeev Feb 01 '23 at 14:01
  • You can call ffmpeg to do that. To specify size you need to change scale. – Aenye_Cerbin Feb 01 '23 at 14:12
  • 2
    You can't preserve transparency if you save as JPEG because JPEG doesn't support transparency. JPEG means *"Joint **Photographics** Experts Group"* and is for **photographs** and you don't get transparent areas in photographs - so if you imagine sending a film for processing and printing, you don't get any transparent, *"see-through, cut-out"* areas in your prints they send back. – Mark Setchell Feb 01 '23 at 15:14

1 Answers1

2

The result is that there is a big black border around the png images after they are placed in the jpg image.

This might be because jpg images do not support transparency in images, so when you paste the png image on the jpg image the transparency is lost.

See here.

What you can do is remove the transparency from the png image before pasting it onto the jpg image.

See here.