analitics

Pages

Showing posts with label PIL. Show all posts
Showing posts with label PIL. Show all posts

Monday, January 8, 2024

Python 3.12.1 : Create a simple color palette.

Today I tested a simple source code for a color palette created with PIL python package.
This is the result:
Let's see the source code:
from PIL import Image, ImageDraw

# 640x640 pixeli with 10x10 squares 64x64 
img = Image.new('RGB', (640, 640))
draw = ImageDraw.Draw(img)

# color list
colors = []
# for 100 colors you need to set steps with 62,62,64 
# or you can change 64,62,62 some little changes 
for r in range(0, 256, 62):
    for g in range(0, 256, 62):
        for b in range(0, 256, 64):
            colors.append((r, g, b))

# show result of colors and size up 100 items 
print(colors)
print(len(colors))

# create 10x10 colors and fill the image 
for i in range(10):
    for j in range(10):
        x0 = j * 64
        y0 = i * 64
        x1 = x0 + 64
        y1 = y0 + 64
        color = colors[i*10 + j]  # Selectarea culorii din lista
        draw.rectangle([x0, y0, x1, y1], fill=color)

# save teh image
img.save('rgb_color_matrix.png')

Monday, July 24, 2017

Fix Gimp with python script.

Today I will show you how python language can help GIMP users.
From my point of view, Gimp does not properly import frames from GIF files.
This program imports GIF files in this way:

Using the python module, you can get the correct frames from the GIF file.
Here's my script that uses the python PIL module.
import sys
from PIL import Image, ImageSequence
try:
        img = Image.open(sys.argv[1])
except IOError:
        print "Cant load", infile
        sys.exit(1)

pal = img.getpalette()
prev = img.convert('RGBA')
prev_dispose = True
for i, frame in enumerate(ImageSequence.Iterator(img)):
    dispose = frame.dispose

    if frame.tile:
        x0, y0, x1, y1 = frame.tile[0][1]
        if not frame.palette.dirty:
            frame.putpalette(pal)
        frame = frame.crop((x0, y0, x1, y1))
        bbox = (x0, y0, x1, y1)
    else:
        bbox = None

    if dispose is None:
        prev.paste(frame, bbox, frame.convert('RGBA'))
        prev.save('result_%03d.png' % i)
        prev_dispose = False
    else:
        if prev_dispose:
            prev = Image.new('RGBA', img.size, (0, 0, 0, 0))
        out = prev.copy()
        out.paste(frame, bbox, frame.convert('RGBA'))
        out.save('result_%03d.png' % i)
Name the python script with convert_gif.py and then you can use it on the GIF file as follows:
C:\Python27>python.exe convert_gif.py 0001.gif
The final result has a smaller number of images than in Gimp, but this was to be expected.

Thursday, February 16, 2017

Compare two images: the histogram method.

This is a very simple example about how to compare the histograms of both images and print the inconsistencies are bound to arise.
The example come with alternative solution: Histogram method.
The script was run under Fedora 25.
If the images are the same the result will be 0.0.
For testing I change the image2.png by make a line into this with a coverage of 10%.
The result of the script was:
1116.63243729
The images come with this dimensions: 738 x 502 px.
import math
import operator
from math import *
import PIL

from PIL import Image
h1 = Image.open("image1.png").histogram()
h2 = Image.open("image2.png").histogram()

rms = math.sqrt(reduce(operator.add,
        map(lambda a,b: (a-b)**2, h1, h2))/len(h1))
print rms
About the operator module exports a set of efficient functions corresponding to the intrinsic operators of Python.
Example:
operator.lt(a, b)
operator.le(a, b)
operator.eq(a, b)
operator.ne(a, b)
operator.ge(a, b)
operator.gt(a, b)
operator.__lt__(a, b)
operator.__le__(a, b)
operator.__eq__(a, b)
operator.__ne__(a, b)
operator.__ge__(a, b)
operator.__gt__(a, b)

This is like math operators:
lt(a, b) is equivalent to a < b
le(a, b) is equivalent to a <= b
Another example:
>>> # Elementwise multiplication
>>> map(mul, [0, 1, 2, 3], [10, 20, 30, 40])
[0, 20, 60, 120]

>>> # Dot product
>>> sum(map(mul, [0, 1, 2, 3], [10, 20, 30, 40]))
200

Thursday, August 11, 2016

Hide your info with stepic python module.

I will show a funny way to put your info into one image and then show this info.
First you need one image. I used this image:


First need to use Python 2.7 with Image ( Pillow python module) and stepic python module.
... and follow the below steps:

C:\Python27>cd Scripts
C:\Python27\Scripts>pip install Image
C:\Python27\Scripts>pip install stepic
C:\Python27\Scripts>cd ..

C:\Python27>python
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

To encode and then to show the text from one image I used this python script:


import PIL
from PIL import Image
import stepic
im=Image.open("MonaLisa.jpg")
im1 = stepic.encode(im,'The smallest feline is a masterpiece.')
im1.save('test_encode.jpg','JPEG')
im.show()
im1.show()
decoding=stepic.decode(im1)
data_encode=decoding.decode()
print data_encode

Sunday, August 23, 2015

Pillow python module with Python 3.4.1 .

This simple tutorial about PIL and Pillow python modules can show you how to install the Pillow python module.
I used pip3.4 and python 3.4.1 version.
Some problems about how to write your source code can be found : porting pil to pillow.

Thursday, October 17, 2013

How to make a color gradient and images with python script.

The Image and ImageDraw provide simple 2D graphics to create new images, annotate or retouch existing images, and to generate graphics.

Also this can help you to make on the fly images for you.

Let's see one example ...

First you need to import this modules and random python module

import Image,ImageDraw
from random import randint as rint

The next step : make one image , get some random numbers...

You need two colors : first is one random color and second is make from first color, see next source code:

img = Image.new("RGB", (500,500), "#FFFFFF")
draw = ImageDraw.Draw(img)
r,g,b = rint(0,255), rint(0,255), rint(0,255)
dr = (rint(0,255) - r)/500.
dg = (rint(0,255) - g)/500.
db = (rint(0,255) - b)/500.

Now you need to draw lines with this gradient of two colors.

    for i in range(500):
        r,g,b = r+dr, g+dg, b+db
        draw.line((i,0,i,500), fill=(int(r),int(g),int(b)))

... and the python script source code:

import Image,ImageDraw
from random import randint as rint

def random_gradient(name):
    img = Image.new("RGB", (500,500), "#FFFFFF")
    draw = ImageDraw.Draw(img)

    r,g,b = rint(0,255), rint(0,255), rint(0,255)
    dr = (rint(0,255) - r)/500.
    dg = (rint(0,255) - g)/500.
    db = (rint(0,255) - b)/500.
    for i in range(500):
        r,g,b = r+dr, g+dg, b+db
        draw.line((i,0,i,500), fill=(int(r),int(g),int(b)))

    img.save(name+".png", "PNG")

if __name__ == "__main__":
    for name in range(10):
        random_gradient(str(name))

The result of this script will make images :

Monday, February 25, 2013

Make Newton fractal with python

A fractal is a mathematical set that has a fractal dimension that usually exceeds its topological dimension , see Fractal wikipedia.

Today I used my mind and also my python skills to make one fractal image.

I use Newton's method to make all points and PIL python module to save the result.

Let's see the source code and comments.

from PIL import Image
#size of image
imgx = 600
imgy = 400
#make image buffer
image = Image.new("RGB", (imgx, imgy))

# area of fractal
xa = -2.0
xb = 2.0
ya = -2.0
yb = 2.0

#define constants
max_iterations = 10 # max iterations allowed
step_derivat = 0.002e-1 # step size for numerical derivative
error = 5e-19 # max error allowed

# function will generate the newton fractal
def f(z): return z * z  + complex(-0.31,0.031)

# draw derivate fractal for each y and x 
for y in range(imgy):
 zy = y * (yb - ya)/(imgy - 1) + ya
 for x in range(imgx):
  zx = x * (xb - xa)/(imgx - 1) + xa
  z = complex(zx, zy)
  for i in range(max_iterations):
   # make complex numerical derivative
   dz = (f(z + complex(step_derivat, step_derivat)) - f(z))/complex(step_derivat,step_derivat)
 # Newton iteration see wikipedia   
   z0 = z - f(z)/dz 
   # stop to the error 
   if abs(z0 - z) < error: 
    break
   z = z0
  #I use modulo operation expression to do RGB colors of the pixels 
  image.putpixel((x, y), (i % 8 * 16, i % 4 * 32,i % 2 * 64))
  
#save the result 
image.save("fractal.png", "PNG")

This is the final result of Newton fractal image:

Sunday, December 16, 2012

Simple way to create png image with an input text.

First you need to have the PIL python module and import this.

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageFilter

Next will be this function.Basically will make and return one RGBA image data type.

def make_img(image, textColor, backgroundColor):
  img = image.convert("RGBA")
  img.putdata([textColor if value == 0 else backgroundColor 
                   for value in image.getdata()])
  return img

The next step is to set the text , font and the new image.

I use Arggotsc.ttf. You can use any TrueType font.

text = " Inca un script in python! "
font = ImageFont.truetype('Arggotsc.ttf', 55)
image = Image.new("1", font.getsize(text), '#FFF')

Now we can draw, add text, resize, bur the text and finally save the image.

draw = ImageDraw.Draw(image)
draw.text((0, 0), text, font=font)
image = image.resize([i for i in image.size], Image.NEAREST)
imgText = make_img(image, (200, 200, 200), (0, 0, 0, 0))
blur_img = make_img(image, (0, 0, 0), (0, 0, 0, 0))
for i in range(3): 
  blur_img = blur_img.filter(ImageFilter.BLUR)
blur_img.paste(imgText, (0, 0), imgText)
blur_img.save("text-img.png")

The result is:


See you later with another tutorial.

Thursday, September 13, 2012

Simple python script - Voronoi diagram

Today I show you how to make Voronoi diagram using python.

I use this to make textures for underwater.

This is just one example. But you can improve to control all cells of voronoi diagram.

The theory say:

In mathematics, a Voronoi diagram is a special kind of decomposition of a metric space, determined by distances to a specified family of objects (subsets) in the space. These objects are usually called the sites or the generators...Source : wikipedia.

I used the euclidean distance to make the Voronoi diagram because it's the most familiar case.

About wikipedia - Euclidean_distance: In mathematics, the Euclidean distance or Euclidean metric is the "ordinary" distance between two points that one would measure with a ruler, and is given by the Pythagorean formula...

My python script use the next python modules:

PIL - this allow me to use image functions.

random - this module give me... random numbers.

math - some math functions.

Let's see the source code :


from PIL import Image
import random
import math

Now I make the function named gen_voronoi.

This take the height and width of the output image and the number of cells.

The function has some random variables for red , green , blue - nr,ng,nb.

The function hypot is not accessible directly so we need to import math module and using math static object.

The return value is the Euclidean norm : sqrt(x*x + y*y).


def gen_voronoi(w, h, cells):
 image = Image.new("RGB", (w, h))
 putpixel = image.putpixel
 img_x, img_y = image.size
 nx = []
 ny = []
 nr = []
 ng = []
 nb = []
 for i in range(cells):
  nx.append(random.randrange(img_x))
  ny.append(random.randrange(img_y))
  nr.append(random.randrange(256))
  ng.append(random.randrange(256))
  nb.append(random.randrange(256))
 for y in range(img_y):
  for x in range(img_x):
   dmin = math.hypot(img_x-1, img_y-1)
   j = -1
   for i in range(cells):
    d = math.hypot(nx[i]-x, ny[i]-y)
    if d < dmin:
     dmin = d
     j = i
   putpixel((x, y), (nr[j], ng[j], nb[j]))
 image.save("output.png", "PNG")
 image.show()

Use the function to make the output.png image.


gen_voronoi(200, 200, 55)

The result is :

Monday, April 30, 2012

Create tile image for your game using python script

What is tile image?
Tile image is a method of storing a sequence of images placed in a single image file.
These images are then processed according to user needs.
Here's an example below:
How we can create these images?
We can use graphics editing software to create them separately.
I used Blender 3D to create separate images.
A tutorial how to do this can be found here on section Blender 3D.
After I rendered images separately and named: 0000.png , 0001.png , 0002.png , 0003.png
I created a python script to put in an tile image, see below:
import os
import PIL
from PIL import Image
from PIL import ImageDraw
o=Image.new("RGBA",(192,48))
d= ImageDraw.Draw(o)
for pic in range(0,4):
        strpic=str(pic)
        filnam="000"+strpic+".png"
        x=pic*48
        img=Image.open(filnam)
        o.paste(img,(0+x,0))
        o.save("out.png")
The script reads the image files of size 48 pixels and puts them into one image called out.png

Resize screenshot with PIL python module .

The script that I've created is made ​​to shrink images. Some screenshots are large and should be resized to be used later on the Internet. It is a simple example that uses PIL module. This script reads the image name that I want to resize and filename that will be saved image. I use python PIL functions how to create a new image.
"""
This python script read the name of image and will create a new image with the given width and height.

$ python imgresz.py 
filename input image:test.png
test.png
filename output image:test-out.jpg
->width:500
->height:400
"""
import os 
import sys
from PIL import Image 
from PIL import ImageDraw
filnaminp=raw_input("filename input image:")
filnamout=raw_input("filename output image:")
w=input("->width:")
h=input("->height:")
imgi=Image.open(str(filnaminp))
imgo=imgi.resize((w,h),Image.BILINEAR)
imgo.save(str(filnamout))

Sunday, June 27, 2010

Add text on image with PIL module.

It's easy to create images with PIL module.
This is the script:
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
font = ImageFont.truetype("/usr/share/fonts/dejavu/DejaVuSans.ttf",25)
img=Image.new("RGBA", (200,200),(120,20,20))
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")
The only error that can occur is not to find the font.
In this case you must change the code line:
font = ImageFont.truetype("/usr/share/fonts/dejavu/DejaVuSans.ttf",25)
Here is the result script:

Saturday, January 30, 2010

How to resize images .

Sometimes it is necessary to resize the images. The PIL module is used for image processing.The glob module takes a wildcard and returns the full path of all files and directories matching the wildcard.
Here are two scripts that I made.
The first is a simple example using a resize after some dimensions.
In this case we used size 300x300.

from PIL import Image
import glob, os
size_file = 300,300
for f in glob.glob("*.png"):
file, ext = os.path.splitext(f)
img = Image.open(f)
img.thumbnail(size_file, Image.ANTIALIAS)
img.save("thumb_" + file, "JPEG")

In the second case I tried to do a resize with proportion preservation.

import glob
import PIL
from PIL import Image
for f in glob.glob("*.jpg"):
img = Image.open(f)
dim_percent=(100/float(img.size[0]))
dim_size=int((float(img.size[1])*float(dim_percent)))
img = img.resize((100,dim_size),PIL.Image.ANTIALIAS)
if f[0:2] != "trumb_":
img.save("thumb_" + f, "JPEG")

In both cases we use a renaming of files by adding the name of "thumb_".
Ambele scripturi pot fi modificate asa cum vreti.
Aceste scripturi demonstreaza cum sa folosim celor doua module "PIL" si "globe".