Page 1 of 1

Showing jpeg file on LCD160

Posted: Sat Feb 25, 2017 8:29 pm
by kamikaze
Hi. Is there any example on this topic? Also how to get the right format of jpeg file.. Right now I'm getting huge random text output instead of image.

Code: Select all

 1 import pyb
 2 from lcd160cr import LCD160CR, PORTRAIT_UPSIDEDOWN
 3 
 4 
 5 def test_ignitor(base_pin_name='Y12'):
 6     pin = pyb.Pin(base_pin_name)
 7     lcd = LCD160CR('X')
 8     lcd.set_orient(PORTRAIT_UPSIDEDOWN)
 9     lcd.set_text_color(lcd.rgb(255, 0, 0), lcd.rgb(0, 0, 0))
10     lcd.set_font(3)
11     lcd.erase()
12 
13     with open('/sd/frames/nuclear.jpg', 'r') as f:
14         nuclear = f.read()
15 
16     countdown = 15000
17     remaining = countdown
18     start = pyb.millis()
19 
20     while True:
21         remaining = countdown - pyb.elapsed_millis(start)
22         lcd.erase()
23         lcd.set_pos(0, 0)
24 
25         if remaining <= 0:
26             break
27 
28         lcd.write('{}'.format(remaining))
29 
30         pyb.delay(50)
31 
32     lcd.erase()
33     lcd.set_pos(0, 0)
34     lcd.jpeg(nuclear)

Re: Showing jpeg file on LCD160

Posted: Sun Feb 26, 2017 7:46 am
by pythoncoder
I can't make this work either. As far as I can see the following should work but in practice it merely erases the screen.

Code: Select all

from lcd160cr import LCD160CR
buf = bytearray(10000)
with open('/sd/test.jpg', 'rb') as f:
    f.readinto(buf)
lcd = LCD160CR('Y')
lcd.set_pen(0, 0)
lcd.erase()
lcd.set_pos(0,0)
lcd.jpeg(buf)
The test file is available here http://hinch.me.uk/lcd/test.jpg. It's a 53x53 pixel jpeg created from a photo using Gimp.

I will raise an issue on GitHub.

Re: Showing jpeg file on LCD160

Posted: Sun Feb 26, 2017 12:40 pm
by chuckbook
LCD160CR accepts raw jpeg encoded files up to a certain size (~65000 bytes). Only very basic parameters of the jpeg stream are evaluated. No EXIF decorators are accepted!
It is a good idea to strip the jpeg files used with something like:

Code: Select all

jpegtran test.jpg > test_stripped.jpg
For the example @pythoncoder provided , this results in a reduced file size of 1872 instead of 9590 bytes.

Re: Showing jpeg file on LCD160

Posted: Sun Feb 26, 2017 11:52 pm
by kamikaze
chuckbook wrote:LCD160CR accepts raw jpeg encoded files up to a certain size (~65000 bytes). Only very basic parameters of the jpeg stream are evaluated. No EXIF decorators are accepted!
It is a good idea to strip the jpeg files used with something like:

Code: Select all

jpegtran test.jpg > test_stripped.jpg
For the example @pythoncoder provided , this results in a reduced file size of 1872 instead of 9590 bytes.
jpegtran may increase the size too. Here is the JPEG file saved from Gimp.

Code: Select all

$ ls -l frames/nuclear.jpg 
-rw-r--r-- 1 kamikaze kamikaze 23551 feb 25 22:16 frames/nuclear.jpg
Running:

Code: Select all

$ jpegtran frames/nuclear.jpg > frames/nuclear_m.jpg
Result:

Code: Select all

$ ls -l frames/nuclear_m.jpg 
-rw-r--r-- 1 kamikaze kamikaze 25556 feb 27 01:49 frames/nuclear_m.jpg

Re: Showing jpeg file on LCD160

Posted: Mon Feb 27, 2017 8:13 am
by pythoncoder
I can confirm that stripping the EXIF data by the method suggested by @chuckbook fixed the problem.

Re: Showing jpeg file on LCD160

Posted: Mon Feb 27, 2017 5:19 pm
by chuckbook
@kamikaze Good point! Don't forget '-o' option for jpegtran! With this option the test file from @pythoncoder will reduce to 1537 bytes.

Re: Showing jpeg file on LCD160

Posted: Wed Mar 01, 2017 6:42 am
by pythoncoder
What is perhaps needed is a utility to run on a PC which takes as input a JPEG file and outputs Python sourcecode. The latter would declare a bytes instance containing the data and a function returning a memoryview of that instance. The Python file could then be frozen as bytecode. That way the JPEGS would consume almost zero RAM. The user would then do something like

Code: Select all

from lcd160cr import LCD160CR
import my_image
lcd = LCD160CR('Y')
lcd.set_pen(0, 0)
lcd.erase()
lcd.set_pos(0,0)
lcd.jpeg(my_image.img())
The same idea as already used for fonts: https://github.com/peterhinch/micropyth ... -to-py.git. This would mainly be of value if people were using large or multiple JPEGs.

If there's any interest I could do this.