Suppose you have .tif file which has 14 pages (multi image tif). I'd like to draw a rectangle on a specific place on a specific page. However, below example does not paste my img which I drew with ImageDraw anywhere on the second page. Where am I failing this?
from PIL import Image, ImageDraw
im = Image.open('tiffile.tif')
for i, page in enumerate(ImageSequence.Iterator(im)):
if i == 1:
height = width = 800
img = Image.new('RGB', (height, width), (255, 0, 0))
draw = ImageDraw.Draw(img)
page.paste(img, (0,0))
im.save('tiffile_b.tif', save_all=True)
Just like #AKX suggested there's no need to create a new image on to paste it onto the current page. Instead draw the rectangle on that page. However, I had to save all the pages in a list and then create a new .tif file out of the new pages in the list.
from PIL import Image, ImageDraw
im = Image.open('tiffile.tif')
frames=[]
for i, page in enumerate(ImageSequence.Iterator(im)):
page = page.copy()
if i == 1:
box = (0, 590, 2272, 2570)
draw = ImageDraw.Draw(page)
draw.rectangle(box, outline=(0), fill=(250))
frames.append(page)
frames[0].save('testfile_b.tif', save_all=True, append_images=frames[1:])
Related
Im trying to using OpenSansEmoji and PIL to render text with emojis to a PNG.
font = ImageFont.truetype("OpenSansEmoji.ttf",size=20,encoding='unic')
img = Image.new("RGBA", (400,100), (54, 57, 63))
draw = ImageDraw.Draw(img)
draw.text((0,0), responceText, (255,255,255), font=font)
img = img.convert('RGB')
img.save("images/image_text.png")
But I get a textbox with a square instead, on some common emojis like the thumbs up, it renders it but line style. Is there any way I can get full color emojis?
I want to put my pic in my frame.I used this cod :
from PIL import Image
img = Image.open('Pic.jpg')
frame = Image.open('Frame.jpg')
size1 = 354,362
paste_point = 69,339
Pic = img.resize((size1))
frame.paste(img, (paste_point))
frame.show()
When i run the program, my pic doesn't put in my frame.
How can i create a frame in python
your question solution depends always on your pic size and frame size ,so code must be adjusted according to pixel size of your pic
i am giving generic code for your problem maynot fit to your image standards
from PIL import Image
img = Image.open('Pic.jpg')
img_w, img_h = img.size
frame = Image.new('RGBA', (1440, 900), (255, 255, 255, 255))
bg_w, bg_h = frame.size
offset = ((bg_w - img_w) / 2, (bg_h - img_h) / 2)
frame.paste(img, offset)
frame.save('out.png')
I'm drawing some text on a black strip, and then pasting the result on top of a base image, using PIL. One criticality is having the text position perfectly in the center of the black strip.
I cater for that via the following code:
from PIL import Image, ImageFont, ImageDraw
background = Image.new('RGB', (strip_width, strip_height)) #creating the black strip
draw = ImageDraw.Draw(background)
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 16)
text_width, text_height = draw.textsize("Foooo Barrrr!")
position = ((strip_width-text_width)/2,(strip_height-text_height)/2)
draw.text(position,"Foooo Barrrr!",(255,255,255),font=font)
offset = (0,base_image_height/2)
base_image.paste(background,offset)
Notice how I'm setting position.
Now with all said and done, the result looks like so:
The text isn't precisely middled. It's slightly to the right and down. How do I improve my algorithm?
Remember to pass your font to draw.textsize as a second parameter (and also make sure you are really using the same text and font arguments to draw.textsize and draw.text).
Here's what worked for me:
from PIL import Image, ImageFont, ImageDraw
def center_text(img, font, text, color=(255, 255, 255)):
draw = ImageDraw.Draw(img)
text_width, text_height = draw.textsize(text, font)
position = ((strip_width-text_width)/2,(strip_height-text_height)/2)
draw.text(position, text, color, font=font)
return img
Usage:
strip_width, strip_height = 300, 50
text = "Foooo Barrrr!!"
background = Image.new('RGB', (strip_width, strip_height)) #creating the black strip
font = ImageFont.truetype("times", 24)
center_text(background, font, "Foooo Barrrr!")
Result:
I'm trying to change the pixels on my picture to darker green than I have already made it, I am trying to add + rgb(0,50,0) to it but I can't seem to be able to do so, could you help? I have put my code in below, and freljord2.png is now just a full green image using getcolor(green, "RGBA")
im = Image.open('freljord2.png')
#one_pixel = im.getpixel((0, 0))
#one_pixel[1] = 0;
#im.save('freljord3.png')
(0, 0, 0, 0)
for x in range(0):
for y in range(0):
im.putpixel((x, y), (210, 210, 210))
for x in range(560):
for y in range(557):
print("hi")
hello = ImageColor.get(00B200)
im.putpixel((x, y), )
im.getpixel((0, 0))
(210, 210, 210, 255)
im.getpixel((0, 50))
(169, 169, 169, 255)
im.save('freljord2.png')
You mention that you want to make your image more dark green, but if you add 50 to each pixel value of an image, you only make it brighter (and greener). What you want to do is to place a green transparent overlay over your existing image. You should create a new image for that with the same size as your original with the color you want to add, and an alpha value, which indicates how transparent it is. Next, you need to paste that image over your original image using a mask.
The following code example should do the trick. The result is shown below. You can play around a little bit with the values to tailor to your needs.
'''
Created on Oct 23, 2016
#author: physicalattraction
'''
import os.path
from PIL import Image
def get_img_dir() -> str:
'''
Return the full path to the image directory
:return: string
'''
pkg_dir = os.path.dirname(__file__)
img_dir = os.path.join(pkg_dir, '..', '..', 'img')
return img_dir
def open_img(img_name: str) -> Image:
'''
Open the given file form the image directory
:param img_name: Name including extension of the image
:return: Image object
'''
img_dir = get_img_dir()
full_img_path = os.path.join(img_dir, img_name)
return Image.open(full_img_path)
def save_img(img: Image, img_name: str):
'''
Save the given image to the image directory
:param img: Image object
:param img_name: Name including the extension of the image
'''
img_dir = get_img_dir()
full_img_path = os.path.join(img_dir, img_name)
img.save(full_img_path)
def overlay(img: Image, overlay_color: tuple):
'''
Place an overlay over an existing image
:param img: Image opened with PIL.Image
:param overlay_color: four-tuple with color to add to your image
'''
assert len(overlay_color) == 4, 'Overlay color shall be a 4-tuple'
img_overlay = Image.new(size=img.size, color=overlay_color, mode='RGBA')
img.paste(img_overlay, None, mask=img_overlay)
color_string = '_'.join([str(c) for c in overlay_color])
filename = 'amsterdam_{color}.jpg'.format(color=color_string)
save_img(img, filename)
if __name__ == '__main__':
ams = open_img('amsterdam.jpg')
green = (0, 50, 0, 128)
overlay(ams, green)
Original image:
Darker green image:
I have 2 images:
PNG (99x97) with white, rotated frame and rest is in full transparency.
JPG - is my generated thumbnail (80x80)
Now I want to put thumbnail into my frame so it looks like some kind of painting. What should I do?
EDIT:
I forgot to add, that picture must be under the frame.
Frame image
I have some script but it shows only a frame. There is no picture in it :/
import Image, ImageDraw
img_size = (99,97)
im = Image.open('logo.jpg')
picture = im.crop((0,0,80,80))
frame = Image.open('thumb-frame.png')
picture = picture.convert('RGBA')
background = Image.new('RGBA', img_size, (255, 255, 255, 0))
background.paste(picture, (10,9))
background.paste(frame, (0,0))
background.save('logocopy.png', 'PNG')
EDIT:
Problem solved. I had to add alpha mask to .paste()
import Image
im = Image.open('logo.jpg')
picture = im.crop((0,0,80,80))
picture = picture.convert('RGBA')
frame = Image.open('thumb-frame.png')
background = Image.new('RGBA', frame.size, (255, 255, 255, 0))
background.paste(picture, (10,9))
background.paste(frame, (0,0), frame)
background.save('logocopy.png', 'PNG')
Here you go. This should take original picture and paste transparent frame image above it. Both pictures should be 100x100, but you can add needed resizing.
from PIL import Image
frame = Image.open('frame.png')
img = Image.open('image.jpg')
img_dest = img.copy().convert('RGBA')
img_dest.paste(frame, (0, 0, 100, 100), frame)
img_dest = img_dest.convert('RGB') # Optional, to remove transparency info
img_dest.save('output.png')