Wednesday, January 29, 2025

Does DeepSeek create the image from text specification?

 Conversing with DeepSeek, I was given to understand that it is not supporting creation of images. At present, it is only text.

However, it offered to create an image from text using Python and PIL. As I have been using these libraries, I gave it a try.

Here is the text specification that I used to create an image from Gemini AI [https://hodentekhelp.blogspot.com/2025/01/how-do-you-create-image-using-gemini-ai.html].

"Create an icon with the letter H decorated in yellow, with its insides filled with a web of electronic circuits. The background should be black  23-bit color with 8-bit transparency and the format in png."

Gemini AI created the following image:


Here is the suggestion from DeepSeek:

```python
from PIL import Image, ImageDraw, ImageFont

# Create a blank image with a black background and transparency
width, height = 256, 256  # Icon size
background_color = (0, 0, 0, 0)  # Black with full transparency
image = Image.new("RGBA", (width, height), background_color)

# Initialize ImageDraw
draw = ImageDraw.Draw(image)

# Draw the letter "H"
font = ImageFont.truetype("arial.ttf", 150)  # Use a system font like Arial
letter_color = (255, 255, 0, 255)  # Yellow
draw.text((75, 50), "H", font=font, fill=letter_color)

# Draw a web of electronic circuits inside the "H"
# This is a simplified representation using lines
circuit_color = (0, 255, 0, 255)  # Green for circuits
draw.line((90, 100, 90, 200), fill=circuit_color, width=2)  # Vertical linej
draw.line((90, 150, 160, 150), fill=circuit_color, width=2)  # Horizontal line
draw.line((160, 100, 160, 200), fill=circuit_color, width=2)  # Vertical line
draw.line((100, 120, 150, 120), fill=circuit_color, width=2)  # Horizontal line
draw.line((100, 180, 150, 180), fill=circuit_color, width=2)  # Horizontal line

# Save the image as a PNG file
image.save("icon_H_circuit.png", "PNG")

print("Icon created and saved as 'icon_H_circuit.png'")
```
I ran this code in PyCharm as shown:

from PIL import Image, ImageDraw, ImageFont

# Create a blank image with a black background and transparency
width, height = 256, 256 # Icon size
background_color = (0, 0, 0, 0) # Black with full transparency
image = Image.new("RGBA", (width, height), background_color)

# Initialize ImageDraw
draw = ImageDraw.Draw(image)

# Draw the letter "H"
font = ImageFont.truetype("arial.ttf", 150) # Use a system font like Arial
letter_color = (255, 255, 0, 255) # Yellow
draw.text((75, 50), "H", font=font, fill=letter_color)

# Draw a web of electronic circuits inside the "H"
# This is a simplified representation using lines
circuit_color = (0, 255, 0, 255) # Green for circuits
draw.line((90, 100, 90, 200), fill=circuit_color, width=2) # Vertical line
draw.line((90, 150, 160, 150), fill=circuit_color, width=2) # Horizontal line
draw.line((160, 100, 160, 200), fill=circuit_color, width=2) # Vertical line
draw.line((100, 120, 150, 120), fill=circuit_color, width=2) # Horizontal line
draw.line((100, 180, 150, 180), fill=circuit_color, width=2) # Horizontal line

# Save the image as a PNG file
image.save("icon_H_circuit.png", "PNG")
image.show()

print("Icon created and saved as 'icon_H_circuit.png'")

Here is the image created:

Well, given the limitations, it did create a H with a minimalist circuit (it decided the color and artwork). 

I really think, the high-end chips are needed for creating something more exotic art work or images. This said, what is the utility of imaginary sceneries with a multitude of colors, etc. 

The basic thing the AI should deliver is VALUE in the first place and in subsequent places.



Monday, January 27, 2025

DeepSeek rattles the US AI dominance. Can it continue to rattle?

 The emergence of DeepSeek AI, a powerful Chinese language model, has sent shockwaves through the US AI industry. Developed with a focus on cost-effectiveness, DeepSeek reportedly functions effectively on lower-end hardware, a stark contrast to US models heavily reliant on high-end chips like Nvidia's. This revelation triggered a significant sell-off in Nvidia stock, highlighting the potential disruption to the current AI landscape.   

https://site.financialmodelingprep.com/market-news/nasdaq-futures-plunge-amidst-concerns-over-deepseeks-impact-on-ai-chip-demand

Last night, I downloaded DeepSeek to take a peek and lo and behold, at first sight, looked as good as the Copilot, Gemini AI, and others I have come across.

Well, what does it lack?

 However, a notable limitation became apparent: DeepSeek lacks robust image generation capabilities. While it can provide code snippets (like Python with Kivy) to generate images, this approach is less user-friendly and may be hindered by the limitations of lower-end hardware in processing and rendering graphics. In contrast, US models excel in not only creating images but also seamlessly modifying them based on simple text prompts.   This is clearly beyond low-end chips.

This development necessitates a renewed focus on innovation and optimization within the US AI sector. US developers must prioritize improving model efficiency and exploring alternative hardware solutions to maintain a competitive edge. While DeepSeek presents a significant challenge, it also serves as a valuable catalyst for further advancements in AI technology.

Does Gemini AI create the image from text prompt?

 Creating an image with Gemini AI is straightforward. Simply provide a textual description of the image you need. While Gemini AI can create and display the image, it does not store it. You can download the image to the Downloads folder on a Windows computer or share it using the built-in share icon.


Here is an example of text description used to create an  image using Gemini AI on Chrome:

Create an icon with the letter H decorated in yellow, with its insides filled with a web of electronic circuits. The background should be black, 24-bit color with 8-bit transparency, and the format should be PNG. 

At first, it did not create a PNG file. Perhaps due to size constraints, it used a JPG format instead. Repeating to ask creation of a PNG file was not successful and most agents have this habit of repeating what they did neglecting to refine them according to the wishes of the user. Sometimes, they get fixated on their result across couple of future sessions. 

Other AI agents, such as Meta AI on WhatsApp and Copilot, also have the capability to create images from text. Many other AI agents offer similar functionality.

Here is the image created by Gemini AI.

The image more or less follows the description. Small refinements may sometime lead to totally different image, not an iteration on the previous. 

I wanted to check the other parts of the image description. It is possible to check the image file using PIL and Python:

from PIL import Image

def verify_image_properties(image_path):
"""
Verifies the color and transparency of an image using PIL.

Args:
image_path: Path to the image file.

Returns:
A tuple containing:
- True if the dominant color is yellow, False otherwise.
- True if the image has 24-bit color, False otherwise.
- True if the image has 8-bit transparency, False otherwise.
"""

try:
img = Image.open(path/to/your/image.png)

# Check color (simplified approximation)
dominant_color = img.getpixel((img.width // 2, img.height // 2)) # Get center pixel color
is_yellow = (dominant_color[0] > 200) and (dominant_color[1] > 200) and (dominant_color[2] < 50)

# Check color depth
is_24_bit_color = img.mode == 'RGB'

# Check transparency depth
has_8_bit_transparency = False
if img.mode == 'RGBA':
if img.info.get('dpi') is not None and len(img.info['dpi']) == 2:
has_8_bit_transparency = True

return is_yellow, is_24_bit_color, has_8_bit_transparency

except Exception as e:
print(f"Error processing image: {e}")
return False, False, False

# Example usage
image_path = "r'C:\Users\hoden\PycharmProjects\exploreImage\Images\GeminiG.jpg')" # Replace with the actual path
is_yellow, is_24_bit_color, has_8_bit_transparency = verify_image_properties(image_path)

if is_yellow:
print("Image is predominantly yellow.")
else:
print("Image is not predominantly yellow.")

if is_24_bit_color:
print("Image has 24-bit color.")
else:
print("Image does not have 24-bit color.")

if has_8_bit_transparency:
print("Image has 8-bit transparency.")
else:
print("Image does not have 8-bit transparency.")

The code returns the following:

C:\Users\hoden\AppData\Local\Programs\Python\Python312\python.exe C:\Users\hoden\PycharmProjects\exploreImage\Images\VerifyImage.py 

Image is not predominantly yellow.

Image has 24-bit color.

Image does not have 8-bit transparency.

Process finished with exit code 0

Did the Gemini AI create an image to fit our description?

The image is not predominantly yellow is true and finding the color in the image center is perhaps the wrong approach.

Other methods may yield better result than the Center Pixel Method in the code for the visible color yellow:

Center Pixel Method: This method checks the color of the pixel at the center of the image. It's simple but may not always represent the overall dominant color.

Most Common Color: This method counts the occurrences of each color and identifies the most frequent one. It can be effective but might not capture the visually dominant color if the image has a lot of background noise

K-Means Clustering: This method groups similar colors together and identifies the most visually impactful color. It's more sophisticated and can provide a better representation of the dominant color but requires more computational resources.



Sunday, January 12, 2025

Are you ready creating a label using Python?

 I assume you've read my previous post, "Is Python a language for creating GUIs?" If so, I invite you to create a label using Python and Kivy. In my previous post, I demonstrated how to install Kivy in the PyCharm IDE, and we shall start from there. However, it is not necessary to use PyCharm to create a label; it can also be done using the command line if you have Kivy and Python installed on your computer. PyCharm is particularly useful if you plan to create multiple projects and more involved programs.

Why a Label?

GUIs comprise many kinds of widgets, and perhaps the label is one of the most common and obvious elements. Labels can be seen individually or as part of larger widgets with smaller sub-widgets, such as in a form or an authentication GUI.

In its most basic form, a label is a small object that usually carries a piece of text, such as 'Welcome' or 'Hello World.' It may even carry a small image.

How do you create a label using Python?

As mentioned earlier you need Python to create a label but need a library such as Kivy. It is assumed that you have both Python and Kivy installed on your computer/laptop. Since, PyCharm is used in this example as the IDE, you create a Python file in a project in PyCharm. 

Python is a declarative language and you create upfront your python file, LabelBasic.py shown here:

===============

import kivy
from kivy.app import App
from kivy.uix.label import Label


class MyApp(App):

def build(self):
return Label(text='This is a basic label')


if __name__ == '__main__':

    MyApp().run()

These are basic things happenings:

1. You import necessary modules:

import kivy: imports Kivy library which provides tools for cross-platform GUIS.

import kivy.app import App: imports the App Class for creating kivy applications.

import kivy.uix.label import label: imports label Class to display text or images on the GUI.

2. class MyApp(App): Creates a class named MyApp that inherits from App class represent kivy application.

   def build(self): This method is required in every kivy app. It is called when the application starts and is responsible for building the user interface.

   return label(text='This is a basic label'): This creates an instance of the label class and sets its text property to 'This is a basic label'. This Label instance is then returned as the root widget of the application's UI.

3. Running the application:

  if __name__=='__main__': ensures that the following code is executed only when the script is run directly.

  MyApp().run(): creates an instance of the MyApp class and calls its run() method. This starts the Kivy application and displays the GUI.

This is the standard way of creating a GUI using Python code and the Kivy library. The PyCharm IDE has been used because all the previous posts were written to demonstrate many other types of applications such as Image studies. It is not necessary for creating a GUI element. It can be run if you have Python installed using the command-line.

The basic premise behind this code is creating an object class and instantiating the class and calling its properties to create a kivy widget.


Result of running the code:


The exception thrown running this code  in PyCharm is shown here:

================

C:\Users\hoden\AppData\Local\Programs\Python\Python312\python.exe C:\Users\hoden\PycharmProjects\PyWidgets\LabelBasic.py 

[INFO   ] [Logger      ] Record log in C:\Users\hoden\.kivy\logs\kivy_25-01-12_11.txt

[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.4.0

[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.3.1

[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.8.0

[INFO   ] [Kivy        ] v2.3.1

[INFO   ] [Kivy        ] Installed at "C:\Users\hoden\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\__init__.py"

[INFO   ] [Python      ] v3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)]

[INFO   ] [Python      ] Interpreter at "C:\Users\hoden\AppData\Local\Programs\Python\Python312\python.exe"

[INFO   ] [Logger      ] Purge log fired. Processing...

[INFO   ] [Logger      ] Purge finished!

[INFO   ] [Factory     ] 195 symbols loaded

[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)

[INFO   ] [Text        ] Provider: sdl2

[CRITICAL] App.root must be an _instance_ of Widget

 Traceback (most recent call last):

   File "C:\Users\hoden\PycharmProjects\PyWidgets\LabelBasic.py", line 13, in <module>

     MyApp().run()

   File "C:\Users\hoden\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\app.py", line 955, in run

     self._run_prepare()

   File "C:\Users\hoden\AppData\Local\Programs\Python\Python312\Lib\site-packages\kivy\app.py", line 931, in _run_prepare

     raise Exception('Invalid instance in App.root')

 Exception: Invalid instance in App.root


Process finished with exit code 1

================

There have been many instances where trying to run this simple code has resulted in the same kind of problem with the exception that ' Invalid instance in App.root. Process finished with exit code 1.

Here are the attempts:

1.https://stackoverflow.com/questions/70997766/why-do-i-get-the-error-invalid-instance-in-app-root-in-kivy-python/79331939#79331939

2. Here are few other examples of code failure:

What is the solution?

I have been looking at this problem and trying to diagnose and finally arrived at a work around. I basically suspect that the Run() method has some basic problem and I have been able to work around to make the display the label. My workaround needs introducing a Python decorator '@property' after the class statement but before the widget's build, def build(self).

The Modified code that runs error free:

import kivy
from kivy.app import App
from kivy.uix.label import Label


class MyApp(App):
@property
def build(self):
return Label(text='This is a basic label')


if __name__ == '__main__':
MyApp().run()

When this is run, the Process finished with exit code 0 and you see the following display.


This is indeed a successful run as the label is displayed. I have gone back to Stack Overflow and made some of the cold files to work with this fix.



Saturday, January 11, 2025

Is Python a language for GUI development?

 

The introduction of Computer applications with a graphic user interface was significant milestone in moving Computers and their utility from specialists and technologists to the general public. This was significantly impacted by the 'Desktop' revolution that shifted requirements from Main Frame to Tabletop devices. This also shifted the computer applications from procedural languages to more 'Object' based or object oriented languages and ushered the Internet revolution and presently enjoys enormous popularity because of their utility in every aspect of life.

GUIs simplified interactions with computers, making them more intuitive and user-friendly and led to the democratization of computer access. GUIs in Internet based applications propelled their utility in World Wide Web, e-commerce, social media and online communications and learning.

In the early days, GUIs were exclusively built using low-level languages such as C and C++. With the introduction of higher-level languages like Visual Basic and Java, object creation became significantly easier. The development of the Document Object Model (DOM) further facilitated the integration of GUIs into the Internet through web-based languages like JavaScript.

Python, being an interpreted language, doesn't come with built-in GUI elements. However, it boasts a rich ecosystem of libraries that enable the creation of sophisticated GUIs. Libraries such as Tkinter, PyQt, and Kivy offer powerful tools for developing graphical interfaces in Python.

For example:

Tkinter: A standard Python interface to the Tk GUI toolkit, great for simple applications.[https://wiki.python.org/moin/TkInter]

PyQt: A set of Python bindings for the Qt application framework, suitable for more complex and feature-rich applications.[https://en.wikipedia.org/wiki/PyQt]

Kivy: An open-source Python library for developing multitouch applications, excellent for mobile and cross-platform apps.[https://kivy.org/]

By leveraging these libraries, developers can create GUIs with superior qualities, making Python a versatile choice for both backend and frontend development. Such integration would require connection to databases and web interactions.

These libraries have means for making such a thing possible.

  • Tkinter can connect to SQLite and interact with web making requests and urllib
  • PyQt has more advanced option supporting network operations like PyQt Network and SQLAlchemy for database interactions. 
  • Kivy also uses SQLAlchemy or peewee for database interactions and requests for web interaction
For starters we will be looking at Kivy in my future posts.




Sunday, January 5, 2025

As an App developer have you tried python library KIVY? Do you know how to install it on PyCharm?

 

There are many platforms for writing Apps for mobile phones and KIVY is a open-source Python library. It can be used for developing multitouch apps on mobile devices. It's strength is once created it can run on various platforms including Windows, macOS, Linux, Android and iOS. 

Key Features of Kivy:

  • Cross-Platform: Kivy allows you to write your code once and run it on multiple platforms without modification. This is incredibly useful for developers who want to reach a broad audience.
  • Multitouch Support: Kivy is designed with multitouch in mind, making it ideal for applications that require touch input, such as mobile apps and interactive kiosks.
  • Customizable Widgets: Kivy provides a wide range of customizable widgets, allowing developers to create unique and visually appealing user interfaces. Note: It has no Radio Button.
  • GPU Acceleration: Kivy uses OpenGL to provide GPU-accelerated graphics, ensuring smooth and responsive user interfaces.
  • Community and Documentation: Kivy has a strong community and extensive documentation, making it easier for developers to find support and resources.

Is Python for GUI devleopment?

It is versatile, easy to design with straight forward syntax and run smoothly and responsively. It has the capability for multitouch and is made for 'Mobile' apps.

This post shows you how to import the KIVY library into PyCharm. PyCharm has native support for KIVY and getting it into PyCharm is a piece of cake!


Why PyCharm? 

It is the platform I am using now to develop some base knowledge for AI, Image Processing, etc. One could use Visual Basic Community Edition.

Recently, I have published many posts on PyCharm in this blog. Search for PyCharm on this blog http://hodentekHelp.blogspot.com

Importing KIVY into PyCharm:

It is very easy as the library is already in PyCharm. However, for your project you need to install it before importing into Python code.

I created a project, PyWidgets and then searched here for KIVY as shown.


Just click on Install and then after installation click on Add Package. The library package will be added to the project. You can read and contact KIVY groups from this screen.


KIVY as stated earlier is not a standalone program but a library. To use this, you need a Python program into which you bring in this library.

In future posts the usage of this library will be described and discussed. It is an important part of multi-touch mobile app development.