In these lines of code, I create a small red image and save it as "new_image.png". But I can't find the saved image, where is it saved? And can I change the place where I want to save my image?
from PIL import Image
img = Image.new('RGB', (60, 30), color = 'red')
img.save("new_image.PNG")
I tested your code:
from PIL import Image
img = Image.new('RGB', (60, 30), color = 'red')
img.save("new_image.PNG")
It works well for me:
The reason may because your current work path is not as you thought.
See my answer here:
https://stackoverflow.com/a/66449241/12838403
If you don't specify a path, the image is saved in the same path as the python file generating the image
from PIL import Image
img = Image.new('RGB', (60, 30), color = 'red')
img.save("new_image.PNG")
To save image in a directory you specify, you can use
from PIL import Image
import os
image_path = "path/to/image"
image = image.save(f"{image_path}/image.png")
Note: If this directory does not exist, you would have to create it before saving your image in it.
from PIL import Image
import os
image_path = "path/to/image"
os.mkdir(image_path)
image = image.save(f"{image_path}/image.png")
Related
Hi i am trying to convert the Tiff file into png or jpg file but the ouput that i am getting is noisy and not what i expected. Below is the code that i have tried :
from PIL import Image
im = Image.open('/content/img.tif')
import numpy as np
imarray = np.array(im)
print(imarray)
from matplotlib import pyplot as plt
plt.imshow(imarray, interpolation='nearest')
plt.show() # To see how the tiff file looks like
import cv2
from PIL import Image, ImageOps
img = (np.maximum(imarray, 0) / imarray.max()) * 255.0
print(img)
img = 255 - img #Inverting the pixel
print("********************************************************************")
print(img)
img = Image.fromarray(np.uint8(img))
img.save(f'/content/img.png')
please find the sample tiff file here
https://drive.google.com/file/d/1Gfyo4dCo_4pfYvUn6_a6lD0SfxZOzUwK/view?usp=sharing
Output png/jpg image i was getting is this
Can anyone please help me in converting the tiff into jpg or png
Thanks
The code below worked for me to read .tiff image and save layers as .jpeg:
from PIL import Image, ImageSequence
#open tiff image
im = Image.open("YOUR IMAGE PATH")
#navigate to the folder were the layers are going to be saved
%cd YOUR DIRECTORY
#loop over layers and export jpeg instances
for i, page in enumerate(ImageSequence.Iterator(im)):
page.mode = 'I'
page.point(lambda i:i*(1./256)).convert('L').save(str(i)+'.jpeg')
from PIL import Image, ImageDraw, ImageFont
import glob
import os
images = glob.glob("directory_path/*.jpg")
for img in images:
images = Image.open(img)
draw = ImageDraw.Draw(images)
font = ImageFont.load_default() #Downloaded Font from Google font
text = "Text on all images from directory"
draw.text((0,150),text,(250,250,250),font=font)
images.save(img)
I have to put text on all images , I have tried above code but its not working
This code worked for me just fine, but the text was hard to read because it was small and white. I did change directory_path to images and put my images in there. The images looked like this, the text is small and on the left side:
Here is the solution
from PIL import Image,ImageDraw,ImageFont
import glob
import os
images=glob.glob("path/*.jpg")
for img in images:
images=Image.open(img)
draw=ImageDraw.Draw(images)
font=ImageFont.load_default()
text="Whatever text"
draw.text((0,240),text,(250,250,250),font=font)
images.save(img)
one possible problem with the code may be that you are using the images variable for saving the list of images and also to iterate through the images.
Try this code, this will work for sure.
from PIL import Image, ImageDraw, ImageFont
import glob
import os
images = glob.glob("new_dir/*.jpg")
print(images)
for img in images:
image = Image.open(img)
draw = ImageDraw.Draw(image)
font = ImageFont.load_default() #Downloaded Font from Google font
text = "Text on all images from directory"
draw.text((0,150),text,fill = 'red' ,font=font)
image.save(img)
I'm trying to resize a set of images, approximatively 366, so I created a script that I tested first on 3 and it was successful.
The issue is when I process the whole folder, it returns me this error :
resizeimage.imageexceptions.ImageSizeError: 'Image is too small, Image size : (275, 183), Required size : (399, 399)'
My script is supposed to iterate an entire folder, resize images then store the output files in another folder:
import os
from PIL import Image
from resizeimage import resizeimage
path = "/Users/sigc2sige/PycharmProjects/helloworld/photos"
size = (399, 399)
for file in os.listdir(path):
with open('/Users/sigc2sige/PycharmProjects/helloworld/photos/'+file, 'r+b') as f:
with Image.open(f) as image:
cover = resizeimage.resize_cover(image, size, Image.ANTIALIAS)
cover.save('/Users/sigc2sige/PycharmProjects/helloworld/photos_2/'+file, image.format)
I did use this instruction:
thumb = ImageOps.fit(image, size, Image.ANTIALIAS) but I believe that it crops images instead of resizing them.
If you have any ideas about how to solve this issue, it would be great.
Downsampling an image (making it smaller) is one thing, and upsampling (making it bigger) is another thing. If you want to downsample, ANTIALIAS is a good choice, if you want to upsample, there are other filters you could use.
import os
from PIL import Image
from resizeimage import resizeimage
path = "/Users/sigc2sige/PycharmProjects/helloworld/photos"
size = (399, 399)
for file in os.listdir(path):
with open('/Users/sigc2sige/PycharmProjects/helloworld/photos/'+file, 'r+b') as f:
with Image.open(f) as image:
if (image.size) >= size:
cover = resizeimage.resize_cover(image, size, Image.ANTIALIAS)
cover.save('/Users/sigc2sige/PycharmProjects/helloworld/photos_2/'+file, image.format)
else:
cover = image.resize(size, Image.BICUBIC).save('/Users/sigc2sige/PycharmProjects/helloworld/photos_2/'+file, image.format)
So I've been trying to make a meme bot, in python using PILLOW (a PIL fork) basically it takes a random template and a random source image and puts them together.
So far I managed to make a version which puts the source image adequately in the template, but fails to resize it. Here's my code:
P. S. I mainly code in C, so my python ain't that great
from PIL import Image, ImageOps
import os, random
import re
import linecache
import string
temp_dir = "temp dir"
source_dir = "source dir"
memes_dir = "memes dir"
#Random template & source image
rand_temp = random.choice(os.listdir(temp_dir))
rand_source = random.choice(os.listdir(source_dir))
#template
bot = Image.open(temp_dir + rand_temp)
#source image
top = Image.open(source_dir + rand_source)
width, height = bot.size
size = (width, height)
#Puts Source image in template
meme = ImageOps.fit(top, size, Image.ANTIALIAS)
meme.paste(bot, (0,0), bot)
meme.show()
Is there any way to achieve what I want? Or should I maybe move to another language? Thanks!
Is there the most popular way of adding a text on an image in Python? I found a few completely different approaches, this seems to be best, but it doesn't work:
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
img = Image.open("/full_path/1.jpg")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf", 16)
draw.text((0, 0),"Sample Text",(255,255,255),font=font)
img.save('/full_path/sample-out.jpg')
After its running, the picture still doesn't have a text on it.
try this:
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
img=Image.open("pathToImage")
font = ImageFont.truetype("pathToFont",10)
draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(255,255,0),font=font)
draw = ImageDraw.Draw(img)
draw = ImageDraw.Draw(img)
img.save("a_test.png")