Programmatically change image resolution - python

I have calculated that if I want my generated image to be A4 size # 600dpi for print purpose, it needs to be 7016x4961px # 72dpi. So, I generate it programmatically, then test it in Photoshop and it seems to be fine so if I resize it, it gets proper size and resolution
.
What I wonder about is if it's possible to make this resizing programmatically, preferably with PIL, but not necessarily with it. I need to make it higher DPI.

If you have generated your image 7016 x 4961 px, it is already A4 at 600 dpi. So you don't need to resize it, you just have to set resolution information in file.
You can do it with PIL:
from PIL import Image
im = Image.open("test.png")
im.save("test-600.png", dpi=(600,600))

This code will resize a PNG image into 7016x4961 with PIL:
size = 7016, 4961
im = Image.open("my_image.png")
im_resized = im.resize(size, Image.ANTIALIAS)
im_resized.save("my_image_resized.png", "PNG")
Perhaps a better approach would be to make your canvas x times bigger prior to printing, where x is a factor you have to figure out (7016x4961 in size for this particular image).

Here's how you can resize by batch (per folder) and skip other file types and Mac system files like .DS_Store
from PIL import Image
import os
Image.MAX_IMAGE_PIXELS = None
path = "./*your-source-folder*"
resize_ratio = 2 # where 0.5 is half size, 2 is double size
def resize_aspect_fit():
dirs = os.listdir(path)
for item in dirs:
print(item)
if item == '.DS_Store':
continue
if item == 'Icon\r':
continue
if item.endswith(".mp4"):
continue
if item.endswith(".txt"):
continue
if item.endswith(".db"):
continue
if os.path.isfile(path+item):
image = Image.open(path+item)
file_path, extension = os.path.splitext(path+item)
new_image_height = int(image.size[0] / (1/resize_ratio))
new_image_length = int(image.size[1] / (1/resize_ratio))
image = image.resize((new_image_height, new_image_length), Image.ANTIALIAS)
image.save("./*your-output-folder*/" + item)
resize_aspect_fit()

Related

How to Paste a Border Image (Constant) over Multiple Thumbnail Images?

Here is what i'm trying to achieve:
Resize Multiple Product images into 500 x 500 pixels
Paste all of these resized images individually onto a 800 x 800 image and output as individual images
For now, I have managed to complete Step 1, but have no idea how to proceed to step 2. Here is my code for Step 1:
from PIL import Image
import os, sys
path = "C:\\Users\\User\\Desktop\\Test\\"
dirs = os.listdir( path )
final_size = 500;
def resize_aspect_fit():
for item in dirs:
if item == '.png':
continue
if os.path.isfile(path+item):
im = Image.open(path+item)
f, e = os.path.splitext(path+item)
size = im.size
ratio = float(final_size) / max(size)
new_image_size = tuple([int(x*ratio) for x in size])
im = im.resize(new_image_size, Image.ANTIALIAS)
new_im = Image.new("RGBA", (final_size, final_size), (255,255,255,000))
new_im.paste(im, ((final_size-new_image_size[0])//2, (final_size-new_image_size[1])//2))
new_im.save(f + 'resized.png', 'PNG', quality=100)
resize_aspect_fit()
Thanks!
Edit:
Here's an image illustration for better explanation of what i am trying to achieve. I have 2 smiley faces (500 x 500) which i need to paste over the default 800 x 800 image multiple times (centered) to produce 2 separate images of 800 x 800.
Example
You're nearly there, you can use .paste() with an offset to paste into the middle over your image. a little bit like:
Border_im = Image.open(pathToBorder)
product_im = Image.open(pathToProduct)
x_offset=150;
y_offset=150;
Border_im.paste(product_im, (x_offset,y_offset))
Then save out border_im to a new file

Is there a faster alternative to PIL's Image.paste for multiple images?

I am trying to use image.paste to paste many images onto one background.
My images contain their x,y offset values in their filenames (eg. Image_1000_2000.png is offset 1000,2000).
The code below works, but it's painfully slow. Here's what I've got:
import re
import glob
from PIL import Image
# Disable Decompression Bomb Protection
Image.MAX_IMAGE_PIXELS = None
# Set the dimensions of the blank canvas
newheight = 13000
newwidth = 13000
# Glob all the PNG images in the folder and create the blank canvas
photos = glob.glob("*.png")
blankbackground = Image.new('RGB', (newheight, newwidth), (0, 0, 0))
blankbackground.save(r'..\bg999.png', "PNG")
for photo in photos:
blankbackground = Image.open(r'..\bg999.png')
photog = Image.open(photo)
# Get the x , y offsets from the filenames using re
y_value = re.findall(r"0_(\d*).", photo)[0]
y = int(y_value)
x_value = re.findall(r'_(\d*).', photo)[0]
x = int(x_value)
# The actual paste operation, and save the image to be re-opened later
blankbackground.paste(photog,(x,y))
blankbackground.save(r"..\bg999.png")
print(photo)
Any suggestions on a speedier alternative?
EDIT: As per the comments below, it is not necessary to save / reload the image with every photo. This makes it considerably faster.
As pointed out by Siyuan and Dan, Image.save does not require you to save the image and re-load it every loop.
Move the Image.open to before the loop, and move the Image.save to after the loop, as shown:
import re
import glob
from PIL import Image
# Disable Decompression Bomb Protection
Image.MAX_IMAGE_PIXELS = None
# Set the dimensions of the blank canvas
newheight = 13000
newwidth = 13000
# Glob all the PNG images in the folder and create the blank canvas
photos = glob.glob("*.png")
blankbackground = Image.new('RGB', (newheight, newwidth), (0, 0, 0))
blankbackground.save(r'..\bg999.png', "PNG")
# MOVE THE IMAGE.OPEN BEFORE THE LOOP
blankbackground = Image.open(r'..\bg999.png')
for photo in photos:
photog = Image.open(photo)
# Get the x , y offsets from the filenames using re
y_value = re.findall(r"0_(\d*).", photo)[0]
y = int(y_value)
x_value = re.findall(r'_(\d*).', photo)[0]
x = int(x_value)
# The actual paste operation
blankbackground.paste(photog,(x,y))
print(photo)
# MOVE THE IMAGE.SAVE AFTER THE LOOP
blankbackground.save(r"..\bg999.png")
Thus, it goes from ten minutes to ten seconds.

Python not resizing height of an image

I want to resize some images and here is my code.
import os
from PIL import Image
size = 300, 300
for f in os.listdir('.'):
if f.endswith('.png'):
i = Image.open(f)
fn, fext = os.path.splitext(f)
i.thumbnail(size, Image.ANTIALIAS)
i.save('output/{}{}'.format(fn, fext))
The code is working fine and it resizes all my image to a width of 300px, but the height did not resize.
Can anyone tell me why?
Image.thumbnail() is designed to keep the aspect ratio of the original image. If you want the output image to be exactly 300x300 px, use Image.resize() instead.

How do you merge images into a canvas using PIL/Pillow?

I'm not familiar with PIL, but I know it's very easy to put a bunch of images into a grid in ImageMagick.
How do I, for example, put 16 images into a 4×4 grid where I can specify the gap between rows and columns?
This is easy to do in PIL too. Create an empty image and just paste in the images you want at whatever positions you need using paste. Here's a quick example:
import Image
#opens an image:
im = Image.open("1_tree.jpg")
#creates a new empty image, RGB mode, and size 400 by 400.
new_im = Image.new('RGB', (400,400))
#Here I resize my opened image, so it is no bigger than 100,100
im.thumbnail((100,100))
#Iterate through a 4 by 4 grid with 100 spacing, to place my image
for i in xrange(0,500,100):
for j in xrange(0,500,100):
#I change brightness of the images, just to emphasise they are unique copies.
im=Image.eval(im,lambda x: x+(i+j)/30)
#paste the image at location i,j:
new_im.paste(im, (i,j))
new_im.show()
Expanding on the great answer by fraxel, I wrote a program which takes in a folder of (.png) images, a number of pixels for the width of the collage, and the number of pictures per row, and does all the calculations for you.
#Evan Russenberger-Rosica
#Create a Grid/Matrix of Images
import PIL, os, glob
from PIL import Image
from math import ceil, floor
PATH = r"C:\Users\path\to\images"
frame_width = 1920
images_per_row = 5
padding = 2
os.chdir(PATH)
images = glob.glob("*.png")
images = images[:30] #get the first 30 images
img_width, img_height = Image.open(images[0]).size
sf = (frame_width-(images_per_row-1)*padding)/(images_per_row*img_width) #scaling factor
scaled_img_width = ceil(img_width*sf) #s
scaled_img_height = ceil(img_height*sf)
number_of_rows = ceil(len(images)/images_per_row)
frame_height = ceil(sf*img_height*number_of_rows)
new_im = Image.new('RGB', (frame_width, frame_height))
i,j=0,0
for num, im in enumerate(images):
if num%images_per_row==0:
i=0
im = Image.open(im)
#Here I resize my opened image, so it is no bigger than 100,100
im.thumbnail((scaled_img_width,scaled_img_height))
#Iterate through a 4 by 4 grid with 100 spacing, to place my image
y_cord = (j//images_per_row)*scaled_img_height
new_im.paste(im, (i,y_cord))
print(i, y_cord)
i=(i+scaled_img_width)+padding
j+=1
new_im.show()
new_im.save("out.jpg", "JPEG", quality=80, optimize=True, progressive=True)

How to complete this python function to save in the same folder?

I am trying to write my first real python function that does something real. What i want to accomplish is searching a given folder, and then open all images and merging them together so they make a filmstrip image. Imagine 5 images stacked on top of eachother in one image.
I have this code now, which should be pretty much ok, but propably needs some modification:
import os
import Image
def filmstripOfImages():
imgpath = '/path/here/'
files = glob.glob(imgpath + '*.jpg')
imgwidth = files[0].size[0]
imgheight = files[0].size[1]
totalheight = imgheight * len(files)
filename = 'filmstrip.jpg'
filmstrip_url = imgpath + filename
# Create the new image. The background doesn't have to be white
white = (255,255,255)
filmtripimage = Image.new('RGB',(imgwidth, totalheight),white)
row = 0
for file in files:
img = Image.open(file)
left = 0
right = left + imgwidth
upper = row*imgheight
lower = upper + imgheight
box = (left,upper,right,lower)
row += 1
filmstripimage.paste(img, box)
try:
filmstripimage.save(filename, 'jpg', quality=90, optimize=1)
except:
filmstripimage.save(miniature_filename, 'jpg', quality=90)")
How do i modify this so that it saves the new filmstrip.jpg in the same directory as I loaded the images from? And it probably has some things that are missing or wrong, anybody got a clue?
Related question: How to generate a filmstrip image in python from a folder of images?
It is not an answer to your question, but It might be helpful:
#!/usr/bin/env python
from PIL import Image
def makefilmstrip(images, mode='RGB', color='white'):
"""Return a combined (filmstripped, each on top of the other) image of the images.
"""
width = max(img.size[0] for img in images)
height = sum(img.size[1] for img in images)
image = Image.new(mode, (width, height), color)
left, upper = 0, 0
for img in images:
image.paste(img, (left, upper))
upper += img.size[1]
return image
if __name__=='__main__':
# Here's how it could be used:
from glob import glob
from optparse import OptionParser
# process command-line args
parser = OptionParser()
parser.add_option("-o", "--output", dest="file",
help="write combined image to OUTPUT")
options, filepatterns = parser.parse_args()
outfilename = options.file
filenames = []
for files in map(glob, filepatterns):
if files:
filenames += files
# construct image
images = map(Image.open, filenames)
img = makefilmstrip(images)
img.save(outfilename)
Example:
$ python filmstrip.py -o output.jpg *.jpg
I think if you change your try section to this:
filmstripimage.save(filmstrip_url, 'jpg', quality=90, optimize=1)
In the case you are not joking there are several problems with your script e.g. glob.glob() returns list of filenames (string objects, not Image objects) therefore files[0].size[0] will not work.
as J. F. Sebastian mentioned, glob does not return image objects... but also:
As it is right now, the script assumes the images in the folder are all the same size and shape. This is not often a safe assumption to make.
So for both of those reasons, you'll need to open the images before you can determine their size. Once you open it you should set the width, and scale the images to that width so there is no empty space.
Also, you didn't set miniature_filename anywhere in the script.

Categories

Resources