Skip to main content


Pixel Style Image to HTML Convert in Python

Convert Image to HTML using Python

Hello Young Python buds,
I hope you are all doing well.

I apologize for not posting on my youtube channel and blog as I got tied up between some personal stuff which really got me busy.
I do had some crazy projects under my sleeve that I was working on and I would share that with you all.

What this Code does?
This Code make Pixel Style Image to HTML Convert in Python.

How it works?
- Python will Convert the Image size to 50x50 new PNG image.
- Python will then read the new PNG image pixel by pixel.
- Saving the hex data as html file.

Why 50x50 size?
I tried with fill size, it just crashes my laptop if I open the html file. I tried with 100x100 as well but it was hard to post in a blog or send it as an html. But 50x50 size work properly.

What kind of Image format works?
All kind of Image format works i.e. jpg, png, gif, webp etc...

How did I come by this Idea for this project?
Well, by accident. While making this tutorial I noticed something. And that something was I could get the pixel colour of an image and also get it on a text file. But then I wondered what can I do with this hex colour data??? And then it clicked. I though what if I get the whole data of pixel on a text file or a CSV file. Only I had to figure out how to make it work. After I got it working I had to try making the hex data and get it into a html type data. And I tried with "<div>" it worked in html at first but then as I tried to email the same it would just disrupt the whole format.
Then I tried something easier that was using "<table>", "<tr>" and "<td>".
I had lot of bugs and errors to correct and finally I made it work.






































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































Image Source I have used:- Image


So here is the code. Enjoy!!!



# Pixel Style Image to HTML Converter
# Developed by: Joel D'costa
# Free to use.
# Shared by :- Python4Fun

# import PIL as Image
from PIL import Image

# Note:- All types of file format works.
# Eg.(jpg, png, gif, webp etc...)
# Enter your Image name.
infile = "mona.jpg"

# If you increase the size your html will crash your computer. 
# Keep the default size as 50 x 50
w, h = 50, 50
image = Image.open(infile)
new_image = image.resize((w,h),Image.ANTIALIAS)
new_image.save(fp="n.png")

# It will save it as 'n.png' dont change the file name.
infile = 'n.png'
width, height = Image.open(infile).size

# Getting Pixel color function
def rgb_to_hex(rgb):
    return '#'+'%02x%02x%02x' % rgb

img = Image.open(infile)
img_rgb = img.convert('RGB')

# Our output will be stored as t_.html
with open('t_.html','+a') as t:
    t.writelines('<table style="border-spacing: 0px;">')
    for i in range(height):
        t.writelines('<tr>')
        for j in range(width):
            rgb = img_rgb.getpixel((int(j),int(i)))
            colorcode = rgb_to_hex(rgb)
            ######## You can Comment this part out ##########
            # This will try to make the white color or close to white as transparent
            if "#fff" in colorcode:
                colorcode = rgb_to_hex(rgb)+"00"
            # This will try to make the black color or close to black as transparent
            elif "000000" in colorcode:
                colorcode = colorcode.replace('000000','00000000')
            else:
                colorcode = rgb_to_hex(rgb)
            ######## You can Comment this part out ##########
            p = 15 # for width and height of the pixel
            xj = j*p
            yi = i*p
            t.writelines(f'<td style="width: {p}px; height: {p}px; background-color: {colorcode}; border-radius: 9px; border: 0px;"></td>\n')
        t.writelines('</tr>')
    t.writelines('</table>')
    

Comments

  1. Save this script in a folder and call it "Image to HTML Converter PIXEL.py"
    Save the image of any format you want to use in the same folder.
    Open the Script and change its name from "mona.jpg" and type your image name.
    Run the script to get result.
    Best result is with white background images for transparent effect on html.

    ReplyDelete

Post a Comment



🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍

Popular Posts

Python underline string, Python underline text, Underline python print

Python pip - Installing modules from IDLE (Pyton GUI) for python 3.7

Top 40 Python - String Processing in Python | Working with String in Python

Python Program - When was I born? / Date of Birth / MY BIRTHDAY (using Python3+)

Top 11 Essential Python Tips and Tricks





Subscribe to our Channel


Follow us on Facebook Page

Join our python facebook groups



Join us on Telegram